Skip to content

Commit 4a6c23c

Browse files
committed
Code for "Lambda Expressions and Functional Interfaces. Best Practices and Tips" article
1 parent e4e8042 commit 4a6c23c

File tree

9 files changed

+226
-6
lines changed

9 files changed

+226
-6
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package org.baeldung;
2+
3+
import java.util.function.Consumer;
4+
import java.util.function.Function;
5+
6+
public interface Adder {
7+
8+
String addWithFunction(Function<String, String> f);
9+
10+
void addWithConsumer(Consumer<Integer> f);
11+
12+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package org.baeldung;
2+
3+
import java.util.function.Consumer;
4+
import java.util.function.Function;
5+
6+
public class AdderImpl implements Adder {
7+
8+
@Override
9+
public String addWithFunction(Function<String, String> f) {
10+
return f.apply("Something ");
11+
}
12+
13+
@Override
14+
public void addWithConsumer(Consumer<Integer> f) {}
15+
16+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package org.baeldung;
2+
3+
@FunctionalInterface
4+
public interface Bar {
5+
6+
String method(String string);
7+
8+
default String defaultMethod() {
9+
return "String from Bar";
10+
}
11+
12+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package org.baeldung;
2+
3+
@FunctionalInterface
4+
public interface Baz {
5+
6+
String method(String string);
7+
8+
default String defaultMethod() {
9+
return "String from Baz";
10+
}
11+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package org.baeldung;
2+
3+
@FunctionalInterface
4+
public interface Foo {
5+
6+
String method(String string);
7+
8+
default void defaultMethod() {}
9+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package org.baeldung;
2+
3+
@FunctionalInterface
4+
public interface FooExtended extends Baz, Bar {
5+
6+
@Override
7+
default String defaultMethod() {
8+
return Bar.super.defaultMethod();
9+
}
10+
11+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package org.baeldung;
2+
3+
import java.util.function.Function;
4+
5+
public class UseFoo {
6+
7+
private String value = "Enclosing scope value";
8+
9+
public String add(String string, Foo foo) {
10+
return foo.method(string);
11+
}
12+
13+
public String addWithStandardFI(String string, Function<String, String> fn) {
14+
return fn.apply(string);
15+
}
16+
17+
public String scopeExperiment() {
18+
Foo fooIC = new Foo() {
19+
String value = "Inner class value";
20+
21+
@Override
22+
public String method(String string) {
23+
return this.value;
24+
}
25+
};
26+
String resultIC = fooIC.method("");
27+
28+
Foo fooLambda = parameter -> {
29+
String value = "Lambda value";
30+
return this.value;
31+
};
32+
String resultLambda = fooLambda.method("");
33+
34+
return "Results: resultIC = " + resultIC +
35+
", resultLambda = " + resultLambda;
36+
}
37+
38+
39+
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package org.baeldung.java8;
2+
3+
import org.baeldung.Foo;
4+
import org.baeldung.FooExtended;
5+
import org.baeldung.UseFoo;
6+
import org.junit.Before;
7+
import org.junit.Test;
8+
9+
import java.util.function.Function;
10+
11+
import static org.junit.Assert.assertEquals;
12+
import static org.junit.Assert.assertNotEquals;
13+
14+
public class Java8FunctionalInteracesLambdasTest {
15+
16+
private UseFoo useFoo;
17+
18+
@Before
19+
public void init() {
20+
useFoo = new UseFoo();
21+
}
22+
23+
@Test
24+
public void functionalInterfaceInstantiation_whenReturnDefiniteString_thenCorrect() {
25+
26+
Foo foo = parameter -> parameter + "from lambda";
27+
String result = useFoo.add("Message ", foo);
28+
29+
assertEquals("Message from lambda", result);
30+
}
31+
32+
@Test
33+
public void standardFIParameter_whenReturnDefiniteString_thenCorrect() {
34+
35+
Function<String, String> fn = parameter -> parameter + "from lambda";
36+
String result = useFoo.addWithStandardFI("Message ", fn);
37+
38+
assertEquals("Message from lambda", result);
39+
}
40+
41+
@Test
42+
public void defaultMethodFromExtendedInterface_whenReturnDefiniteString_thenCorrect() {
43+
44+
FooExtended fooExtended = string -> string;
45+
String result = fooExtended.defaultMethod();
46+
47+
assertEquals("String from Bar", result);
48+
}
49+
50+
@Test
51+
public void lambdaAndInnerClassInstantiation_whenReturnSameString_thenCorrect() {
52+
53+
Foo foo = parameter -> parameter + "from Foo";
54+
55+
Foo fooByIC = new Foo() {
56+
@Override
57+
public String method(String string) {
58+
return string + "from Foo";
59+
}
60+
};
61+
62+
assertEquals(foo.method("Something "), fooByIC.method("Something "));
63+
}
64+
65+
@Test
66+
public void accessVariablesFromDifferentScopes_whenReturnPredefinedString_thenCorrect() {
67+
68+
assertEquals("Results: resultIC = Inner class value, resultLambda = Enclosing scope value",
69+
useFoo.scopeExperiment());
70+
}
71+
72+
@Test
73+
public void shorteningLambdas_whenReturnEqualsResults_thenCorrect() {
74+
75+
Foo foo = parameter -> buildString(parameter);
76+
77+
Foo fooHuge = parameter -> { String result = "Something " + parameter;
78+
//many lines of code
79+
return result;
80+
};
81+
82+
assertEquals(foo.method("Something"), fooHuge.method("Something"));
83+
}
84+
85+
private String buildString(String parameter) {
86+
String result = "Something " + parameter;
87+
//many lines of code
88+
return result;
89+
}
90+
91+
@Test
92+
public void mutatingOfEffectivelyFinalVariable_whenNotEquals_thenCorrect() {
93+
94+
int[] total = new int[1];
95+
Runnable r = () -> total[0]++;
96+
r.run();
97+
98+
assertNotEquals(0, total[0]);
99+
}
100+
101+
}

core-java-8/src/test/java/org/baeldung/java8/JavaFolderSizeTest.java

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,24 @@
1515
import java.util.stream.StreamSupport;
1616

1717
import org.apache.commons.io.FileUtils;
18+
import org.junit.Before;
1819
import org.junit.Test;
1920

2021
public class JavaFolderSizeTest {
2122

23+
private String path;
24+
25+
@Before
26+
public void init() {
27+
final String separator = File.separator;
28+
path = "src" + separator + "test" + separator + "resources";
29+
}
30+
2231
@Test
2332
public void whenGetFolderSizeRecursive_thenCorrect() {
2433
final long expectedSize = 136;
2534

26-
final File folder = new File("src/test/resources");
35+
final File folder = new File(path);
2736
final long size = getFolderSize(folder);
2837

2938
assertEquals(expectedSize, size);
@@ -34,7 +43,7 @@ public void whenGetFolderSizeUsingJava7_thenCorrect() throws IOException {
3443
final long expectedSize = 136;
3544

3645
final AtomicLong size = new AtomicLong(0);
37-
final Path folder = Paths.get("src/test/resources");
46+
final Path folder = Paths.get(path);
3847

3948
Files.walkFileTree(folder, new SimpleFileVisitor<Path>() {
4049
@Override
@@ -51,7 +60,7 @@ public FileVisitResult visitFile(final Path file, final BasicFileAttributes attr
5160
public void whenGetFolderSizeUsingJava8_thenCorrect() throws IOException {
5261
final long expectedSize = 136;
5362

54-
final Path folder = Paths.get("src/test/resources");
63+
final Path folder = Paths.get(path);
5564
final long size = Files.walk(folder).filter(p -> p.toFile().isFile()).mapToLong(p -> p.toFile().length()).sum();
5665

5766
assertEquals(expectedSize, size);
@@ -61,7 +70,7 @@ public void whenGetFolderSizeUsingJava8_thenCorrect() throws IOException {
6170
public void whenGetFolderSizeUsingApacheCommonsIO_thenCorrect() {
6271
final long expectedSize = 136;
6372

64-
final File folder = new File("src/test/resources");
73+
final File folder = new File(path);
6574
final long size = FileUtils.sizeOfDirectory(folder);
6675

6776
assertEquals(expectedSize, size);
@@ -71,7 +80,7 @@ public void whenGetFolderSizeUsingApacheCommonsIO_thenCorrect() {
7180
public void whenGetFolderSizeUsingGuava_thenCorrect() {
7281
final long expectedSize = 136;
7382

74-
final File folder = new File("src/test/resources");
83+
final File folder = new File(path);
7584

7685
final Iterable<File> files = com.google.common.io.Files.fileTreeTraverser().breadthFirstTraversal(folder);
7786
final long size = StreamSupport.stream(files.spliterator(), false).filter(f -> f.isFile()).mapToLong(File::length).sum();
@@ -81,7 +90,7 @@ public void whenGetFolderSizeUsingGuava_thenCorrect() {
8190

8291
@Test
8392
public void whenGetReadableSize_thenCorrect() {
84-
final File folder = new File("src/test/resources");
93+
final File folder = new File(path);
8594
final long size = getFolderSize(folder);
8695

8796
final String[] units = new String[] { "B", "KB", "MB", "GB", "TB" };

0 commit comments

Comments
 (0)