Skip to content

Commit

Permalink
Merge pull request #9 from furplag/2.x
Browse files Browse the repository at this point in the history
add Streamr#collect for Map .
  • Loading branch information
furplag authored Dec 3, 2018
2 parents a8a7437 + 7e23acd commit 96f7637
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 2 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>jp.furplag.sandbox</groupId>
<artifactId>relic</artifactId>
<version>2.1.0-SNAPSHOT</version>
<version>2.1.0</version>
<packaging>jar</packaging>
<inceptionYear>2018</inceptionYear>
<name>${project.artifactId}</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.function.Predicate;

import org.apache.commons.lang3.ArrayUtils;

Expand Down
17 changes: 17 additions & 0 deletions src/main/java/jp/furplag/sandbox/stream/Streamr.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,20 @@

import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.function.UnaryOperator;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.apache.commons.lang3.tuple.Pair;

import jp.furplag.function.ThrowablePredicate;

/**
Expand All @@ -48,6 +53,18 @@ static <T, R extends Collection<T>> R collect(final Stream<T> stream, final Supp
return stream(stream).collect(Collectors.toCollection(supplier));
}

/**
* we might to use this many .
*
* @param <T> the type of the key of stream elements
* @param <U> the type of the value of stream elements
* @param entries {@link Stream} of {@link Pair}, maybe null
* @return {@link Map}
*/
static <T, U> Map<T, U> collect(final Stream<Pair<T, U>> entries, BinaryOperator<U> mergeFunction, final Supplier<Map<T, U>> supplier) {
return Streamr.stream(entries).collect(Collectors.toMap(Pair::getLeft, Pair::getRight, Objects.requireNonNullElse(mergeFunction, (current, next) -> next), Objects.requireNonNullElse(supplier, HashMap::new)));
}

/**
* do less coding in case of {@link Collection#stream()} .
*
Expand Down
15 changes: 15 additions & 0 deletions src/test/java/jp/furplag/sandbox/stream/StreamrTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,21 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Deque;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Objects;
import java.util.Queue;
import java.util.Set;
import java.util.concurrent.DelayQueue;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;

import org.apache.commons.lang3.tuple.Pair;
import org.junit.Test;

import jp.furplag.sandbox.stream.Streamr.Filter.FilteringMode;
Expand Down Expand Up @@ -148,6 +152,17 @@ public void testToSet() {
assertEquals(new HashSet<>(Arrays.asList(new Integer[] { 1, 2, 3 })), Streamr.collect(Streamr.stream(new Integer[] { 1, null, 2, null, 3 }), HashSet::new));
}

@Test
public void testToMap() {
assertEquals(Collections.emptyMap(), Streamr.collect(Stream.of((Pair<Integer, Integer>) null), null, null));
assertThat(Streamr.collect(Stream.of((Pair<Integer, Integer>) null, Pair.of(1, 2)), null, null).toString(), is("{1=2}"));
assertEquals(Stream.of(1, 2).map((x) -> Pair.of(x, x)).collect(Collectors.toMap(Pair::getLeft, Pair::getRight)), Streamr.collect(Stream.of((Pair<Integer, Integer>) null, Pair.of(2, 2), Pair.of(1, 1)), null, null));
assertThat(Streamr.collect(Stream.of((Pair<Integer, Integer>) null, Pair.of(1, 2), Pair.of(1, 1)), null, null).toString(), is("{1=1}"));
assertThat(Streamr.collect(Stream.of((Pair<Integer, Integer>) null, Pair.of(1, 2), Pair.of(1, 1)), (a, b) -> a, null).toString(), is("{1=2}"));
assertThat(Streamr.collect(Stream.of((Pair<Integer, Integer>) null, Pair.of(2, 2), Pair.of(1, 1), Pair.of(1, 2)), (a, b) -> b, LinkedHashMap::new).toString(), is("{2=2, 1=2}"));
assertThat(Streamr.collect(Stream.of((Pair<Integer, Integer>) Pair.of(2, 2), Pair.of(1, 1), Pair.of(1, 2)).sorted(Comparator.comparing(Pair::getLeft)), (a, b) -> b, LinkedHashMap::new).toString(), is("{1=2, 2=2}"));
}

@Test
public void testFilteringMode() {
assertThat(Streamr.Filter.FilteringMode.And.and(), is(true));
Expand Down

0 comments on commit 96f7637

Please sign in to comment.