minor cleanup work

This commit is contained in:
eugenp 2016-01-09 01:34:39 +02:00 committed by David Morley
parent e2d30f5683
commit 3d564b081e
6 changed files with 50 additions and 51 deletions

View File

@ -1,4 +1,5 @@
eclipse.preferences.version=1 eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.annotation.inheritNullAnnotations=disabled
org.eclipse.jdt.core.compiler.annotation.missingNonNullByDefaultAnnotation=ignore org.eclipse.jdt.core.compiler.annotation.missingNonNullByDefaultAnnotation=ignore
org.eclipse.jdt.core.compiler.annotation.nonnull=org.eclipse.jdt.annotation.NonNull org.eclipse.jdt.core.compiler.annotation.nonnull=org.eclipse.jdt.annotation.NonNull
org.eclipse.jdt.core.compiler.annotation.nonnullbydefault=org.eclipse.jdt.annotation.NonNullByDefault org.eclipse.jdt.core.compiler.annotation.nonnullbydefault=org.eclipse.jdt.annotation.NonNullByDefault
@ -26,7 +27,7 @@ org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.explicitlyClosedAutoCloseable=ignore org.eclipse.jdt.core.compiler.problem.explicitlyClosedAutoCloseable=ignore
org.eclipse.jdt.core.compiler.problem.fallthroughCase=ignore org.eclipse.jdt.core.compiler.problem.fallthroughCase=ignore
org.eclipse.jdt.core.compiler.problem.fatalOptionalError=disabled org.eclipse.jdt.core.compiler.problem.fatalOptionalError=disabled
org.eclipse.jdt.core.compiler.problem.fieldHiding=error org.eclipse.jdt.core.compiler.problem.fieldHiding=warning
org.eclipse.jdt.core.compiler.problem.finalParameterBound=warning org.eclipse.jdt.core.compiler.problem.finalParameterBound=warning
org.eclipse.jdt.core.compiler.problem.finallyBlockNotCompletingNormally=warning org.eclipse.jdt.core.compiler.problem.finallyBlockNotCompletingNormally=warning
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
@ -35,7 +36,7 @@ org.eclipse.jdt.core.compiler.problem.includeNullInfoFromAsserts=disabled
org.eclipse.jdt.core.compiler.problem.incompatibleNonInheritedInterfaceMethod=warning org.eclipse.jdt.core.compiler.problem.incompatibleNonInheritedInterfaceMethod=warning
org.eclipse.jdt.core.compiler.problem.incompleteEnumSwitch=ignore org.eclipse.jdt.core.compiler.problem.incompleteEnumSwitch=ignore
org.eclipse.jdt.core.compiler.problem.indirectStaticAccess=ignore org.eclipse.jdt.core.compiler.problem.indirectStaticAccess=ignore
org.eclipse.jdt.core.compiler.problem.localVariableHiding=error org.eclipse.jdt.core.compiler.problem.localVariableHiding=warning
org.eclipse.jdt.core.compiler.problem.methodWithConstructorName=warning org.eclipse.jdt.core.compiler.problem.methodWithConstructorName=warning
org.eclipse.jdt.core.compiler.problem.missingDefaultCase=ignore org.eclipse.jdt.core.compiler.problem.missingDefaultCase=ignore
org.eclipse.jdt.core.compiler.problem.missingDeprecatedAnnotation=ignore org.eclipse.jdt.core.compiler.problem.missingDeprecatedAnnotation=ignore
@ -48,6 +49,7 @@ org.eclipse.jdt.core.compiler.problem.missingSynchronizedOnInheritedMethod=ignor
org.eclipse.jdt.core.compiler.problem.noEffectAssignment=warning org.eclipse.jdt.core.compiler.problem.noEffectAssignment=warning
org.eclipse.jdt.core.compiler.problem.noImplicitStringConversion=warning org.eclipse.jdt.core.compiler.problem.noImplicitStringConversion=warning
org.eclipse.jdt.core.compiler.problem.nonExternalizedStringLiteral=ignore org.eclipse.jdt.core.compiler.problem.nonExternalizedStringLiteral=ignore
org.eclipse.jdt.core.compiler.problem.nonnullParameterAnnotationDropped=warning
org.eclipse.jdt.core.compiler.problem.nullAnnotationInferenceConflict=error org.eclipse.jdt.core.compiler.problem.nullAnnotationInferenceConflict=error
org.eclipse.jdt.core.compiler.problem.nullReference=warning org.eclipse.jdt.core.compiler.problem.nullReference=warning
org.eclipse.jdt.core.compiler.problem.nullSpecViolation=error org.eclipse.jdt.core.compiler.problem.nullSpecViolation=error
@ -68,6 +70,7 @@ org.eclipse.jdt.core.compiler.problem.specialParameterHidingField=disabled
org.eclipse.jdt.core.compiler.problem.staticAccessReceiver=warning org.eclipse.jdt.core.compiler.problem.staticAccessReceiver=warning
org.eclipse.jdt.core.compiler.problem.suppressOptionalErrors=disabled org.eclipse.jdt.core.compiler.problem.suppressOptionalErrors=disabled
org.eclipse.jdt.core.compiler.problem.suppressWarnings=enabled org.eclipse.jdt.core.compiler.problem.suppressWarnings=enabled
org.eclipse.jdt.core.compiler.problem.syntacticNullAnalysisForFields=disabled
org.eclipse.jdt.core.compiler.problem.syntheticAccessEmulation=ignore org.eclipse.jdt.core.compiler.problem.syntheticAccessEmulation=ignore
org.eclipse.jdt.core.compiler.problem.typeParameterHiding=error org.eclipse.jdt.core.compiler.problem.typeParameterHiding=error
org.eclipse.jdt.core.compiler.problem.unavoidableGenericTypeProblems=enabled org.eclipse.jdt.core.compiler.problem.unavoidableGenericTypeProblems=enabled
@ -82,6 +85,7 @@ org.eclipse.jdt.core.compiler.problem.unusedDeclaredThrownException=ignore
org.eclipse.jdt.core.compiler.problem.unusedDeclaredThrownExceptionExemptExceptionAndThrowable=enabled org.eclipse.jdt.core.compiler.problem.unusedDeclaredThrownExceptionExemptExceptionAndThrowable=enabled
org.eclipse.jdt.core.compiler.problem.unusedDeclaredThrownExceptionIncludeDocCommentReference=enabled org.eclipse.jdt.core.compiler.problem.unusedDeclaredThrownExceptionIncludeDocCommentReference=enabled
org.eclipse.jdt.core.compiler.problem.unusedDeclaredThrownExceptionWhenOverriding=disabled org.eclipse.jdt.core.compiler.problem.unusedDeclaredThrownExceptionWhenOverriding=disabled
org.eclipse.jdt.core.compiler.problem.unusedExceptionParameter=ignore
org.eclipse.jdt.core.compiler.problem.unusedImport=warning org.eclipse.jdt.core.compiler.problem.unusedImport=warning
org.eclipse.jdt.core.compiler.problem.unusedLabel=warning org.eclipse.jdt.core.compiler.problem.unusedLabel=warning
org.eclipse.jdt.core.compiler.problem.unusedLocal=warning org.eclipse.jdt.core.compiler.problem.unusedLocal=warning
@ -91,6 +95,7 @@ org.eclipse.jdt.core.compiler.problem.unusedParameterIncludeDocCommentReference=
org.eclipse.jdt.core.compiler.problem.unusedParameterWhenImplementingAbstract=disabled org.eclipse.jdt.core.compiler.problem.unusedParameterWhenImplementingAbstract=disabled
org.eclipse.jdt.core.compiler.problem.unusedParameterWhenOverridingConcrete=disabled org.eclipse.jdt.core.compiler.problem.unusedParameterWhenOverridingConcrete=disabled
org.eclipse.jdt.core.compiler.problem.unusedPrivateMember=warning org.eclipse.jdt.core.compiler.problem.unusedPrivateMember=warning
org.eclipse.jdt.core.compiler.problem.unusedTypeParameter=ignore
org.eclipse.jdt.core.compiler.problem.unusedWarningToken=warning org.eclipse.jdt.core.compiler.problem.unusedWarningToken=warning
org.eclipse.jdt.core.compiler.problem.varargsArgumentNeedCast=warning org.eclipse.jdt.core.compiler.problem.varargsArgumentNeedCast=warning
org.eclipse.jdt.core.compiler.source=1.8 org.eclipse.jdt.core.compiler.source=1.8

