Skip to content

Commit

Permalink
Merge d06c067 into b95f1d5
Browse files Browse the repository at this point in the history
  • Loading branch information
born-to-be-mad committed Jun 15, 2020
2 parents b95f1d5 + d06c067 commit a9a3d20
Show file tree
Hide file tree
Showing 4 changed files with 433 additions and 19 deletions.
178 changes: 178 additions & 0 deletions src/main/java/one/util/streamex/MoreCollectors.java
Expand Up @@ -168,6 +168,184 @@ private MoreCollectors() {
}, Function.identity(), set -> set.size() == size, UNORDERED_ID_CHARACTERISTICS);
}

/**
* Returns a {@code Collector} that accumulates elements into a {@code Map}
* whose keys and values are taken from {@code Map.Entry}.
*
* @param <K> the {@link Comparable} type of then map keys
* @param <V> the type of the map values
*
* @return {@code Collector} which collects elements into a {@code Map}
* whose keys and values are taken from {@code Map.Entry}
* @throws IllegalStateException if this stream contains duplicate keys
* (according to {@link Object#equals(Object)}).
*
* @see #entriesToMap(BinaryOperator)
* @see #entriesToMap(Function, BinaryOperator)
* @see Collectors#toMap(Function, Function)
* @since 0.7.3
*/
public static <K, V> Collector<Entry<? extends K, ? extends V>, ?, Map<K, V>> entriesToMap() {
return Collectors.toMap(Entry::getKey, Entry::getValue);
}

/**
* Returns a {@code Collector} that accumulates elements into a {@code Map}
* whose keys and values are taken from {@code Map.Entry} and combining them
* using the provided {@code combiner} function to the input elements.
*
* <p>If the mapped keys contains duplicates (according to {@link Object#equals(Object)}),
* the value mapping function is applied to each equal element, and the
* results are merged using the provided {@code combiner} function.
*
* @param <K> the {@link Comparable} type of then map keys
* @param <V> the type of the map values
* @param combiner a merge function, used to resolve collisions between
* values associated with the same key, as supplied
* to {@link Map#merge(Object, Object, BiFunction)}
* @return {@code Collector} which collects elements into a {@code Map}
* whose keys and values are taken from {@code Map.Entry} and combining them
* using the {@code combiner} function
*
* @see #entriesToMap()
* @see #entriesToMap(Function, BinaryOperator)
* @see Collectors#toMap(Function, Function, BinaryOperator)
* @since 0.7.3
*/
public static <K, V> Collector<Entry<? extends K, ? extends V>, ?, Map<K, V>> entriesToMap(
BinaryOperator<V> combiner) {
return Collectors.toMap(Entry::getKey, Entry::getValue, combiner);
}

/**
* Returns a {@code Collector} that accumulates input elements into a {@code Map}
* whose keys are taken from {@code Map.Entry} and values are the result
* of applying the provided {@code valueMapper} function to {@code Map.Entry} values
* and combining them using the provided {@code combiner} function.
*
* @param <K> the {@link Comparable} type of then map keys
* @param <V> the type of the map values
* @param <VV> the output type of the value mapping function
* @param valueMapper a mapping function to produce values
* @param combiner a merge function, used to resolve collisions between
* values associated with the same key, as supplied
* to {@link Map#merge(Object, Object, BiFunction)}
* @return {@code Collector} which collects elements into a {@code Map}
* whose keys are taken from {@code Map.Entry} and values are the result
* of applying the provided {@code valueMapper} function and combining
* them using the provided {@code combiner} function.
* @throws NullPointerException if mapper is null.
*
* @see #entriesToMap()
* @see #entriesToMap(BinaryOperator)
* @see Collectors#toMap(Function, Function, BinaryOperator)
* @since 0.7.3
*/
public static <K, V, VV> Collector<Entry<? extends K, ? extends V>, ?, Map<K, VV>> entriesToMap(
Function<V, VV> valueMapper, BinaryOperator<VV> combiner) {
Objects.requireNonNull(valueMapper);
return Collectors.toMap(Entry::getKey, entry -> valueMapper.apply(entry.getValue()), combiner);
}

