Skip to content

Instantly share code, notes, and snippets.

@yaauie
Created October 5, 2016 17:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yaauie/68aee3f74cc240d73486236573069bac to your computer and use it in GitHub Desktop.
Save yaauie/68aee3f74cc240d73486236573069bac to your computer and use it in GitHub Desktop.
package com.yaauie.guava-utils;
import com.google.common.collect.ImmutableBiMap;
import com.google.common.collect.ImmutableCollection;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSortedMap;
import com.google.common.collect.ImmutableSortedSet;
import java.util.Comparator;
import java.util.function.Function;
import java.util.stream.Collector;
import java.util.stream.Stream;
/**
* Implementations of {@link Collector} that are useful for transforming a {@link Stream} into
* an {@link ImmutableCollection}.
*
* Relies on optimisations within the implementations of {@link ImmutableCollection.Builder}
* to minimize the number of single-use objects created.
*/
public class ImmutableCollectors {
/**
* @param <E> the type of items in the stream (generally can be inferred).
*
* @return a {@link Collector} for transforming a {@link Stream}{@code <E>} into an order-
* preserved {@link ImmutableList}{@code <E>}.
*/
public static <E> Collector<E, ImmutableList.Builder<E>, ImmutableList<E>> toImmutableList() {
return Collector.of(ImmutableList.Builder::new,
ImmutableList.Builder::add,
(left, right) -> left.addAll(right.build()),
ImmutableList.Builder<E>::build);
}
/**
* @param <E> the type of items in the stream (generally can be inferred).
*
* @return a {@link Collector} for transforming a {@link Stream}{@code <E>} into an
* {@link ImmutableSet}{@code <E>} of distinct entries, ordered by first appearance.
*/
public static <E> Collector<E, ImmutableSet.Builder<E>, ImmutableSet<E>> toImmutableSet() {
return Collector.of(ImmutableSet.Builder::new,
ImmutableSet.Builder::add,
(left, right) -> left.addAll(right.build()),
ImmutableSet.Builder<E>::build);
}
/**
* @param comparator the {@link Comparator<E>} with which to sort the elements in the stream.
*
* @param <E> the type of items in the stream (generally can be inferred).
*
* @return a {@link Collector} for transforming a {@link Stream}{@code <E>} into an
* {@link ImmutableSortedSet}{@code <E>} of distinct entries, ordered by the given
* {@link Comparator}{@code <E>}.
*/
public static <E> Collector<E, ImmutableSortedSet.Builder<E>, ImmutableSortedSet<E>> toImmutableSortedSet(final Comparator<? super E> comparator) {
return Collector.of(() -> new ImmutableSortedSet.Builder<>(comparator),
ImmutableSortedSet.Builder::add,
(left, right) -> left.addAll(right.build()),
ImmutableSortedSet.Builder<E>::build);
}
/**
* @param <E> the type of items in the stream, generally inferred.
*
* @return a {@link Collector} for transforming a {@link Stream}{@code <E extends Comparable<E>>}
* into an {@link ImmutableSortedSet}{@code <E>} of all distinct entries, ordered by the
* natural order.
*/
public static <E extends Comparable<E>> Collector<E, ImmutableSortedSet.Builder<E>, ImmutableSortedSet<E>> toImmutableSortedSet(){
return toImmutableSortedSet(Comparator.naturalOrder());
}
/**
* @param keyMapper a {@link Function} that creates a key from an element in the stream.
* @param valueMapper a {@link Function} that creates a value from an element in the stream.
*
* @param <E> the type of item in the stream (generally can be inferred).
* @param <K> the type of key in the resulting {@link ImmutableMap} (generally can be inferred).
* @param <V> the type of value in the resulting {@link ImmutableMap} (generally can be inferred).
*
* @return a {@link Collector} for transforming a {@link Stream}{@code <E>} into an order-
* preserved {@link ImmutableMap}{@code <K, V>}.
*/
public static <E, K, V> Collector<E, ImmutableMap.Builder<K, V>, ImmutableMap<K, V>> toImmutableMap(final Function<E, K> keyMapper,
final Function<E, V> valueMapper) {
return Collector.of(ImmutableMap::<K, V>builder,
(partial, element) -> partial.put(keyMapper.apply(element), valueMapper.apply(element)),
(left, right) -> left.putAll(right.build()),
ImmutableMap.Builder::build);
}
/**
*
*
* @param keyMapper a {@link Function} that creates a key from an element in the stream.
* @param valueMapper a {@link Function} that creates a value from an element in the stream.
*
* @param <E> the type of item in the stream (generally can be inferred).
* @param <K> the type of key in the resulting {@link ImmutableMap} (generally can be inferred).
* @param <V> the type of value in the resulting {@link ImmutableMap}.
*
* @return a {@link Collector} for transforming a {@link Stream}{@code <E>} into an order-
* preserved {@link ImmutableBiMap}{@code <K, V>}.
*/
public static <E, K, V> Collector<E, ImmutableBiMap.Builder<K, V>, ImmutableBiMap<K, V>> toImmutableBiMap(final Function<E, K> keyMapper,
final Function<E, V> valueMapper) {
return Collector.of(ImmutableBiMap::<K, V>builder,
(partial, element) -> partial.put(keyMapper.apply(element), valueMapper.apply(element)),
(left, right) -> left.putAll(right.build()),
ImmutableBiMap.Builder::build);
}
/**
* @param keyMapper a {@link Function} that creates a key from an element in the stream.
* @param valueMapper a {@link Function} that creates a value from an element in the stream.
*
* @param <E> the type of item in the stream (generally can be inferred).
* @param <K> the type of key in the resulting {@link ImmutableMap} (generally can be inferred).
* @param <V> the type of value in the resulting {@link ImmutableMap} (generally can be inferred).
*
* @return a {@link Collector} for transforming a {@link Stream}{@code <E>} into an
* {@link ImmutableSortedMap}{@code <K, V>} ordered by the given
* {@link Comparator}{@code <K>}.
*/
public static <E, K, V> Collector<E, ImmutableSortedMap.Builder<K, V>, ImmutableSortedMap<K, V>> toImmutableSortedMap(final Function<E, K> keyMapper,
final Function<E, V> valueMapper,
final Comparator<? super K> comparator) {
return Collector.of(() -> new ImmutableSortedMap.Builder<>(comparator),
(partial, element) -> partial.put(keyMapper.apply(element), valueMapper.apply(element)),
(left, right) -> left.putAll(right.build()),
ImmutableSortedMap.Builder::build);
}
/**
* @param keyMapper a {@link Function} that creates a key from an element in the stream.
* @param valueMapper a {@link Function} that creates a value from an element in the stream.
*
* @param <E> the type of item in the stream (generally can be inferred)
* @param <K> the type of key in the resulting {@link ImmutableMap}, must implement
* {@link Comparable}{@code <K>} (generally can be inferred).
* @param <V> the type of value in the resulting {@link ImmutableMap} (generally can be inferred).
*
* @return a {@link Collector} for transforming a {@link Stream}{@code <E>} into an
* {@link ImmutableSortedMap}{@code <K extends Comparable<K>, V>} ordered by the natural
* order of {@code <K extends Comparable<K>>}.
*/
public static <E, K extends Comparable<K>, V> Collector<E, ImmutableSortedMap.Builder<K, V>, ImmutableSortedMap<K, V>> toImmutableSortedMap(final Function<E, K> keyMapper,
final Function<E, V> valueMapper) {
return toImmutableSortedMap(keyMapper, valueMapper, Comparator.naturalOrder());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment