lambda example

This commit is contained in:
eugenp 2013-12-25 14:22:15 +02:00
parent 1a12a44cb1
commit 9543f1f067
6 changed files with 206 additions and 176 deletions

View File

@ -1,4 +1,3 @@
#Sat Jan 21 23:04:06 EET 2012
eclipse.preferences.version=1 eclipse.preferences.version=1
editor_save_participant_org.eclipse.jdt.ui.postsavelistener.cleanup=true editor_save_participant_org.eclipse.jdt.ui.postsavelistener.cleanup=true
sp_cleanup.add_default_serial_version_id=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_parentheses_in_expressions=true
sp_cleanup.always_use_this_for_non_static_field_access=false sp_cleanup.always_use_this_for_non_static_field_access=false
sp_cleanup.always_use_this_for_non_static_method_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.convert_to_enhanced_for_loop=true
sp_cleanup.correct_indentation=true sp_cleanup.correct_indentation=true
sp_cleanup.format_source_code=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.remove_unused_private_types=true
sp_cleanup.sort_members=false sp_cleanup.sort_members=false
sp_cleanup.sort_members_all=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_blocks_only_for_return_and_throw=false
sp_cleanup.use_lambda=false
sp_cleanup.use_parentheses_in_expressions=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=true
sp_cleanup.use_this_for_non_static_field_access_only_if_necessary=true sp_cleanup.use_this_for_non_static_field_access_only_if_necessary=true

View File

@ -1,139 +1,146 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<modelVersion>4.0.0</modelVersion> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<groupId>org.baeldung</groupId> <modelVersion>4.0.0</modelVersion>
<artifactId>spring-rest</artifactId> <groupId>org.baeldung</groupId>
<version>0.1-SNAPSHOT</version> <artifactId>spring-rest</artifactId>
<version>0.1-SNAPSHOT</version>
<name>spring-rest</name> <name>spring-rest</name>
<dependencies> <dependencies>
<!-- utils --> <!-- utils -->
<dependency> <dependency>
<groupId>com.google.guava</groupId> <groupId>com.google.guava</groupId>
<artifactId>guava</artifactId> <artifactId>guava</artifactId>
<version>15.0</version> <version>16.0-rc1</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.apache.commons</groupId> <groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId> <artifactId>commons-collections4</artifactId>
<version>4.0</version> <version>4.0</version>
</dependency> </dependency>
<!-- web --> <dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.1</version>
</dependency>
<!-- marshalling --> <!-- web -->
<dependency> <!-- marshalling -->
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<!-- test scoped --> <dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency> <!-- test scoped -->
<groupId>junit</groupId>
<artifactId>junit-dep</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency> <dependency>
<groupId>org.hamcrest</groupId> <groupId>junit</groupId>
<artifactId>hamcrest-core</artifactId> <artifactId>junit-dep</artifactId>
<version>${org.hamcrest.version}</version> <version>${junit.version}</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
<version>${org.hamcrest.version}</version>
<scope>test</scope>
</dependency>
<dependency> <dependency>
<groupId>org.mockito</groupId> <groupId>org.hamcrest</groupId>
<artifactId>mockito-core</artifactId> <artifactId>hamcrest-core</artifactId>
<version>${mockito.version}</version> <version>${org.hamcrest.version}</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
<version>${org.hamcrest.version}</version>
<scope>test</scope>
</dependency>
</dependencies> <dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
<build> </dependencies>
<finalName>core-java-8</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins> <build>
<finalName>core-java-8</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugin> <plugins>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId> <artifactId>maven-compiler-plugin</artifactId>
<version>${maven-surefire-plugin.version}</version> <version>${maven-compiler-plugin.version}</version>
</plugin> <configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins> <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven-surefire-plugin.version}</version>
</plugin>
</build> </plugins>
<properties> </build>
<!-- Spring -->
<org.springframework.version>4.0.0.RELEASE</org.springframework.version>
<org.springframework.security.version>3.2.0.RELEASE</org.springframework.security.version>
<!-- persistence --> <properties>
<hibernate.version>4.3.0.Final</hibernate.version> <!-- Spring -->
<mysql-connector-java.version>5.1.27</mysql-connector-java.version> <org.springframework.version>4.0.0.RELEASE</org.springframework.version>
<org.springframework.security.version>3.2.0.RELEASE</org.springframework.security.version>
<!-- marshalling --> <!-- persistence -->
<jackson.version>2.3.0</jackson.version> <hibernate.version>4.3.0.Final</hibernate.version>
<mysql-connector-java.version>5.1.27</mysql-connector-java.version>
<!-- logging --> <!-- marshalling -->
<org.slf4j.version>1.7.5</org.slf4j.version> <jackson.version>2.3.0</jackson.version>
<logback.version>1.0.11</logback.version>
<!-- various --> <!-- logging -->
<hibernate-validator.version>5.0.1.Final</hibernate-validator.version> <org.slf4j.version>1.7.5</org.slf4j.version>
<logback.version>1.0.11</logback.version>
<!-- util --> <!-- various -->
<guava.version>15.0</guava.version> <hibernate-validator.version>5.0.1.Final</hibernate-validator.version>
<commons-lang3.version>3.1</commons-lang3.version>
<!-- testing --> <!-- util -->
<org.hamcrest.version>1.3</org.hamcrest.version> <guava.version>15.0</guava.version>
<junit.version>4.11</junit.version> <commons-lang3.version>3.1</commons-lang3.version>
<mockito.version>1.9.5</mockito.version>
<httpcore.version>4.3</httpcore.version> <!-- testing -->
<httpclient.version>4.3.1</httpclient.version> <org.hamcrest.version>1.3</org.hamcrest.version>
<junit.version>4.11</junit.version>
<mockito.version>1.9.5</mockito.version>
<rest-assured.version>2.1.0</rest-assured.version> <httpcore.version>4.3</httpcore.version>
<httpclient.version>4.3.1</httpclient.version>
<!-- maven plugins --> <rest-assured.version>2.1.0</rest-assured.version>
<maven-compiler-plugin.version>3.1</maven-compiler-plugin.version>
<maven-war-plugin.version>2.4</maven-war-plugin.version>
<maven-surefire-plugin.version>2.16</maven-surefire-plugin.version>
<maven-resources-plugin.version>2.6</maven-resources-plugin.version>
<cargo-maven2-plugin.version>1.4.5</cargo-maven2-plugin.version>
</properties> <!-- maven plugins -->
<maven-compiler-plugin.version>3.1</maven-compiler-plugin.version>
<maven-war-plugin.version>2.4</maven-war-plugin.version>
<maven-surefire-plugin.version>2.16</maven-surefire-plugin.version>
<maven-resources-plugin.version>2.6</maven-resources-plugin.version>
<cargo-maven2-plugin.version>1.4.5</cargo-maven2-plugin.version>
</properties>
</project> </project>

