From 9543f1f067443d87b2d3923cbbed76b0dc26199e Mon Sep 17 00:00:00 2001 From: eugenp Date: Wed, 25 Dec 2013 14:22:15 +0200 Subject: [PATCH] lambda example --- .../.settings/org.eclipse.jdt.ui.prefs | 6 +- core-java-8/pom.xml | 221 +++++++++--------- .../org/baeldung/java/CoreJava8UnitTest.java | 15 -- .../org/baeldung/java/CoreJavaUnitTest.java | 52 ----- .../java8/Java8ComparatorUnitTest.java | 24 ++ .../java/org/baeldung/java8/entity/Human.java | 64 +++++ 6 files changed, 206 insertions(+), 176 deletions(-) delete mode 100644 core-java-8/src/test/java/org/baeldung/java/CoreJava8UnitTest.java delete mode 100644 core-java-8/src/test/java/org/baeldung/java/CoreJavaUnitTest.java create mode 100644 core-java-8/src/test/java/org/baeldung/java8/Java8ComparatorUnitTest.java create mode 100644 core-java-8/src/test/java/org/baeldung/java8/entity/Human.java diff --git a/core-java-8/.settings/org.eclipse.jdt.ui.prefs b/core-java-8/.settings/org.eclipse.jdt.ui.prefs index 471e9b0d81..d84d2a7f8c 100644 --- a/core-java-8/.settings/org.eclipse.jdt.ui.prefs +++ b/core-java-8/.settings/org.eclipse.jdt.ui.prefs @@ -1,4 +1,3 @@ -#Sat Jan 21 23:04:06 EET 2012 eclipse.preferences.version=1 editor_save_participant_org.eclipse.jdt.ui.postsavelistener.cleanup=true sp_cleanup.add_default_serial_version_id=true @@ -14,6 +13,7 @@ sp_cleanup.always_use_blocks=true sp_cleanup.always_use_parentheses_in_expressions=true sp_cleanup.always_use_this_for_non_static_field_access=false sp_cleanup.always_use_this_for_non_static_method_access=false +sp_cleanup.convert_functional_interfaces=false sp_cleanup.convert_to_enhanced_for_loop=true sp_cleanup.correct_indentation=true sp_cleanup.format_source_code=true @@ -46,8 +46,10 @@ sp_cleanup.remove_unused_private_methods=true sp_cleanup.remove_unused_private_types=true sp_cleanup.sort_members=false sp_cleanup.sort_members_all=false -sp_cleanup.use_blocks=false +sp_cleanup.use_anonymous_class_creation=false +sp_cleanup.use_blocks=true sp_cleanup.use_blocks_only_for_return_and_throw=false +sp_cleanup.use_lambda=false sp_cleanup.use_parentheses_in_expressions=false sp_cleanup.use_this_for_non_static_field_access=true sp_cleanup.use_this_for_non_static_field_access_only_if_necessary=true diff --git a/core-java-8/pom.xml b/core-java-8/pom.xml index 782e9a7da9..cd690f1e71 100644 --- a/core-java-8/pom.xml +++ b/core-java-8/pom.xml @@ -1,139 +1,146 @@ - - 4.0.0 - org.baeldung - spring-rest - 0.1-SNAPSHOT + + 4.0.0 + org.baeldung + spring-rest + 0.1-SNAPSHOT - spring-rest + spring-rest - + - + - - com.google.guava - guava - 15.0 - + + com.google.guava + guava + 16.0-rc1 + - - org.apache.commons - commons-collections4 - 4.0 - + + org.apache.commons + commons-collections4 + 4.0 + - + + org.apache.commons + commons-lang3 + 3.1 + - + - - com.fasterxml.jackson.core - jackson-databind - ${jackson.version} - + - + + com.fasterxml.jackson.core + jackson-databind + ${jackson.version} + - - junit - junit-dep - ${junit.version} - test - + - - org.hamcrest - hamcrest-core - ${org.hamcrest.version} - test - - - org.hamcrest - hamcrest-library - ${org.hamcrest.version} - test - + + junit + junit-dep + ${junit.version} + test + - - org.mockito - mockito-core - ${mockito.version} - test - + + org.hamcrest + hamcrest-core + ${org.hamcrest.version} + test + + + org.hamcrest + hamcrest-library + ${org.hamcrest.version} + test + - + + org.mockito + mockito-core + ${mockito.version} + test + - - core-java-8 - - - src/main/resources - true - - + - + + core-java-8 + + + src/main/resources + true + + - - org.apache.maven.plugins - maven-compiler-plugin - ${maven-compiler-plugin.version} - - 1.7 - 1.7 - - + - - org.apache.maven.plugins - maven-surefire-plugin - ${maven-surefire-plugin.version} - + + org.apache.maven.plugins + maven-compiler-plugin + ${maven-compiler-plugin.version} + + 1.7 + 1.7 + + - + + org.apache.maven.plugins + maven-surefire-plugin + ${maven-surefire-plugin.version} + - + - - - 4.0.0.RELEASE - 3.2.0.RELEASE + - - 4.3.0.Final - 5.1.27 + + + 4.0.0.RELEASE + 3.2.0.RELEASE - - 2.3.0 + + 4.3.0.Final + 5.1.27 - - 1.7.5 - 1.0.11 + + 2.3.0 - - 5.0.1.Final + + 1.7.5 + 1.0.11 - - 15.0 - 3.1 + + 5.0.1.Final - - 1.3 - 4.11 - 1.9.5 + + 15.0 + 3.1 - 4.3 - 4.3.1 + + 1.3 + 4.11 + 1.9.5 - 2.1.0 + 4.3 + 4.3.1 - - 3.1 - 2.4 - 2.16 - 2.6 - 1.4.5 + 2.1.0 - + + 3.1 + 2.4 + 2.16 + 2.6 + 1.4.5 + + \ No newline at end of file diff --git a/core-java-8/src/test/java/org/baeldung/java/CoreJava8UnitTest.java b/core-java-8/src/test/java/org/baeldung/java/CoreJava8UnitTest.java deleted file mode 100644 index 9e7c00b4eb..0000000000 --- a/core-java-8/src/test/java/org/baeldung/java/CoreJava8UnitTest.java +++ /dev/null @@ -1,15 +0,0 @@ -package org.baeldung.java; - -import org.junit.Test; - -public class CoreJava8UnitTest { - - // tests - - - @Test - public final void when_thenCorrect() { - final Runnable r2 = () -> System.out.println("Hello world two!"); - r2.run(); - } - -} diff --git a/core-java-8/src/test/java/org/baeldung/java/CoreJavaUnitTest.java b/core-java-8/src/test/java/org/baeldung/java/CoreJavaUnitTest.java deleted file mode 100644 index fb6e39f768..0000000000 --- a/core-java-8/src/test/java/org/baeldung/java/CoreJavaUnitTest.java +++ /dev/null @@ -1,52 +0,0 @@ -package org.baeldung.java; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.List; - -import org.apache.commons.collections4.ListUtils; -import org.junit.Test; - -import com.google.common.collect.ImmutableList; - -public class CoreJavaUnitTest { - - // tests - - - @Test - public final void givenUsingTheJdk_whenArrayListIsSynchronized_thenCorrect() { - final List list = new ArrayList(Arrays.asList("one", "two", "three")); - final List synchronizedList = Collections.synchronizedList(list); - System.out.println("Synchronized List is: " + synchronizedList); - } - - @Test(expected = UnsupportedOperationException.class) - public final void givenUsingTheJdk_whenUnmodifiableListIsCreatedFromOriginal_thenNoLongerModifiable() { - final List list = new ArrayList(Arrays.asList("one", "two", "three")); - final List unmodifiableList = Collections.unmodifiableList(list); - unmodifiableList.add("four"); - } - - @Test(expected = UnsupportedOperationException.class) - public final void givenUsingGuava_whenUnmodifiableListIsCreatedFromOriginal_thenNoLongerModifiable() { - final List list = new ArrayList(Arrays.asList("one", "two", "three")); - final List unmodifiableList = ImmutableList.copyOf(list); - unmodifiableList.add("four"); - } - - @Test(expected = UnsupportedOperationException.class) - public final void givenUsingGuavaBuilder_whenUnmodifiableListIsCreatedFromOriginal_thenNoLongerModifiable() { - final List list = new ArrayList(Arrays.asList("one", "two", "three")); - final ImmutableList unmodifiableList = ImmutableList.builder().addAll(list).build(); - unmodifiableList.add("four"); - } - - @Test(expected = UnsupportedOperationException.class) - public final void givenUsingCommonsCollections_whenUnmodifiableListIsCreatedFromOriginal_thenNoLongerModifiable() { - final List list = new ArrayList(Arrays.asList("one", "two", "three")); - final List unmodifiableList = ListUtils.unmodifiableList(list); - unmodifiableList.add("four"); - } - -} diff --git a/core-java-8/src/test/java/org/baeldung/java8/Java8ComparatorUnitTest.java b/core-java-8/src/test/java/org/baeldung/java8/Java8ComparatorUnitTest.java new file mode 100644 index 0000000000..8a2a2cd3d8 --- /dev/null +++ b/core-java-8/src/test/java/org/baeldung/java8/Java8ComparatorUnitTest.java @@ -0,0 +1,24 @@ +package org.baeldung.java8; + +import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic; + +import java.util.Collections; +import java.util.List; + +import org.baeldung.java8.entity.Human; +import org.junit.Test; + +import com.google.common.collect.Lists; + +public class Java8ComparatorUnitTest { + + // tests - + + @Test + public final void when_thenCorrect() { + final List humans = Lists.newArrayList(new Human(randomAlphabetic(5)), new Human(randomAlphabetic(5))); + Collections.sort(humans, (final Human h1, final Human h2) -> h1.getName().compareTo(h2.getName())); + System.out.println(humans); + } + +} diff --git a/core-java-8/src/test/java/org/baeldung/java8/entity/Human.java b/core-java-8/src/test/java/org/baeldung/java8/entity/Human.java new file mode 100644 index 0000000000..e5d96262bf --- /dev/null +++ b/core-java-8/src/test/java/org/baeldung/java8/entity/Human.java @@ -0,0 +1,64 @@ +package org.baeldung.java8.entity; + +public class Human { + private String name; + + public Human() { + super(); + } + + public Human(final String name) { + super(); + + this.name = name; + } + + // API + + public String getName() { + return name; + } + + public void setName(final String name) { + this.name = name; + } + + // + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((name == null) ? 0 : name.hashCode()); + return result; + } + + @Override + public boolean equals(final Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + final Human other = (Human) obj; + if (name == null) { + if (other.name != null) { + return false; + } + } else if (!name.equals(other.name)) { + return false; + } + return true; + } + + @Override + public String toString() { + final StringBuilder builder = new StringBuilder(); + builder.append("Human [name=").append(name).append("]"); + return builder.toString(); + } + +}