commit
a5dfae0dc3
|
@ -1,48 +1,48 @@
|
||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
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">
|
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>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<groupId>com.baeldung</groupId>
|
<groupId>com.baeldung</groupId>
|
||||||
<artifactId>core-java-12</artifactId>
|
<artifactId>core-java-12</artifactId>
|
||||||
<version>0.1.0-SNAPSHOT</version>
|
<version>0.1.0-SNAPSHOT</version>
|
||||||
<name>core-java-12</name>
|
<name>core-java-12</name>
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
<url>http://maven.apache.org</url>
|
<url>http://maven.apache.org</url>
|
||||||
|
|
||||||
<parent>
|
<parent>
|
||||||
<groupId>com.baeldung</groupId>
|
<groupId>com.baeldung</groupId>
|
||||||
<artifactId>parent-modules</artifactId>
|
<artifactId>parent-modules</artifactId>
|
||||||
<version>1.0.0-SNAPSHOT</version>
|
<version>1.0.0-SNAPSHOT</version>
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.assertj</groupId>
|
<groupId>org.assertj</groupId>
|
||||||
<artifactId>assertj-core</artifactId>
|
<artifactId>assertj-core</artifactId>
|
||||||
<version>${assertj.version}</version>
|
<version>${assertj.version}</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
<plugins>
|
<plugins>
|
||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
<artifactId>maven-compiler-plugin</artifactId>
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
<version>${maven-compiler-plugin.version}</version>
|
<version>${maven-compiler-plugin.version}</version>
|
||||||
<configuration>
|
<configuration>
|
||||||
<source>${maven.compiler.source.version}</source>
|
<source>${maven.compiler.source.version}</source>
|
||||||
<target>${maven.compiler.target.version}</target>
|
<target>${maven.compiler.target.version}</target>
|
||||||
</configuration>
|
</configuration>
|
||||||
</plugin>
|
</plugin>
|
||||||
</plugins>
|
</plugins>
|
||||||
</build>
|
</build>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<maven.compiler.source.version>12</maven.compiler.source.version>
|
<maven.compiler.source.version>12</maven.compiler.source.version>
|
||||||
<maven.compiler.target.version>12</maven.compiler.target.version>
|
<maven.compiler.target.version>12</maven.compiler.target.version>
|
||||||
<assertj.version>3.6.1</assertj.version>
|
<assertj.version>3.6.1</assertj.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
</project>
|
</project>
|
|
@ -16,62 +16,56 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||||
*/
|
*/
|
||||||
public class CollectorsUnitTest {
|
public class CollectorsUnitTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenTeeing_ItShouldCombineTheResultsAsExpected() {
|
public void whenTeeing_ItShouldCombineTheResultsAsExpected() {
|
||||||
List<Integer> numbers = Arrays.asList(42, 4, 2, 24);
|
List<Integer> numbers = Arrays.asList(42, 4, 2, 24);
|
||||||
Range range = numbers.stream()
|
Range range = numbers.stream()
|
||||||
.collect(teeing(
|
.collect(teeing(minBy(Integer::compareTo), maxBy(Integer::compareTo), (min, max) -> new Range(min.orElse(null), max.orElse(null))));
|
||||||
minBy(Integer::compareTo),
|
|
||||||
maxBy(Integer::compareTo),
|
|
||||||
(min, max) -> new Range(min.orElse(null), max.orElse(null))
|
|
||||||
));
|
|
||||||
|
|
||||||
assertThat(range).isEqualTo(new Range(2, 42));
|
assertThat(range).isEqualTo(new Range(2, 42));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents a closed range of numbers between {@link #min} and
|
* Represents a closed range of numbers between {@link #min} and
|
||||||
* {@link #max}, both inclusive.
|
* {@link #max}, both inclusive.
|
||||||
*/
|
*/
|
||||||
private static class Range {
|
private static class Range {
|
||||||
|
|
||||||
private final Integer min;
|
private final Integer min;
|
||||||
|
|
||||||
private final Integer max;
|
private final Integer max;
|
||||||
|
|
||||||
Range(Integer min, Integer max) {
|
Range(Integer min, Integer max) {
|
||||||
this.min = min;
|
this.min = min;
|
||||||
this.max = max;
|
this.max = max;
|
||||||
}
|
}
|
||||||
|
|
||||||
Integer getMin() {
|
Integer getMin() {
|
||||||
return min;
|
return min;
|
||||||
}
|
}
|
||||||
|
|
||||||
Integer getMax() {
|
Integer getMax() {
|
||||||
return max;
|
return max;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object o) {
|
public boolean equals(Object o) {
|
||||||
if (this == o) return true;
|
if (this == o)
|
||||||
if (o == null || getClass() != o.getClass()) return false;
|
return true;
|
||||||
Range range = (Range) o;
|
if (o == null || getClass() != o.getClass())
|
||||||
return Objects.equals(getMin(), range.getMin()) &&
|
return false;
|
||||||
Objects.equals(getMax(), range.getMax());
|
Range range = (Range) o;
|
||||||
}
|
return Objects.equals(getMin(), range.getMin()) && Objects.equals(getMax(), range.getMax());
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int hashCode() {
|
public int hashCode() {
|
||||||
return Objects.hash(getMin(), getMax());
|
return Objects.hash(getMin(), getMax());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "Range{" +
|
return "Range{" + "min=" + min + ", max=" + max + '}';
|
||||||
"min=" + min +
|
}
|
||||||
", max=" + max +
|
}
|
||||||
'}';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue