Code for "Lambda Expressions and Functional Interfaces. Best Practices and Tips" article

This commit is contained in:
Alex V 2015-12-31 05:14:39 +02:00
parent e4e8042d73
commit 4a6c23cac6
9 changed files with 226 additions and 6 deletions

View File

@ -0,0 +1,12 @@
package org.baeldung;
import java.util.function.Consumer;
import java.util.function.Function;
public interface Adder {
String addWithFunction(Function<String, String> f);
void addWithConsumer(Consumer<Integer> f);
}

View File

@ -0,0 +1,16 @@
package org.baeldung;
import java.util.function.Consumer;
import java.util.function.Function;
public class AdderImpl implements Adder {
@Override
public String addWithFunction(Function<String, String> f) {
return f.apply("Something ");
}
@Override
public void addWithConsumer(Consumer<Integer> f) {}
}

View File

@ -0,0 +1,12 @@
package org.baeldung;
@FunctionalInterface
public interface Bar {
String method(String string);
default String defaultMethod() {
return "String from Bar";
}
}

View File

@ -0,0 +1,11 @@
package org.baeldung;
@FunctionalInterface
public interface Baz {
String method(String string);
default String defaultMethod() {
return "String from Baz";
}
}

View File

@ -0,0 +1,9 @@
package org.baeldung;
@FunctionalInterface
public interface Foo {
String method(String string);
default void defaultMethod() {}
}

View File

@ -0,0 +1,11 @@
package org.baeldung;
@FunctionalInterface
public interface FooExtended extends Baz, Bar {
@Override
default String defaultMethod() {
return Bar.super.defaultMethod();
}
}

View File

@ -0,0 +1,39 @@
package org.baeldung;
import java.util.function.Function;
public class UseFoo {
private String value = "Enclosing scope value";
public String add(String string, Foo foo) {
return foo.method(string);
}
public String addWithStandardFI(String string, Function<String, String> fn) {
return fn.apply(string);
}
public String scopeExperiment() {
Foo fooIC = new Foo() {
String value = "Inner class value";
@Override
public String method(String string) {
return this.value;
}
};
String resultIC = fooIC.method("");
Foo fooLambda = parameter -> {
String value = "Lambda value";
return this.value;
};
String resultLambda = fooLambda.method("");
return "Results: resultIC = " + resultIC +
", resultLambda = " + resultLambda;
}
}

View File

@ -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]);
}
}

View File

@ -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" };