Last active
October 13, 2015 15:20
-
-
Save mikaelhg/4216018 to your computer and use it in GitHub Desktop.
Testing Java 8 lambdas
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package lambda; | |
import java.util.*; | |
import java.util.stream.Collectors; | |
public class LambdaDemo { | |
/** | |
* http://en.wikipedia.org/wiki/List_of_The_Wire_characters | |
*/ | |
public static class TheWireCharacter { | |
public final String name; | |
public final Set<Integer> seasons; | |
public TheWireCharacter(final String name, final Integer ... seasons) { | |
this.name = name; | |
this.seasons = Collections.unmodifiableSet(new TreeSet<>(Arrays.asList(seasons))); | |
} | |
} | |
private static final List<TheWireCharacter> CHARACTERS = Collections.unmodifiableList(Arrays.asList( | |
new TheWireCharacter("Jimmy McNulty", 1, 2, 3, 4, 5), | |
new TheWireCharacter("Lester Freamon", 2, 3, 4, 5), | |
new TheWireCharacter("Stringer Bell", 1, 2, 3), | |
new TheWireCharacter("Prez", 3, 4), | |
new TheWireCharacter("Omar Little", 3, 4, 5), | |
new TheWireCharacter("Chris Partlow", 5), | |
new TheWireCharacter("Frank Sobotka", 2), | |
new TheWireCharacter("D'Angelo Barksdale", 1, 2), | |
new TheWireCharacter("Avon Barksdale", 1, 2, 3) | |
)); | |
public static void main(final String ... args) { | |
final List<String> docks = CHARACTERS.stream() | |
.filter(c -> c.seasons.contains(2)) | |
.sorted((a, b) -> a.seasons.size() - b.seasons.size()) | |
.<String>map(c -> c.name) | |
.collect(Collectors.<String>toList()); | |
System.out.println("Characters in the Baltimore docks-centered season: " + docks); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package lambda; | |
import com.google.common.base.Function; | |
import com.google.common.base.Predicate; | |
import com.google.common.collect.FluentIterable; | |
import com.google.common.collect.ImmutableList; | |
import com.google.common.collect.ImmutableSet; | |
import java.util.*; | |
public class GuavaDemo { | |
/** | |
* http://en.wikipedia.org/wiki/List_of_The_Wire_characters | |
*/ | |
public static class TheWireCharacter { | |
public final String name; | |
public final ImmutableSet<Integer> seasons; | |
public TheWireCharacter(final String name, final Integer ... seasons) { | |
this.name = name; | |
this.seasons = ImmutableSet.copyOf(seasons); | |
} | |
public static final Function<TheWireCharacter, String> GET_NAME = new Function<TheWireCharacter, String>() { | |
@Override public String apply(final TheWireCharacter input) { | |
return input.name; | |
} | |
}; | |
} | |
public static final ImmutableList<TheWireCharacter> CHARACTERS = ImmutableList.of( | |
new TheWireCharacter("Jimmy McNulty", 1, 2, 3, 4, 5), | |
new TheWireCharacter("Lester Freamon", 2, 3, 4, 5), | |
new TheWireCharacter("Stringer Bell", 1, 2, 3), | |
new TheWireCharacter("Prez", 3, 4), | |
new TheWireCharacter("Omar Little", 3, 4, 5), | |
new TheWireCharacter("Chris Partlow", 5), | |
new TheWireCharacter("Frank Sobotka", 2), | |
new TheWireCharacter("D'Angelo Barksdale", 1, 2), | |
new TheWireCharacter("Avon Barksdale", 1, 2, 3) | |
); | |
private static Predicate<TheWireCharacter> inSeasons(final Integer season) { | |
return new Predicate<TheWireCharacter>() { | |
@Override public boolean apply(final TheWireCharacter c) { | |
return c.seasons.contains(season); | |
} | |
}; | |
} | |
private static final Comparator<TheWireCharacter> BY_SEASONS = new Comparator<TheWireCharacter>() { | |
@Override public int compare(final TheWireCharacter a, final TheWireCharacter b) { | |
return a.seasons.size() - b.seasons.size(); | |
} | |
}; | |
public static void main(final String ... args) { | |
final ImmutableList<TheWireCharacter> characters = FluentIterable.from(CHARACTERS) | |
.filter(inSeasons(2)) | |
.toSortedList(BY_SEASONS); | |
final ImmutableList<String> docks = FluentIterable.from(characters) | |
.transform(TheWireCharacter.GET_NAME) | |
.toList(); | |
System.out.println("Characters in the Baltimore docks-centered season: " + docks); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package lambda; | |
import static ch.lambdaj.Lambda.*; | |
import java.util.*; | |
public class LambdaJDemo { | |
/** | |
* http://en.wikipedia.org/wiki/List_of_The_Wire_characters | |
*/ | |
public static class TheWireCharacter { | |
public final String name; | |
public final Set<Integer> seasons; | |
public TheWireCharacter(final String name, final Integer ... seasons) { | |
this.name = name; | |
this.seasons = Collections.unmodifiableSet(new TreeSet<>(Arrays.asList(seasons))); | |
} | |
public Set<Integer> getSeasons() { | |
return seasons; | |
} | |
public String toString() { | |
return name; | |
} | |
} | |
private static final Comparator<TheWireCharacter> BY_SEASONS = new Comparator<TheWireCharacter>() { | |
@Override public int compare(final TheWireCharacter a, final TheWireCharacter b) { | |
return a.seasons.size() - b.seasons.size(); | |
} | |
}; | |
public static void main(final String ... args) { | |
final List<TheWireCharacter> characters = Arrays.asList( | |
new TheWireCharacter("Jimmy McNulty", 1, 2, 3, 4, 5), | |
new TheWireCharacter("Lester Freamon", 2, 3, 4, 5), | |
new TheWireCharacter("Stringer Bell", 1, 2, 3), | |
new TheWireCharacter("Prez", 3, 4), | |
new TheWireCharacter("Omar Little", 3, 4, 5), | |
new TheWireCharacter("Chris Partlow", 5), | |
new TheWireCharacter("Frank Sobotka", 2), | |
new TheWireCharacter("D'Angelo Barksdale", 1, 2), | |
new TheWireCharacter("Avon Barksdale", 1, 2, 3) | |
); | |
final List<TheWireCharacter> docks = sort(filter( | |
having(on(TheWireCharacter.class).getSeasons().contains(2)), characters), | |
on(TheWireCharacter.class).getSeasons().size()); | |
System.out.println("Characters in the Baltimore docks-centered season: " + docks); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package lambda; | |
import com.google.common.base.Function; | |
import com.google.common.base.Predicate; | |
import com.google.common.collect.FluentIterable; | |
import com.google.common.collect.ImmutableList; | |
import com.google.common.collect.ImmutableSet; | |
import java.util.*; | |
/** | |
* Demonstrating how a large project with many classes needing these | |
* functionalities might structure this kind of source code. | |
*/ | |
public class EnterpriseGuavaDemo { | |
public static final ImmutableList<TheWireCharacter> CHARACTERS = ImmutableList.of( | |
new TheWireCharacter("Jimmy McNulty", 1, 2, 3, 4, 5), | |
new TheWireCharacter("Lester Freamon", 2, 3, 4, 5), | |
new TheWireCharacter("Stringer Bell", 1, 2, 3), | |
new TheWireCharacter("Prez", 3, 4), | |
new TheWireCharacter("Omar Little", 3, 4, 5), | |
new TheWireCharacter("Chris Partlow", 5), | |
new TheWireCharacter("Frank Sobotka", 2), | |
new TheWireCharacter("D'Angelo Barksdale", 1, 2), | |
new TheWireCharacter("Avon Barksdale", 1, 2, 3) | |
); | |
/** | |
* http://en.wikipedia.org/wiki/List_of_The_Wire_characters | |
*/ | |
public static class TheWireCharacter { | |
public final String name; | |
public final ImmutableSet<Integer> seasons; | |
public TheWireCharacter(final String name, final Integer ... seasons) { | |
this.name = name; | |
this.seasons = ImmutableSet.copyOf(seasons); | |
} | |
public static class Filters { | |
public static Predicate<TheWireCharacter> inSeasons(final Integer season) { | |
return new Predicate<TheWireCharacter>() { | |
@Override public boolean apply(final TheWireCharacter c) { | |
return c.seasons.contains(season); | |
} | |
}; | |
} | |
public static Predicate<TheWireCharacter> nameContains(final String substring) { | |
return new Predicate<TheWireCharacter>() { | |
@Override public boolean apply(final TheWireCharacter c) { | |
return c.name.contains(substring); | |
} | |
}; | |
} | |
} | |
public static class Comparators { | |
public static final Comparator<TheWireCharacter> BY_SEASONS = new Comparator<TheWireCharacter>() { | |
@Override public int compare(final TheWireCharacter a, final TheWireCharacter b) { | |
return a.seasons.size() - b.seasons.size(); | |
} | |
}; | |
public static final Comparator<TheWireCharacter> BY_NAME = new Comparator<TheWireCharacter>() { | |
@Override public int compare(final TheWireCharacter a, final TheWireCharacter b) { | |
return a.name.compareTo(b.name); | |
} | |
}; | |
} | |
} | |
public static void main(final String ... args) { | |
final ImmutableList<TheWireCharacter> characters = FluentIterable.from(CHARACTERS) | |
.filter(TheWireCharacter.Filters.inSeasons(2)) | |
.toSortedList(TheWireCharacter.Comparators.BY_SEASONS); | |
final ImmutableList<String> docks = FluentIterable.from(characters) | |
.transform(new Function<TheWireCharacter, String>() { | |
@Override public String apply(final TheWireCharacter input) { | |
return input.name; | |
} | |
}) | |
.toList(); | |
System.out.println("Characters in the Baltimore docks-centered season: " + docks); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package lambda.groovy | |
import groovy.transform.* | |
@Immutable @TupleConstructor class TheWireCharacter { | |
final String name; | |
final Set<Integer> seasons; | |
} | |
CHARACTERS = [ | |
new TheWireCharacter("Jimmy McNulty", [1, 2, 3, 4, 5] as Set), | |
new TheWireCharacter("Lester Freamon", [2, 3, 4, 5] as Set), | |
new TheWireCharacter("Stringer Bell", [1, 2, 3] as Set), | |
new TheWireCharacter("Prez", [3, 4] as Set), | |
new TheWireCharacter("Omar Little", [3, 4, 5] as Set), | |
new TheWireCharacter("Chris Partlow", [5] as Set), | |
new TheWireCharacter("Frank Sobotka", [2] as Set), | |
new TheWireCharacter("D'Angelo Barksdale", [1, 2] as Set), | |
new TheWireCharacter("Avon Barksdale", [1, 2, 3] as Set) | |
].asImmutable() | |
docks = CHARACTERS.findAll { it.seasons.contains(2) } | |
.sort { it.seasons.size() } | |
.collect { it.name } | |
println("Characters in the Baltimore docks-centered season: " + docks) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package lambda.kotlin | |
data class TheWireCharacter(val name : String, vararg val seasons : Int) | |
private val CHARACTERS = array( | |
TheWireCharacter("Jimmy McNulty", 1, 2, 3, 4, 5), | |
TheWireCharacter("Lester Freamon", 2, 3, 4, 5), | |
TheWireCharacter("Stringer Bell", 1, 2, 3), | |
TheWireCharacter("Prez", 3, 4), | |
TheWireCharacter("Omar Little", 3, 4, 5), | |
TheWireCharacter("Chris Partlow", 5), | |
TheWireCharacter("Frank Sobotka", 2), | |
TheWireCharacter("D'Angelo Barksdale", 1, 2), | |
TheWireCharacter("Avon Barksdale", 1, 2, 3) | |
) | |
fun main(args : Array<String>) { | |
val docks = CHARACTERS.filter { it.seasons.any { it == 2 } } | |
.sortBy { it.seasons.size } | |
.map { it.name } | |
println("Characters in the Baltimore docks-centered season: " + docks) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<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"> | |
<modelVersion>4.0.0</modelVersion> | |
<groupId>com.gueck</groupId> | |
<artifactId>lambda-demo</artifactId> | |
<version>1.0-SNAPSHOT</version> | |
<properties> | |
<kotlin.version>0.5.162</kotlin.version> | |
<gmavenVersion>1.5</gmavenVersion> | |
<gmavenProviderSelection>2.0</gmavenProviderSelection> | |
<groovyVersion>2.1.0</groovyVersion> | |
</properties> | |
<repositories> | |
<repository> | |
<id>jetbrains-all</id> | |
<url>http://repository.jetbrains.com/all</url> | |
</repository> | |
</repositories> | |
<pluginRepositories> | |
<pluginRepository> | |
<id>jetbrains-all</id> | |
<url>http://repository.jetbrains.com/all</url> | |
</pluginRepository> | |
</pluginRepositories> | |
<dependencies> | |
<dependency> | |
<groupId>com.google.guava</groupId> | |
<artifactId>guava</artifactId> | |
<version>14.0</version> | |
</dependency> | |
<dependency> | |
<groupId>com.googlecode.lambdaj</groupId> | |
<artifactId>lambdaj</artifactId> | |
<version>2.3.3</version> | |
</dependency> | |
<dependency> | |
<groupId>org.jetbrains.kotlin</groupId> | |
<artifactId>kotlin-stdlib</artifactId> | |
<version>${kotlin.version}</version> | |
</dependency> | |
<dependency> | |
<groupId>org.codehaus.groovy</groupId> | |
<artifactId>groovy-all</artifactId> | |
<version>${groovyVersion}</version> | |
</dependency> | |
<dependency> | |
<groupId>org.mockito</groupId> | |
<artifactId>mockito-all</artifactId> | |
<version>1.9.5</version> | |
<scope>test</scope> | |
</dependency> | |
<dependency> | |
<groupId>junit</groupId> | |
<artifactId>junit</artifactId> | |
<version>4.11</version> | |
<scope>test</scope> | |
</dependency> | |
</dependencies> | |
<build> | |
<plugins> | |
<plugin> | |
<groupId>org.apache.maven.plugins</groupId> | |
<artifactId>maven-compiler-plugin</artifactId> | |
<version>3.0</version> | |
<configuration> | |
<source>8</source> | |
<target>8</target> | |
</configuration> | |
</plugin> | |
<plugin> | |
<artifactId>kotlin-maven-plugin</artifactId> | |
<groupId>org.jetbrains.kotlin</groupId> | |
<version>${kotlin.version}</version> | |
<executions> | |
<execution> | |
<id>compile</id> | |
<phase>process-sources</phase> | |
<goals> <goal>compile</goal> </goals> | |
</execution> | |
<execution> | |
<id>test-compile</id> | |
<phase>process-test-sources</phase> | |
<goals> <goal>test-compile</goal> </goals> | |
</execution> | |
</executions> | |
</plugin> | |
<plugin> | |
<groupId>org.codehaus.gmaven</groupId> | |
<artifactId>gmaven-plugin</artifactId> | |
<version>${gmavenVersion}</version> | |
<configuration> | |
<providerSelection>${gmavenProviderSelection}</providerSelection> | |
<sourceEncoding>UTF-8</sourceEncoding> | |
<source/> | |
</configuration> | |
<executions> | |
<execution> | |
<goals> | |
<goal>generateStubs</goal> | |
<goal>compile</goal> | |
<goal>generateTestStubs</goal> | |
<goal>testCompile</goal> | |
</goals> | |
</execution> | |
</executions> | |
<dependencies> | |
<dependency> | |
<groupId>org.codehaus.groovy</groupId> | |
<artifactId>groovy-all</artifactId> | |
<version>${groovyVersion}</version> | |
</dependency> | |
</dependencies> | |
</plugin> | |
</plugins> | |
</build> | |
</project> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment