Bael 1043/protonpack (#2415)

* BAEL-1043/ Introduction to protonpack

* Added test cases for windowed() api

* Resolving merge issues
This commit is contained in:
Devendra Tiwari 2017-09-04 22:34:18 +05:30 committed by maibin
parent 133bbe1aca
commit 1afa7e2fcf
5 changed files with 511 additions and 107 deletions

View File

@ -1,111 +1,110 @@
<?xml version="1.0" encoding="UTF-8"?> <?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" <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"> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent> <parent>
<artifactId>parent-modules</artifactId> <artifactId>parent-modules</artifactId>
<groupId>com.baeldung</groupId> <groupId>com.baeldung</groupId>
<version>1.0.0-SNAPSHOT</version> <version>1.0.0-SNAPSHOT</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<artifactId>libraries</artifactId> <artifactId>libraries</artifactId>
<name>libraries</name> <name>libraries</name>
<build> <build>
<plugins> <plugins>
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId> <artifactId>maven-dependency-plugin</artifactId>
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>org.apache.felix</groupId> <groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId> <artifactId>maven-bundle-plugin</artifactId>
<version>3.3.0</version> <version>3.3.0</version>
<type>maven-plugin</type> <type>maven-plugin</type>
</dependency> </dependency>
</dependencies> </dependencies>
<extensions>true</extensions> <extensions>true</extensions>
</plugin> </plugin>
<plugin> <plugin>
<artifactId>maven-failsafe-plugin</artifactId> <artifactId>maven-failsafe-plugin</artifactId>
<version>2.20</version> <version>2.20</version>
<configuration> <configuration>
<systemProperties> <systemProperties>
<webdriver.chrome.driver>chromedriver</webdriver.chrome.driver> <webdriver.chrome.driver>chromedriver</webdriver.chrome.driver>
</systemProperties> </systemProperties>
</configuration> </configuration>
</plugin> </plugin>
<plugin> <plugin>
<groupId>net.serenity-bdd.maven.plugins</groupId> <groupId>net.serenity-bdd.maven.plugins</groupId>
<artifactId>serenity-maven-plugin</artifactId> <artifactId>serenity-maven-plugin</artifactId>
<version>${serenity.plugin.version}</version> <version>${serenity.plugin.version}</version>
<executions> <executions>
<execution> <execution>
<id>serenity-reports</id> <id>serenity-reports</id>
<phase>post-integration-test</phase> <phase>post-integration-test</phase>
<goals> <goals>
<goal>aggregate</goal> <goal>aggregate</goal>
</goals> </goals>
</execution> </execution>
</executions> </executions>
</plugin> </plugin>
<!-- JDO Plugin --> <!-- JDO Plugin -->
<plugin> <plugin>
<groupId>org.datanucleus</groupId> <groupId>org.datanucleus</groupId>
<artifactId>datanucleus-maven-plugin</artifactId> <artifactId>datanucleus-maven-plugin</artifactId>
<version>5.0.2</version> <version>5.0.2</version>
<configuration> <configuration>
<api>JDO</api> <api>JDO</api>
<props>${basedir}/datanucleus.properties</props> <props>${basedir}/datanucleus.properties</props>
<log4jConfiguration>${basedir}/log4j.properties</log4jConfiguration> <log4jConfiguration>${basedir}/log4j.properties</log4jConfiguration>
<verbose>true</verbose> <verbose>true</verbose>
<fork>false</fork> <fork>false</fork>
<!-- Solve windows line too long error --> <!-- Solve windows line too long error -->
</configuration> </configuration>
<executions> <executions>
<execution> <execution>
<phase>process-classes</phase> <phase>process-classes</phase>
<goals> <goals>
<goal>enhance</goal> <goal>enhance</goal>
</goals> </goals>
</execution> </execution>
</executions> </executions>
</plugin> </plugin>
<!-- Neuroph --> <!-- Neuroph -->
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId> <artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version> <version>3.0.2</version>
<configuration> <configuration>
<excludes> <excludes>
<exclude>**/log4j.properties</exclude> <exclude>**/log4j.properties</exclude>
</excludes> </excludes>
<archive> <archive>
<manifest> <manifest>
<mainClass>com.baeldung.neuroph.NeurophXOR</mainClass> <mainClass>com.baeldung.neuroph.NeurophXOR</mainClass>
</manifest> </manifest>
</archive> </archive>
</configuration> </configuration>
</plugin> </plugin>
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId> <artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version> <version>2.18.1</version>
<executions> <executions>
<execution> <execution>
<id>test</id> <id>test</id>
<phase>test</phase> <phase>test</phase>
<goals> <goals>
<goal>test</goal> <goal>test</goal>
</goals> </goals>
<configuration> <configuration>
<includes> <includes>
<include>test/java/com/baeldung/neuroph/XORTest.java</include> <include>test/java/com/baeldung/neuroph/XORTest.java</include>
</includes> </includes>
</configuration> </configuration>
</execution> </execution>
</executions> </executions>
</plugin> </plugin>
<!-- /Neuroph --> <!-- /Neuroph -->
</plugins> </plugins>
</build> </build>
<dependencies> <dependencies>
@ -534,7 +533,12 @@
<artifactId>jdeferred-core</artifactId> <artifactId>jdeferred-core</artifactId>
<version>1.2.6</version> <version>1.2.6</version>
</dependency> </dependency>
</dependencies> <dependency>
<groupId>com.codepoetics</groupId>
<artifactId>protonpack</artifactId>
<version>${protonpack.version}</version>
</dependency>
</dependencies>
<repositories> <repositories>
<repository> <repository>
<id>maven2-repository.dev.java.net</id> <id>maven2-repository.dev.java.net</id>
@ -610,5 +614,6 @@
<geotools.version>15.2</geotools.version> <geotools.version>15.2</geotools.version>
<joda-time.version>2.9.9</joda-time.version> <joda-time.version>2.9.9</joda-time.version>
<hirondelle-date4j.version>1.5.1</hirondelle-date4j.version> <hirondelle-date4j.version>1.5.1</hirondelle-date4j.version>
<protonpack.version>1.14</protonpack.version>
</properties> </properties>
</project> </project>

View File

@ -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));
}
}

View File

@ -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));
}
}

View File

@ -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());
}
}

View File

@ -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)));
}
}