/**
* Returns a {@code Collector} that accumulates elements into
* a result {@code Map} defined by {@code mapSupplier} function
* whose keys and values are taken from {@code Map.Entry}.
*
* @param <K> the {@link Comparable} type of then map keys
* @param <V> the type of the map values
* @param <M> the type of the resulting {@code Map}
* @return {@code Collector} which collects elements into a {@code Map}
* defined by {@code mapSupplier} function
* whose keys and values are taken from {@code Map.Entry}
* @throws IllegalStateException if this stream contains duplicate keys
* (according to {@link Object#equals(Object)}).
*
* @see #entriesToCustomMap(BinaryOperator, Supplier)
* @see #entriesToCustomMap(Function, BinaryOperator, Supplier)
* @see Collectors#toMap(Function, Function, BinaryOperator, Supplier)
* @since 0.7.3
*/
public static <K, V, M extends Map<K, V>> Collector<Entry<? extends K, ? extends V>, ?, M> entriesToCustomMap(
Supplier<M> mapSupplier) {
return Collectors.toMap(Entry::getKey, Entry::getValue, throwingMerger(), mapSupplier);
}

/**
* Returns a {@code Collector} that accumulates elements into
* a result {@code Map} defined by {@code mapSupplier} function
* whose keys and values are taken from {@code Map.Entry} and combining them
* using the provided {@code combiner} function to the input elements.
*
* <p>If the mapped keys contains duplicates (according to {@link Object#equals(Object)}),
* the value mapping function is applied to each equal element, and the
* results are merged using the provided {@code combiner} function.
*
* @param <K> the {@link Comparable} type of then map keys
* @param <V> the type of the map values
* @param <M> the type of the resulting {@code Map}
* @param combiner a merge function, used to resolve collisions between
* values associated with the same key, as supplied
* to {@link Map#merge(Object, Object, BiFunction)}
* @param mapSupplier a function which returns a new, empty {@code Map} into
* which the results will be inserted
* @return {@code Collector} which collects elements into a {@code Map}
* whose keys and values are taken from {@code Map.Entry} and combining them
* using the {@code combiner} function
*
* @see #entriesToCustomMap(Supplier)
* @see #entriesToCustomMap(Function, BinaryOperator, Supplier)
* @see Collectors#toMap(Function, Function, BinaryOperator, Supplier)
* @since 0.7.3
*/
public static <K, V, M extends Map<K, V>> Collector<Entry<? extends K, ? extends V>, ?, M> entriesToCustomMap(
BinaryOperator<V> combiner, Supplier<M> mapSupplier) {
return Collectors.toMap(Entry::getKey, Entry::getValue, combiner, mapSupplier);
}

/**
* Returns a {@code Collector} that accumulates elements into
* a result {@code Map} defined by {@code mapSupplier} function
* whose keys are taken from {@code Map.Entry} and values are the result
* of applying the provided {@code valueMapper} function to {@code Map.Entry} values
* and combining them using the provided {@code combiner} function.
*
* <p>If the mapped keys contains duplicates (according to {@link Object#equals(Object)}),
* the value mapping function is applied to each equal element, and the
* results are merged using the provided {@code combiner} function.
*
* @param <K> the {@link Comparable} type of then map keys
* @param <V> the type of the map values
* @param <VV> the output type of the value mapping function
* @param <M> the type of the resulting {@code Map}
* @param valueMapper a mapping function to produce values
* @param combiner a merge function, used to resolve collisions between
* values associated with the same key, as supplied
* to {@link Map#merge(Object, Object, BiFunction)}
* @param mapSupplier a function which returns a new, empty {@code Map} into
* which the results will be inserted
* @return {@code Collector} which collects elements into a {@code Map}
* whose keys and values are taken from {@code Map.Entry} and combining them
* using the {@code combiner} function
* @throws NullPointerException if mapper is null.
*
* @see #entriesToCustomMap(Supplier)
* @see #entriesToCustomMap(BinaryOperator, Supplier)
* @see Collectors#toMap(Function, Function, BinaryOperator, Supplier)
* @since 0.7.3
*/
public static <K, V, VV, M extends Map<K, VV>> Collector<Entry<? extends K, ? extends V>, ?, M> entriesToCustomMap(
Function<V, VV> valueMapper, BinaryOperator<VV> combiner, Supplier<M> mapSupplier) {
Objects.requireNonNull(valueMapper);
return Collectors.toMap(Entry::getKey, entry -> valueMapper.apply(entry.getValue()), combiner, mapSupplier);
}

private static <T> BinaryOperator<T> throwingMerger() {
return (firstKey, secondKey) -> {
throw new IllegalStateException("Duplicate entry keys are not allowed (attempt to merge key '" + firstKey + "'). ");
};
}

/**
* Returns a {@code Collector} which counts a number of distinct values the
* mapper function returns for the stream elements.
Expand Down

0 comments on commit a9a3d20

Please sign in to comment.