View File

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

View File

@ -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<String> list = new ArrayList<String>(Arrays.asList("one", "two", "three"));
final List<String> synchronizedList = Collections.synchronizedList(list);
System.out.println("Synchronized List is: " + synchronizedList);
}
@Test(expected = UnsupportedOperationException.class)
public final void givenUsingTheJdk_whenUnmodifiableListIsCreatedFromOriginal_thenNoLongerModifiable() {
final List<String> list = new ArrayList<String>(Arrays.asList("one", "two", "three"));
final List<String> unmodifiableList = Collections.unmodifiableList(list);
unmodifiableList.add("four");
}
@Test(expected = UnsupportedOperationException.class)
public final void givenUsingGuava_whenUnmodifiableListIsCreatedFromOriginal_thenNoLongerModifiable() {
final List<String> list = new ArrayList<String>(Arrays.asList("one", "two", "three"));
final List<String> unmodifiableList = ImmutableList.copyOf(list);
unmodifiableList.add("four");
}
@Test(expected = UnsupportedOperationException.class)
public final void givenUsingGuavaBuilder_whenUnmodifiableListIsCreatedFromOriginal_thenNoLongerModifiable() {
final List<String> list = new ArrayList<String>(Arrays.asList("one", "two", "three"));
final ImmutableList<Object> unmodifiableList = ImmutableList.builder().addAll(list).build();
unmodifiableList.add("four");
}
@Test(expected = UnsupportedOperationException.class)
public final void givenUsingCommonsCollections_whenUnmodifiableListIsCreatedFromOriginal_thenNoLongerModifiable() {
final List<String> list = new ArrayList<String>(Arrays.asList("one", "two", "three"));
final List<String> unmodifiableList = ListUtils.unmodifiableList(list);
unmodifiableList.add("four");
}
}

View File

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

View File

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