Code for "Lambda Expressions and Functional Interfaces. Best Practices and Tips" article
This commit is contained in:
+101
@@ -0,0 +1,101 @@
|
||||
package org.baeldung.java8;
|
||||
|
||||
import org.baeldung.Foo;
|
||||
import org.baeldung.FooExtended;
|
||||
import org.baeldung.UseFoo;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.function.Function;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotEquals;
|
||||
|
||||
public class Java8FunctionalInteracesLambdasTest {
|
||||
|
||||
private UseFoo useFoo;
|
||||
|
||||
@Before
|
||||
public void init() {
|
||||
useFoo = new UseFoo();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void functionalInterfaceInstantiation_whenReturnDefiniteString_thenCorrect() {
|
||||
|
||||
Foo foo = parameter -> parameter + "from lambda";
|
||||
String result = useFoo.add("Message ", foo);
|
||||
|
||||
assertEquals("Message from lambda", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void standardFIParameter_whenReturnDefiniteString_thenCorrect() {
|
||||
|
||||
Function<String, String> fn = parameter -> parameter + "from lambda";
|
||||
String result = useFoo.addWithStandardFI("Message ", fn);
|
||||
|
||||
assertEquals("Message from lambda", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void defaultMethodFromExtendedInterface_whenReturnDefiniteString_thenCorrect() {
|
||||
|
||||
FooExtended fooExtended = string -> string;
|
||||
String result = fooExtended.defaultMethod();
|
||||
|
||||
assertEquals("String from Bar", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void lambdaAndInnerClassInstantiation_whenReturnSameString_thenCorrect() {
|
||||
|
||||
Foo foo = parameter -> parameter + "from Foo";
|
||||
|
||||
Foo fooByIC = new Foo() {
|
||||
@Override
|
||||
public String method(String string) {
|
||||
return string + "from Foo";
|
||||
}
|
||||
};
|
||||
|
||||
assertEquals(foo.method("Something "), fooByIC.method("Something "));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void accessVariablesFromDifferentScopes_whenReturnPredefinedString_thenCorrect() {
|
||||
|
||||
assertEquals("Results: resultIC = Inner class value, resultLambda = Enclosing scope value",
|
||||
useFoo.scopeExperiment());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shorteningLambdas_whenReturnEqualsResults_thenCorrect() {
|
||||
|
||||
Foo foo = parameter -> buildString(parameter);
|
||||
|
||||
Foo fooHuge = parameter -> { String result = "Something " + parameter;
|
||||
//many lines of code
|
||||
return result;
|
||||
};
|
||||
|
||||
assertEquals(foo.method("Something"), fooHuge.method("Something"));
|
||||
}
|
||||
|
||||
private String buildString(String parameter) {
|
||||
String result = "Something " + parameter;
|
||||
//many lines of code
|
||||
return result;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void mutatingOfEffectivelyFinalVariable_whenNotEquals_thenCorrect() {
|
||||
|
||||
int[] total = new int[1];
|
||||
Runnable r = () -> total[0]++;
|
||||
r.run();
|
||||
|
||||
assertNotEquals(0, total[0]);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -15,15 +15,24 @@ import java.util.concurrent.atomic.AtomicLong;
|
||||
import java.util.stream.StreamSupport;
|
||||
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class JavaFolderSizeTest {
|
||||
|
||||
private String path;
|
||||
|
||||
@Before
|
||||
public void init() {
|
||||
final String separator = File.separator;
|
||||
path = "src" + separator + "test" + separator + "resources";
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenGetFolderSizeRecursive_thenCorrect() {
|
||||
final long expectedSize = 136;
|
||||
|
||||
final File folder = new File("src/test/resources");
|
||||
final File folder = new File(path);
|
||||
final long size = getFolderSize(folder);
|
||||
|
||||
assertEquals(expectedSize, size);
|
||||
@@ -34,7 +43,7 @@ public class JavaFolderSizeTest {
|
||||
final long expectedSize = 136;
|
||||
|
||||
final AtomicLong size = new AtomicLong(0);
|
||||
final Path folder = Paths.get("src/test/resources");
|
||||
final Path folder = Paths.get(path);
|
||||
|
||||
Files.walkFileTree(folder, new SimpleFileVisitor<Path>() {
|
||||
@Override
|
||||
@@ -51,7 +60,7 @@ public class JavaFolderSizeTest {
|
||||
public void whenGetFolderSizeUsingJava8_thenCorrect() throws IOException {
|
||||
final long expectedSize = 136;
|
||||
|
||||
final Path folder = Paths.get("src/test/resources");
|
||||
final Path folder = Paths.get(path);
|
||||
final long size = Files.walk(folder).filter(p -> p.toFile().isFile()).mapToLong(p -> p.toFile().length()).sum();
|
||||
|
||||
assertEquals(expectedSize, size);
|
||||
@@ -61,7 +70,7 @@ public class JavaFolderSizeTest {
|
||||
public void whenGetFolderSizeUsingApacheCommonsIO_thenCorrect() {
|
||||
final long expectedSize = 136;
|
||||
|
||||
final File folder = new File("src/test/resources");
|
||||
final File folder = new File(path);
|
||||
final long size = FileUtils.sizeOfDirectory(folder);
|
||||
|
||||
assertEquals(expectedSize, size);
|
||||
@@ -71,7 +80,7 @@ public class JavaFolderSizeTest {
|
||||
public void whenGetFolderSizeUsingGuava_thenCorrect() {
|
||||
final long expectedSize = 136;
|
||||
|
||||
final File folder = new File("src/test/resources");
|
||||
final File folder = new File(path);
|
||||
|
||||
final Iterable<File> files = com.google.common.io.Files.fileTreeTraverser().breadthFirstTraversal(folder);
|
||||
final long size = StreamSupport.stream(files.spliterator(), false).filter(f -> f.isFile()).mapToLong(File::length).sum();
|
||||
@@ -81,7 +90,7 @@ public class JavaFolderSizeTest {
|
||||
|
||||
@Test
|
||||
public void whenGetReadableSize_thenCorrect() {
|
||||
final File folder = new File("src/test/resources");
|
||||
final File folder = new File(path);
|
||||
final long size = getFolderSize(folder);
|
||||
|
||||
final String[] units = new String[] { "B", "KB", "MB", "GB", "TB" };
|
||||
|
||||
Reference in New Issue
Block a user