minor cleanup work
This commit is contained in:
@@ -6,11 +6,12 @@ import java.util.function.Function;
|
||||
public class AdderImpl implements Adder {
|
||||
|
||||
@Override
|
||||
public String addWithFunction(Function<String, String> f) {
|
||||
public String addWithFunction(Function<String, String> f) {
|
||||
return f.apply("Something ");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addWithConsumer(Consumer<Integer> f) {}
|
||||
public void addWithConsumer(Consumer<Integer> f) {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -5,5 +5,6 @@ public interface Foo {
|
||||
|
||||
String method(String string);
|
||||
|
||||
default void defaultMethod() {}
|
||||
default void defaultMethod() {
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,34 +6,32 @@ public class UseFoo {
|
||||
|
||||
private String value = "Enclosing scope value";
|
||||
|
||||
public String add(String string, Foo foo) {
|
||||
public String add(final String string, final Foo foo) {
|
||||
return foo.method(string);
|
||||
}
|
||||
|
||||
public String addWithStandardFI(String string, Function<String, String> fn) {
|
||||
public String addWithStandardFI(final String string, final Function<String, String> fn) {
|
||||
return fn.apply(string);
|
||||
}
|
||||
|
||||
public String scopeExperiment() {
|
||||
Foo fooIC = new Foo() {
|
||||
final Foo fooIC = new Foo() {
|
||||
String value = "Inner class value";
|
||||
|
||||
@Override
|
||||
public String method(String string) {
|
||||
return this.value;
|
||||
public String method(final String string) {
|
||||
return value;
|
||||
}
|
||||
};
|
||||
String resultIC = fooIC.method("");
|
||||
final String resultIC = fooIC.method("");
|
||||
|
||||
Foo fooLambda = parameter -> {
|
||||
String value = "Lambda value";
|
||||
final Foo fooLambda = parameter -> {
|
||||
final String value = "Lambda value";
|
||||
return this.value;
|
||||
};
|
||||
String resultLambda = fooLambda.method("");
|
||||
final String resultLambda = fooLambda.method("");
|
||||
|
||||
return "Results: resultIC = " + resultIC +
|
||||
", resultLambda = " + resultLambda;
|
||||
return "Results: resultIC = " + resultIC + ", resultLambda = " + resultLambda;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
+27
-33
@@ -1,15 +1,16 @@
|
||||
package com.baeldung.java8;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotEquals;
|
||||
|
||||
import java.util.function.Function;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.Foo;
|
||||
import com.baeldung.FooExtended;
|
||||
import com.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 {
|
||||
|
||||
@@ -22,39 +23,35 @@ public class Java8FunctionalInteracesLambdasTest {
|
||||
|
||||
@Test
|
||||
public void functionalInterfaceInstantiation_whenReturnDefiniteString_thenCorrect() {
|
||||
|
||||
Foo foo = parameter -> parameter + "from lambda";
|
||||
String result = useFoo.add("Message ", foo);
|
||||
final Foo foo = parameter -> parameter + "from lambda";
|
||||
final 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);
|
||||
final Function<String, String> fn = parameter -> parameter + "from lambda";
|
||||
final 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();
|
||||
final FooExtended fooExtended = string -> string;
|
||||
final String result = fooExtended.defaultMethod();
|
||||
|
||||
assertEquals("String from Bar", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void lambdaAndInnerClassInstantiation_whenReturnSameString_thenCorrect() {
|
||||
final Foo foo = parameter -> parameter + "from Foo";
|
||||
|
||||
Foo foo = parameter -> parameter + "from Foo";
|
||||
|
||||
Foo fooByIC = new Foo() {
|
||||
final Foo fooByIC = new Foo() {
|
||||
@Override
|
||||
public String method(String string) {
|
||||
public String method(final String string) {
|
||||
return string + "from Foo";
|
||||
}
|
||||
};
|
||||
@@ -64,35 +61,32 @@ public class Java8FunctionalInteracesLambdasTest {
|
||||
|
||||
@Test
|
||||
public void accessVariablesFromDifferentScopes_whenReturnPredefinedString_thenCorrect() {
|
||||
|
||||
assertEquals("Results: resultIC = Inner class value, resultLambda = Enclosing scope value",
|
||||
useFoo.scopeExperiment());
|
||||
assertEquals("Results: resultIC = Inner class value, resultLambda = Enclosing scope value", useFoo.scopeExperiment());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shorteningLambdas_whenReturnEqualsResults_thenCorrect() {
|
||||
final Foo foo = parameter -> buildString(parameter);
|
||||
|
||||
Foo foo = parameter -> buildString(parameter);
|
||||
|
||||
Foo fooHuge = parameter -> { String result = "Something " + parameter;
|
||||
//many lines of code
|
||||
final Foo fooHuge = parameter -> {
|
||||
final 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
|
||||
private String buildString(final String parameter) {
|
||||
final 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]++;
|
||||
final int[] total = new int[1];
|
||||
final Runnable r = () -> total[0]++;
|
||||
r.run();
|
||||
|
||||
assertNotEquals(0, total[0]);
|
||||
|
||||
@@ -6,10 +6,10 @@ import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
import com.baeldung.java8.entity.Human;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.java8.entity.Human;
|
||||
import com.google.common.collect.Lists;
|
||||
import com.google.common.primitives.Ints;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user