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

@ -105,7 +105,6 @@
</executions> </executions>
</plugin> </plugin>
<!-- /Neuroph --> <!-- /Neuroph -->
</plugins> </plugins>
</build> </build>
<dependencies> <dependencies>
@ -534,6 +533,11 @@
<artifactId>jdeferred-core</artifactId> <artifactId>jdeferred-core</artifactId>
<version>1.2.6</version> <version>1.2.6</version>
</dependency> </dependency>
<dependency>
<groupId>com.codepoetics</groupId>
<artifactId>protonpack</artifactId>
<version>${protonpack.version}</version>
</dependency>
</dependencies> </dependencies>
<repositories> <repositories>
<repository> <repository>
@ -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)));
}
}