View File

@ -6,11 +6,12 @@ import java.util.function.Function;
public class AdderImpl implements Adder { public class AdderImpl implements Adder {
@Override @Override
public String addWithFunction(Function<String, String> f) { public String addWithFunction(Function<String, String> f) {
return f.apply("Something "); return f.apply("Something ");
} }
@Override @Override
public void addWithConsumer(Consumer<Integer> f) {} public void addWithConsumer(Consumer<Integer> f) {
}
} }

View File

@ -5,5 +5,6 @@ public interface Foo {
String method(String string); String method(String string);
default void defaultMethod() {} default void defaultMethod() {
}
} }

View File

@ -6,34 +6,32 @@ public class UseFoo {
private String value = "Enclosing scope value"; 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); 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); return fn.apply(string);
} }
public String scopeExperiment() { public String scopeExperiment() {
Foo fooIC = new Foo() { final Foo fooIC = new Foo() {
String value = "Inner class value"; String value = "Inner class value";
@Override @Override
public String method(String string) { public String method(final String string) {
return this.value; return value;
} }
}; };
String resultIC = fooIC.method(""); final String resultIC = fooIC.method("");
Foo fooLambda = parameter -> { final Foo fooLambda = parameter -> {
String value = "Lambda value"; final String value = "Lambda value";
return this.value; return this.value;
}; };
String resultLambda = fooLambda.method(""); final String resultLambda = fooLambda.method("");
return "Results: resultIC = " + resultIC + return "Results: resultIC = " + resultIC + ", resultLambda = " + resultLambda;
", resultLambda = " + resultLambda;
} }
} }

