From 18eb32a7dbf2405b30c59cd0ce36b171b6c74b6f Mon Sep 17 00:00:00 2001 From: Ali Dehghani Date: Fri, 22 Mar 2019 03:46:02 +0430 Subject: [PATCH] Introducing the core-java-12 module. It's ignored just like other 9+ versions. --- core-java-12/pom.xml | 48 ++++++++++++ .../collectors/CollectorsUnitTest.java | 77 +++++++++++++++++++ pom.xml | 1 + 3 files changed, 126 insertions(+) create mode 100644 core-java-12/pom.xml create mode 100644 core-java-12/src/test/java/com/baeldung/collectors/CollectorsUnitTest.java diff --git a/core-java-12/pom.xml b/core-java-12/pom.xml new file mode 100644 index 0000000000..729b29381b --- /dev/null +++ b/core-java-12/pom.xml @@ -0,0 +1,48 @@ + + + 4.0.0 + com.baeldung + core-java-12 + 0.1.0-SNAPSHOT + core-java-12 + jar + http://maven.apache.org + + + com.baeldung + parent-modules + 1.0.0-SNAPSHOT + + + + + org.assertj + assertj-core + ${assertj.version} + test + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + ${maven-compiler-plugin.version} + + ${maven.compiler.source.version} + ${maven.compiler.target.version} + + + + + + + 12 + 12 + 3.6.1 + + + \ No newline at end of file diff --git a/core-java-12/src/test/java/com/baeldung/collectors/CollectorsUnitTest.java b/core-java-12/src/test/java/com/baeldung/collectors/CollectorsUnitTest.java new file mode 100644 index 0000000000..68915b504d --- /dev/null +++ b/core-java-12/src/test/java/com/baeldung/collectors/CollectorsUnitTest.java @@ -0,0 +1,77 @@ +package com.baeldung.collectors; + +import java.util.Arrays; +import java.util.List; +import java.util.Objects; + +import org.junit.Test; + +import static java.util.stream.Collectors.maxBy; +import static java.util.stream.Collectors.minBy; +import static java.util.stream.Collectors.teeing; +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Unit tests for collectors additions in Java 12. + */ +public class CollectorsUnitTest { + + @Test + public void whenTeeing_ItShouldCombineTheResultsAsExpected() { + List numbers = Arrays.asList(42, 4, 2, 24); + Range range = numbers.stream() + .collect(teeing( + minBy(Integer::compareTo), + maxBy(Integer::compareTo), + (min, max) -> new Range(min.orElse(null), max.orElse(null)) + )); + + assertThat(range).isEqualTo(new Range(2, 42)); + } + + /** + * Represents a closed range of numbers between {@link #min} and + * {@link #max}, both inclusive. + */ + private static class Range { + + private final Integer min; + + private final Integer max; + + Range(Integer min, Integer max) { + this.min = min; + this.max = max; + } + + Integer getMin() { + return min; + } + + Integer getMax() { + return max; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Range range = (Range) o; + return Objects.equals(getMin(), range.getMin()) && + Objects.equals(getMax(), range.getMax()); + } + + @Override + public int hashCode() { + return Objects.hash(getMin(), getMax()); + } + + @Override + public String toString() { + return "Range{" + + "min=" + min + + ", max=" + max + + '}'; + } + } +} diff --git a/pom.xml b/pom.xml index bf225d82f0..d97c30ad64 100644 --- a/pom.xml +++ b/pom.xml @@ -377,6 +377,7 @@ core-groovy + core-java-8