Merge pull request #6569 from alimate/BAEL-2786

Teeing Collector
This commit is contained in:
Loredana Crusoveanu 2019-03-23 12:23:29 +02:00 committed by GitHub
commit a34d4f0171
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 126 additions and 0 deletions

48
core-java-12/pom.xml Normal file
View File

@ -0,0 +1,48 @@
<?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.baeldung</groupId>
<artifactId>core-java-12</artifactId>
<version>0.1.0-SNAPSHOT</version>
<name>core-java-12</name>
<packaging>jar</packaging>
<url>http://maven.apache.org</url>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<source>${maven.compiler.source.version}</source>
<target>${maven.compiler.target.version}</target>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<maven.compiler.source.version>12</maven.compiler.source.version>
<maven.compiler.target.version>12</maven.compiler.target.version>
<assertj.version>3.6.1</assertj.version>
</properties>
</project>

View File

@ -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<Integer> 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 +
'}';
}
}
}

View File

@ -377,6 +377,7 @@
<module>core-groovy</module>
<!-- <module>core-java-10</module> --> <!-- We haven't upgraded to java 10. Fixing in BAEL-10841 -->
<!-- <module>core-java-11</module> --> <!-- We haven't upgraded to java 11. Fixing in BAEL-10841 -->
<!-- <module>core-java-12</module> --> <!-- We haven't upgraded to java 12. Fixing in BAEL-10841 -->
<module>core-java-8</module>
<!--<module>core-java-9</module> --> <!-- We haven't upgraded to java 9. Fixing in BAEL-10841 -->
<!--<module>core-java-os</module> --> <!-- We haven't upgraded to java 9.-->