View File

@ -1,15 +1,16 @@
package com.baeldung.java8; 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.Foo;
import com.baeldung.FooExtended; import com.baeldung.FooExtended;
import com.baeldung.UseFoo; 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 { public class Java8FunctionalInteracesLambdasTest {
@ -22,39 +23,35 @@ public class Java8FunctionalInteracesLambdasTest {
@Test @Test
public void functionalInterfaceInstantiation_whenReturnDefiniteString_thenCorrect() { public void functionalInterfaceInstantiation_whenReturnDefiniteString_thenCorrect() {
final Foo foo = parameter -> parameter + "from lambda";
Foo foo = parameter -> parameter + "from lambda"; final String result = useFoo.add("Message ", foo);
String result = useFoo.add("Message ", foo);
assertEquals("Message from lambda", result); assertEquals("Message from lambda", result);
} }
@Test @Test
public void standardFIParameter_whenReturnDefiniteString_thenCorrect() { public void standardFIParameter_whenReturnDefiniteString_thenCorrect() {
final Function<String, String> fn = parameter -> parameter + "from lambda";
Function<String, String> fn = parameter -> parameter + "from lambda"; final String result = useFoo.addWithStandardFI("Message ", fn);
String result = useFoo.addWithStandardFI("Message ", fn);
assertEquals("Message from lambda", result); assertEquals("Message from lambda", result);
} }
@Test @Test
public void defaultMethodFromExtendedInterface_whenReturnDefiniteString_thenCorrect() { public void defaultMethodFromExtendedInterface_whenReturnDefiniteString_thenCorrect() {
final FooExtended fooExtended = string -> string;
FooExtended fooExtended = string -> string; final String result = fooExtended.defaultMethod();
String result = fooExtended.defaultMethod();
assertEquals("String from Bar", result); assertEquals("String from Bar", result);
} }
@Test @Test
public void lambdaAndInnerClassInstantiation_whenReturnSameString_thenCorrect() { public void lambdaAndInnerClassInstantiation_whenReturnSameString_thenCorrect() {
final Foo foo = parameter -> parameter + "from Foo";
Foo foo = parameter -> parameter + "from Foo"; final Foo fooByIC = new Foo() {
Foo fooByIC = new Foo() {
@Override @Override
public String method(String string) { public String method(final String string) {
return string + "from Foo"; return string + "from Foo";
} }
}; };
@ -64,35 +61,32 @@ public class Java8FunctionalInteracesLambdasTest {
@Test @Test
public void accessVariablesFromDifferentScopes_whenReturnPredefinedString_thenCorrect() { 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 @Test
public void shorteningLambdas_whenReturnEqualsResults_thenCorrect() { public void shorteningLambdas_whenReturnEqualsResults_thenCorrect() {
final Foo foo = parameter -> buildString(parameter);
Foo foo = parameter -> buildString(parameter); final Foo fooHuge = parameter -> {
final String result = "Something " + parameter;
Foo fooHuge = parameter -> { String result = "Something " + parameter; // many lines of code
//many lines of code
return result; return result;
}; };
assertEquals(foo.method("Something"), fooHuge.method("Something")); assertEquals(foo.method("Something"), fooHuge.method("Something"));
} }
private String buildString(String parameter) { private String buildString(final String parameter) {
String result = "Something " + parameter; final String result = "Something " + parameter;
//many lines of code // many lines of code
return result; return result;
} }
@Test @Test
public void mutatingOfEffectivelyFinalVariable_whenNotEquals_thenCorrect() { public void mutatingOfEffectivelyFinalVariable_whenNotEquals_thenCorrect() {
final int[] total = new int[1];
int[] total = new int[1]; final Runnable r = () -> total[0]++;
Runnable r = () -> total[0]++;
r.run(); r.run();
assertNotEquals(0, total[0]); assertNotEquals(0, total[0]);

View File

@ -6,10 +6,10 @@ import java.util.Collections;
import java.util.Comparator; import java.util.Comparator;
import java.util.List; import java.util.List;
import com.baeldung.java8.entity.Human;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import com.baeldung.java8.entity.Human;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import com.google.common.primitives.Ints; import com.google.common.primitives.Ints;