Bael 1043/protonpack (#2415)
* BAEL-1043/ Introduction to protonpack * Added test cases for windowed() api * Resolving merge issues
This commit is contained in:
parent
133bbe1aca
commit
1afa7e2fcf
|
@ -1,111 +1,110 @@
|
|||
<?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">
|
||||
<parent>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>libraries</artifactId>
|
||||
<name>libraries</name>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-dependency-plugin</artifactId>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.apache.felix</groupId>
|
||||
<artifactId>maven-bundle-plugin</artifactId>
|
||||
<version>3.3.0</version>
|
||||
<type>maven-plugin</type>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<extensions>true</extensions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-failsafe-plugin</artifactId>
|
||||
<version>2.20</version>
|
||||
<configuration>
|
||||
<systemProperties>
|
||||
<webdriver.chrome.driver>chromedriver</webdriver.chrome.driver>
|
||||
</systemProperties>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>net.serenity-bdd.maven.plugins</groupId>
|
||||
<artifactId>serenity-maven-plugin</artifactId>
|
||||
<version>${serenity.plugin.version}</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>serenity-reports</id>
|
||||
<phase>post-integration-test</phase>
|
||||
<goals>
|
||||
<goal>aggregate</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<!-- JDO Plugin -->
|
||||
<plugin>
|
||||
<groupId>org.datanucleus</groupId>
|
||||
<artifactId>datanucleus-maven-plugin</artifactId>
|
||||
<version>5.0.2</version>
|
||||
<configuration>
|
||||
<api>JDO</api>
|
||||
<props>${basedir}/datanucleus.properties</props>
|
||||
<log4jConfiguration>${basedir}/log4j.properties</log4jConfiguration>
|
||||
<verbose>true</verbose>
|
||||
<fork>false</fork>
|
||||
<!-- Solve windows line too long error -->
|
||||
</configuration>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>process-classes</phase>
|
||||
<goals>
|
||||
<goal>enhance</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<!-- Neuroph -->
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
<version>3.0.2</version>
|
||||
<configuration>
|
||||
<excludes>
|
||||
<exclude>**/log4j.properties</exclude>
|
||||
</excludes>
|
||||
<archive>
|
||||
<manifest>
|
||||
<mainClass>com.baeldung.neuroph.NeurophXOR</mainClass>
|
||||
</manifest>
|
||||
</archive>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>2.18.1</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>test</id>
|
||||
<phase>test</phase>
|
||||
<goals>
|
||||
<goal>test</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<includes>
|
||||
<include>test/java/com/baeldung/neuroph/XORTest.java</include>
|
||||
</includes>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<!-- /Neuroph -->
|
||||
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>libraries</artifactId>
|
||||
<name>libraries</name>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-dependency-plugin</artifactId>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.apache.felix</groupId>
|
||||
<artifactId>maven-bundle-plugin</artifactId>
|
||||
<version>3.3.0</version>
|
||||
<type>maven-plugin</type>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<extensions>true</extensions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-failsafe-plugin</artifactId>
|
||||
<version>2.20</version>
|
||||
<configuration>
|
||||
<systemProperties>
|
||||
<webdriver.chrome.driver>chromedriver</webdriver.chrome.driver>
|
||||
</systemProperties>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>net.serenity-bdd.maven.plugins</groupId>
|
||||
<artifactId>serenity-maven-plugin</artifactId>
|
||||
<version>${serenity.plugin.version}</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>serenity-reports</id>
|
||||
<phase>post-integration-test</phase>
|
||||
<goals>
|
||||
<goal>aggregate</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<!-- JDO Plugin -->
|
||||
<plugin>
|
||||
<groupId>org.datanucleus</groupId>
|
||||
<artifactId>datanucleus-maven-plugin</artifactId>
|
||||
<version>5.0.2</version>
|
||||
<configuration>
|
||||
<api>JDO</api>
|
||||
<props>${basedir}/datanucleus.properties</props>
|
||||
<log4jConfiguration>${basedir}/log4j.properties</log4jConfiguration>
|
||||
<verbose>true</verbose>
|
||||
<fork>false</fork>
|
||||
<!-- Solve windows line too long error -->
|
||||
</configuration>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>process-classes</phase>
|
||||
<goals>
|
||||
<goal>enhance</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<!-- Neuroph -->
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
<version>3.0.2</version>
|
||||
<configuration>
|
||||
<excludes>
|
||||
<exclude>**/log4j.properties</exclude>
|
||||
</excludes>
|
||||
<archive>
|
||||
<manifest>
|
||||
<mainClass>com.baeldung.neuroph.NeurophXOR</mainClass>
|
||||
</manifest>
|
||||
</archive>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>2.18.1</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>test</id>
|
||||
<phase>test</phase>
|
||||
<goals>
|
||||
<goal>test</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<includes>
|
||||
<include>test/java/com/baeldung/neuroph/XORTest.java</include>
|
||||
</includes>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<!-- /Neuroph -->
|
||||
</plugins>
|
||||
</build>
|
||||
<dependencies>
|
||||
|
@ -534,7 +533,12 @@
|
|||
<artifactId>jdeferred-core</artifactId>
|
||||
<version>1.2.6</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<dependency>
|
||||
<groupId>com.codepoetics</groupId>
|
||||
<artifactId>protonpack</artifactId>
|
||||
<version>${protonpack.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>maven2-repository.dev.java.net</id>
|
||||
|
@ -610,5 +614,6 @@
|
|||
<geotools.version>15.2</geotools.version>
|
||||
<joda-time.version>2.9.9</joda-time.version>
|
||||
<hirondelle-date4j.version>1.5.1</hirondelle-date4j.version>
|
||||
<protonpack.version>1.14</protonpack.version>
|
||||
</properties>
|
||||
</project>
|
||||
|
|
|
@ -0,0 +1,18 @@
|
|||
package com.baeldung.protonpack;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static com.codepoetics.protonpack.collectors.CollectorUtils.maxBy;
|
||||
import static com.codepoetics.protonpack.collectors.CollectorUtils.minBy;
|
||||
|
||||
public class CollectorUtilsExample {
|
||||
|
||||
public void minMaxProjectionCollector() {
|
||||
Stream<String> integerStream = Stream.of("a", "bb", "ccc", "1");
|
||||
|
||||
Optional<String> max = integerStream.collect(maxBy(String::length));
|
||||
Optional<String> min = integerStream.collect(minBy(String::length));
|
||||
|
||||
}
|
||||
}
|
|
@ -0,0 +1,107 @@
|
|||
package com.baeldung.protonpack;
|
||||
|
||||
import com.codepoetics.protonpack.Indexed;
|
||||
import com.codepoetics.protonpack.StreamUtils;
|
||||
import com.codepoetics.protonpack.selectors.Selectors;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.LongStream;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static java.util.stream.Collectors.toList;
|
||||
|
||||
public class StreamUtilsExample {
|
||||
|
||||
public void createInfiniteIndex() {
|
||||
LongStream indices = StreamUtils.indices();
|
||||
}
|
||||
|
||||
public void zipAStreamWithIndex() {
|
||||
Stream<String> source = Stream.of("Foo", "Bar", "Baz");
|
||||
List<Indexed<String>> zipped = StreamUtils
|
||||
.zipWithIndex(source)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public void zipAPairOfStreams() {
|
||||
Stream<String> streamA = Stream.of("A", "B", "C");
|
||||
Stream<String> streamB = Stream.of("Apple", "Banana", "Carrot");
|
||||
|
||||
List<String> zipped = StreamUtils
|
||||
.zip(streamA, streamB, (a, b) -> a + " is for " + b)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public void zipThreeStreams() {
|
||||
Stream<String> streamA = Stream.of("A", "B", "C");
|
||||
Stream<String> streamB = Stream.of("aggravating", "banausic", "complaisant");
|
||||
Stream<String> streamC = Stream.of("Apple", "Banana", "Carrot");
|
||||
|
||||
List<String> zipped = StreamUtils
|
||||
.zip(streamA, streamB, streamC, (a, b, c) -> a + " is for " + b + " " + c)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public void mergeThreeStreams() {
|
||||
Stream<String> streamA = Stream.of("A", "B", "C");
|
||||
Stream<String> streamB = Stream.of("apple", "banana", "carrot", "date");
|
||||
Stream<String> streamC = Stream.of("fritter", "split", "cake", "roll", "pastry");
|
||||
|
||||
Stream<List<String>> merged = StreamUtils.mergeToList(streamA, streamB, streamC);
|
||||
}
|
||||
|
||||
public void interleavingStreamsUsingRoundRobin() {
|
||||
Stream<String> streamA = Stream.of("Peter", "Paul", "Mary");
|
||||
Stream<String> streamB = Stream.of("A", "B", "C", "D", "E");
|
||||
Stream<String> streamC = Stream.of("foo", "bar", "baz", "xyzzy");
|
||||
|
||||
Stream<String> interleaved = StreamUtils.interleave(Selectors.roundRobin(), streamA, streamB, streamC);
|
||||
}
|
||||
|
||||
public void takeWhileAndTakeUntilStream() {
|
||||
Stream<Integer> infiniteInts = Stream.iterate(0, i -> i + 1);
|
||||
Stream<Integer> finiteIntsWhileLessThan10 = StreamUtils.takeWhile(infiniteInts, i -> i < 10);
|
||||
Stream<Integer> finiteIntsUntilGreaterThan10 = StreamUtils.takeUntil(infiniteInts, i -> i > 10);
|
||||
}
|
||||
|
||||
public void skipWhileAndSkipUntilStream() {
|
||||
Stream<Integer> ints = Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
|
||||
Stream<Integer> skippedWhileConditionMet = StreamUtils.skipWhile(ints, i -> i < 4);
|
||||
Stream<Integer> skippedUntilConditionMet = StreamUtils.skipWhile(ints, i -> i > 4);
|
||||
}
|
||||
|
||||
public void unfoldStream() {
|
||||
Stream<Integer> unfolded = StreamUtils.unfold(1, i -> (i < 10) ? Optional.of(i + 1) : Optional.empty());
|
||||
}
|
||||
|
||||
public void windowedStream() {
|
||||
Stream<Integer> integerStream = Stream.of(1, 2, 3, 4, 5);
|
||||
|
||||
List<List<Integer>> windows = StreamUtils
|
||||
.windowed(integerStream, 2)
|
||||
.collect(toList());
|
||||
List<List<Integer>> windowsWithSkipIndex = StreamUtils
|
||||
.windowed(integerStream, 3, 2)
|
||||
.collect(toList());
|
||||
List<List<Integer>> windowsWithSkipIndexAndAllowLowerSize = StreamUtils
|
||||
.windowed(integerStream, 2, 2, true)
|
||||
.collect(toList());
|
||||
|
||||
}
|
||||
|
||||
public void groupRunsStreams() {
|
||||
Stream<Integer> integerStream = Stream.of(1, 1, 2, 2, 3, 4, 5);
|
||||
|
||||
List<List<Integer>> runs = StreamUtils
|
||||
.groupRuns(integerStream)
|
||||
.collect(toList());
|
||||
}
|
||||
|
||||
public void aggreagateOnBiElementPredicate() {
|
||||
Stream<String> stream = Stream.of("a1", "b1", "b2", "c1");
|
||||
Stream<List<String>> aggregated = StreamUtils.aggregate(stream, (e1, e2) -> e1.charAt(0) == e2.charAt(0));
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,66 @@
|
|||
package com.baeldung.protonpack;
|
||||
|
||||
import com.codepoetics.protonpack.collectors.CollectorUtils;
|
||||
import com.codepoetics.protonpack.collectors.NonUniqueValueException;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static com.codepoetics.protonpack.collectors.CollectorUtils.maxBy;
|
||||
import static com.codepoetics.protonpack.collectors.CollectorUtils.minBy;
|
||||
import static org.hamcrest.CoreMatchers.equalTo;
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
|
||||
public class CollectorUtilsTests {
|
||||
|
||||
@Test
|
||||
public void maxByWithProjectionAndDefaultComparer() {
|
||||
Stream<String> integerStream = Stream.of("a", "bb", "ccc", "1");
|
||||
|
||||
Optional<String> max = integerStream.collect(maxBy(String::length));
|
||||
|
||||
assertThat(max.get(), is("ccc"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void minByWithProjectionAndDefaultComparer() {
|
||||
Stream<String> integerStream = Stream.of("abc", "bb", "ccc", "1");
|
||||
|
||||
Optional<String> max = integerStream.collect(minBy(String::length));
|
||||
|
||||
assertThat(max.get(), is("1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void returnsEmptyForEmptyStream() {
|
||||
assertThat(Stream
|
||||
.empty()
|
||||
.collect(CollectorUtils.unique()), equalTo(Optional.empty()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void returnsUniqueItem() {
|
||||
assertThat(Stream
|
||||
.of(1, 2, 3)
|
||||
.filter(i -> i > 2)
|
||||
.collect(CollectorUtils.unique()), equalTo(Optional.of(3)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void returnsUniqueNullableItem() {
|
||||
assertThat(Stream
|
||||
.of(1, 2, 3)
|
||||
.filter(i -> i > 2)
|
||||
.collect(CollectorUtils.uniqueNullable()), equalTo(3));
|
||||
}
|
||||
|
||||
@Test(expected = NonUniqueValueException.class)
|
||||
public void throwsExceptionIfItemIsNotUnique() {
|
||||
Stream
|
||||
.of(1, 2, 3)
|
||||
.filter(i -> i > 1)
|
||||
.collect(CollectorUtils.unique());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,208 @@
|
|||
package com.baeldung.protonpack;
|
||||
|
||||
import com.codepoetics.protonpack.Indexed;
|
||||
import com.codepoetics.protonpack.StreamUtils;
|
||||
import com.codepoetics.protonpack.selectors.Selectors;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.LongStream;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static java.util.Arrays.asList;
|
||||
import static java.util.stream.Collectors.maxBy;
|
||||
import static java.util.stream.Collectors.toList;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
|
||||
public class StreamUtilsTests {
|
||||
|
||||
@Test
|
||||
public void createInfiniteIndex() {
|
||||
LongStream indices = StreamUtils
|
||||
.indices()
|
||||
.limit(500);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void zipAStreamWithIndex() {
|
||||
Stream<String> source = Stream.of("Foo", "Bar", "Baz");
|
||||
|
||||
List<Indexed<String>> zipped = StreamUtils
|
||||
.zipWithIndex(source)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
assertThat(zipped, contains(Indexed.index(0, "Foo"), Indexed.index(1, "Bar"), Indexed.index(2, "Baz")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void zipAPairOfStreams() {
|
||||
Stream<String> streamA = Stream.of("A", "B", "C");
|
||||
Stream<String> streamB = Stream.of("Apple", "Banana", "Carrot");
|
||||
|
||||
List<String> zipped = StreamUtils
|
||||
.zip(streamA, streamB, (a, b) -> a + " is for " + b)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
assertThat(zipped, contains("A is for Apple", "B is for Banana", "C is for Carrot"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void zipThreeStreams() {
|
||||
Stream<String> streamA = Stream.of("A", "B", "C");
|
||||
Stream<String> streamB = Stream.of("aggravating", "banausic", "complaisant");
|
||||
Stream<String> streamC = Stream.of("Apple", "Banana", "Carrot");
|
||||
|
||||
List<String> zipped = StreamUtils
|
||||
.zip(streamA, streamB, streamC, (a, b, c) -> a + " is for " + b + " " + c)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
assertThat(zipped, contains("A is for aggravating Apple", "B is for banausic Banana", "C is for complaisant Carrot"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void mergeThreeStreams() {
|
||||
Stream<String> streamA = Stream.of("A", "B", "C");
|
||||
Stream<String> streamB = Stream.of("apple", "banana", "carrot", "date");
|
||||
Stream<String> streamC = Stream.of("fritter", "split", "cake", "roll", "pastry");
|
||||
|
||||
Stream<List<String>> merged = StreamUtils.mergeToList(streamA, streamB, streamC);
|
||||
|
||||
assertThat(merged.collect(toList()), contains(asList("A", "apple", "fritter"), asList("B", "banana", "split"), asList("C", "carrot", "cake"), asList("date", "roll"), asList("pastry")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void roundRobinInterleaving() {
|
||||
Stream<String> streamA = Stream.of("Peter", "Paul", "Mary");
|
||||
Stream<String> streamB = Stream.of("A", "B", "C", "D", "E");
|
||||
Stream<String> streamC = Stream.of("foo", "bar", "baz", "xyzzy");
|
||||
|
||||
Stream<String> interleaved = StreamUtils.interleave(Selectors.roundRobin(), streamA, streamB, streamC);
|
||||
|
||||
assertThat(interleaved.collect(Collectors.toList()), contains("Peter", "A", "foo", "Paul", "B", "bar", "Mary", "C", "baz", "D", "xyzzy", "E"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void takeWhileConditionIsMet() {
|
||||
Stream<Integer> infiniteInts = Stream.iterate(0, i -> i + 1);
|
||||
Stream<Integer> finiteInts = StreamUtils.takeWhile(infiniteInts, i -> i < 10);
|
||||
|
||||
assertThat(finiteInts.collect(Collectors.toList()), hasSize(10));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void takeUntilConditionIsNotMet() {
|
||||
Stream<Integer> infiniteInts = Stream.iterate(0, i -> i + 1);
|
||||
Stream<Integer> finiteInts = StreamUtils.takeUntil(infiniteInts, i -> i > 10);
|
||||
|
||||
assertThat(finiteInts.collect(Collectors.toList()), hasSize(11));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void skipWhileConditionMet() {
|
||||
Stream<Integer> ints = Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
|
||||
Stream<Integer> skipped = StreamUtils.skipWhile(ints, i -> i < 4);
|
||||
List<Integer> collected = skipped.collect(Collectors.toList());
|
||||
|
||||
assertThat(collected, contains(4, 5, 6, 7, 8, 9, 10));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void skipUntilConditionMet() {
|
||||
Stream<Integer> ints = Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
|
||||
Stream<Integer> skipped = StreamUtils.skipUntil(ints, i -> i > 4);
|
||||
List<Integer> collected = skipped.collect(Collectors.toList());
|
||||
|
||||
assertThat(collected, contains(5, 6, 7, 8, 9, 10));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void unfoldUntilEmptyIsReturned() {
|
||||
Stream<Integer> unfolded = StreamUtils.unfold(1, i -> (i < 10) ? Optional.of(i + 1) : Optional.empty());
|
||||
|
||||
assertThat(unfolded.collect(Collectors.toList()), contains(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void groupRunsStreamTest() {
|
||||
Stream<Integer> integerStream = Stream.of(1, 1, 2, 2, 3, 4, 5);
|
||||
List<List<Integer>> runs = StreamUtils
|
||||
.groupRuns(integerStream)
|
||||
.collect(toList());
|
||||
|
||||
assertThat(runs, contains(asList(1, 1), asList(2, 2), asList(3), asList(4), asList(5)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void aggreagateOnBiElementPredicate() {
|
||||
Stream<String> stream = Stream.of("a1", "b1", "b2", "c1");
|
||||
Stream<List<String>> aggregated = StreamUtils.aggregate(stream, (e1, e2) -> e1.charAt(0) == e2.charAt(0));
|
||||
assertThat(aggregated.collect(toList()), contains(asList("a1"), asList("b1", "b2"), asList("c1")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void windowingOnList() {
|
||||
Stream<Integer> integerStream = Stream.of(1, 2, 3, 4, 5);
|
||||
|
||||
List<List<Integer>> windows = StreamUtils
|
||||
.windowed(integerStream, 2)
|
||||
.collect(toList());
|
||||
|
||||
assertThat(windows, contains(asList(1, 2), asList(2, 3), asList(3, 4), asList(4, 5)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void windowingOnListTwoOverlap() {
|
||||
Stream<Integer> integerStream = Stream.of(1, 2, 3, 4, 5);
|
||||
|
||||
List<List<Integer>> windows = StreamUtils
|
||||
.windowed(integerStream, 3, 2)
|
||||
.collect(toList());
|
||||
|
||||
assertThat(windows, contains(asList(1, 2, 3), asList(3, 4, 5)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void windowingOnEmptyList() {
|
||||
ArrayList<Integer> ints = new ArrayList<>();
|
||||
|
||||
ints
|
||||
.stream()
|
||||
.collect(maxBy((a, b) -> a
|
||||
.toString()
|
||||
.compareTo(b.toString())));
|
||||
|
||||
List<List<Integer>> windows = StreamUtils
|
||||
.windowed(ints.stream(), 2)
|
||||
.collect(toList());
|
||||
|
||||
assertThat(windows, iterableWithSize(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void windowingOnListTwoOverlapAllowLesserSize() {
|
||||
Stream<Integer> integerStream = Stream.of(1, 2, 3, 4, 5);
|
||||
|
||||
List<List<Integer>> windows = StreamUtils
|
||||
.windowed(integerStream, 2, 2, true)
|
||||
.collect(toList());
|
||||
|
||||
assertThat(windows, contains(asList(1, 2), asList(3, 4), asList(5)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void windowingOnListOneOverlapAllowLesserSizeMultipleLesserWindows() {
|
||||
Stream<Integer> integerStream = Stream.of(1, 2, 3, 4, 5);
|
||||
|
||||
List<List<Integer>> windows = StreamUtils
|
||||
.windowed(integerStream, 3, 1, true)
|
||||
.collect(toList());
|
||||
|
||||
assertThat(windows, contains(asList(1, 2, 3), asList(2, 3, 4), asList(3, 4, 5), asList(4, 5), asList(5)));
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue