Example usage for java.util.stream Collector characteristics

List of usage examples for java.util.stream Collector characteristics

Introduction

In this page you can find the example usage for java.util.stream Collector characteristics.

Prototype

Set<Characteristics> characteristics();

Source Link

Document

Returns a Set of Collector.Characteristics indicating the characteristics of this Collector.

Usage

From source file:msi.gama.util.GamaListFactory.java

public static <T> Collector<T, IList<T>, IList<T>> toGamaList() {
    return new Collector<T, IList<T>, IList<T>>() {

        @Override//from   w ww .  ja  v a 2 s  . c  om
        public Supplier<IList<T>> supplier() {
            return GamaListFactory::create;
        }

        @Override
        public BiConsumer<IList<T>, T> accumulator() {
            return (left, right) -> left.add(right);
        }

        @Override
        public BinaryOperator<IList<T>> combiner() {
            return (left, right) -> {
                left.addAll(right);
                return left;
            };
        }

        @Override
        public java.util.function.Function<IList<T>, IList<T>> finisher() {
            return (left) -> left;
        }

        @Override
        public Set<java.util.stream.Collector.Characteristics> characteristics() {
            return CH;
        }
    };
}

From source file:com.github.steveash.guavate.Guavate.java

/**
 * Collector used at the end of a stream to build an immutable set.
 * <p>/*from   www  .j  a  v a 2 s .c  o m*/
 * A collector is used to gather data at the end of a stream operation.
 * This method returns a collector allowing streams to be gathered into
 * an {@link ImmutableSet}.
 * @param <T> the type of element in the set
 * @return the immutable set collector
 */
public static <T> Collector<T, ImmutableSet.Builder<T>, ImmutableSet<T>> toImmutableSet() {
    return Collector.of(ImmutableSet.Builder<T>::new, ImmutableSet.Builder<T>::add,
            (l, r) -> l.addAll(r.build()), ImmutableSet.Builder<T>::build, Collector.Characteristics.UNORDERED);
}

From source file:com.github.steveash.guavate.Guavate.java

/**
 * Collector used at the end of a stream to build an immutable sorted set.
 * <p>/*from ww w . j  ava 2s. c  om*/
 * A collector is used to gather data at the end of a stream operation.
 * This method returns a collector allowing streams to be gathered into
 * an {@link ImmutableSet}.
 * @param <T> the type of element in the sorted set
 * @return the immutable sorted set collector
 */
public static <T extends Comparable<?>> Collector<T, ImmutableSortedSet.Builder<T>, ImmutableSortedSet<T>> toImmutableSortedSet() {
    return Collector.of((Supplier<ImmutableSortedSet.Builder<T>>) ImmutableSortedSet::naturalOrder,
            ImmutableSortedSet.Builder<T>::add, (l, r) -> l.addAll(r.build()),
            ImmutableSortedSet.Builder<T>::build, Collector.Characteristics.UNORDERED);
}

From source file:com.github.steveash.guavate.Guavate.java

/**
 * Collector used at the end of a stream to build an immutable sorted set.
 * <p>/*from  w ww . j a v a  2  s.  c om*/
 * A collector is used to gather data at the end of a stream operation.
 * This method returns a collector allowing streams to be gathered into
 * an {@link ImmutableSet}.
 * @param <T> the type of element in the sorted set
 * @param comparator the comparator
 * @return the immutable sorted set collector
 */
public static <T> Collector<T, ImmutableSortedSet.Builder<T>, ImmutableSortedSet<T>> toImmutableSortedSet(
        Comparator<? super T> comparator) {
    return Collector.of(
            (Supplier<ImmutableSortedSet.Builder<T>>) () -> new ImmutableSortedSet.Builder<>(comparator),
            ImmutableSortedSet.Builder<T>::add, (l, r) -> l.addAll(r.build()),
            ImmutableSortedSet.Builder<T>::build, Collector.Characteristics.UNORDERED);
}

From source file:com.github.steveash.guavate.Guavate.java

/**
 * Collector used at the end of a stream to build an immutable multiset.
 * <p>//  w  ww. j a v  a2  s  . c o  m
 * A collector is used to gather data at the end of a stream operation.
 * This method returns a collector allowing streams to be gathered into
 * an {@link ImmutableMultiset}.
 * @param <T> the type of element in the multiset
 * @return the immutable multiset collector
 */
public static <T> Collector<T, ImmutableMultiset.Builder<T>, ImmutableMultiset<T>> toImmutableMultiset() {
    return Collector.of(ImmutableMultiset.Builder<T>::new, ImmutableMultiset.Builder<T>::add,
            (l, r) -> l.addAll(r.build()), ImmutableMultiset.Builder<T>::build,
            Collector.Characteristics.UNORDERED);
}

From source file:com.ikanow.aleph2.data_model.utils.TestCrudUtils.java

private static <T> DBObject convertToMongoQuery_single(String andVsOr, SingleQueryComponent<T> query_in) {
    LinkedHashMultimap<String, Tuple2<Operator, Tuple2<Object, Object>>> fields = query_in.getAll();

    // The actual query:

    return Patterns.match(fields).<DBObject>andReturn().when(f -> f.isEmpty(), f -> new BasicDBObject())
            .otherwise(f -> f.asMap().entrySet().stream()
                    .<Tuple2<String, Tuple2<Operator, Tuple2<Object, Object>>>>flatMap(
                            entry -> entry.getValue().stream().map(val -> Tuples._2T(entry.getKey(), val)))
                    .collect(/*w w w  . ja v  a  2s  .  co  m*/
                            new Collector<Tuple2<String, Tuple2<Operator, Tuple2<Object, Object>>>, BasicDBObject, DBObject>() {
                                @Override
                                public Supplier<BasicDBObject> supplier() {
                                    return BasicDBObject::new;
                                }

                                @Override
                                public BiConsumer<BasicDBObject, Tuple2<String, Tuple2<Operator, Tuple2<Object, Object>>>> accumulator() {
                                    return (acc, entry) -> {
                                        Patterns.match(acc.get(andVsOr)).andAct().when(l -> (null == l), l -> {
                                            BasicDBList dbl = new BasicDBList();
                                            dbl.add(operatorToMongoKey(entry._1(), entry._2()));
                                            acc.put(andVsOr, dbl);
                                        }).when(BasicDBList.class,
                                                l -> l.add(operatorToMongoKey(entry._1(), entry._2())))
                                                .otherwise(l -> {
                                                });
                                    };
                                }

                                // Boilerplate:
                                @Override
                                public BinaryOperator<BasicDBObject> combiner() {
                                    return (a, b) -> {
                                        a.putAll(b.toMap());
                                        return a;
                                    };
                                }

                                @Override
                                public Function<BasicDBObject, DBObject> finisher() {
                                    return acc -> acc;
                                }

                                @Override
                                public Set<java.util.stream.Collector.Characteristics> characteristics() {
                                    return EnumSet.of(Characteristics.UNORDERED);
                                }
                            }));
}

From source file:com.github.steveash.guavate.Guavate.java

/**
 * Collector used at the end of a stream to build an immutable map.
 * <p>//from   ww  w.j  a va  2 s.c  o  m
 * A collector is used to gather data at the end of a stream operation.
 * This method returns a collector allowing streams to be gathered into
 * an {@link ImmutableMap}.
 * <p>
 * This returns a map by converting each stream element to a key and value.
 * The input stream must resolve to unique keys.
 * See {@link Collectors#toMap(Function, Function)} for more details.
 * @param <T> the type of the stream elements
 * @param <K> the type of the keys in the result map
 * @param <V> the type of the values in the result map
 * @param keyExtractor function to produce keys from stream elements
 * @param valueExtractor function to produce values from stream elements
 * @return the immutable map collector
 * @throws IllegalArgumentException if the same key is generated twice
 */
public static <T, K, V> Collector<T, ?, ImmutableMap<K, V>> toImmutableMap(
        Function<? super T, ? extends K> keyExtractor, Function<? super T, ? extends V> valueExtractor) {

    return Collector.of(ImmutableMap.Builder<K, V>::new,
            (builder, val) -> builder.put(keyExtractor.apply(val), valueExtractor.apply(val)),
            (l, r) -> l.putAll(r.build()), ImmutableMap.Builder<K, V>::build,
            Collector.Characteristics.UNORDERED);
}

From source file:com.github.steveash.guavate.Guavate.java

/**
 * Collector used at the end of a stream to build an immutable map.
 * <p>/* w  w  w.j a  v a2s  . co m*/
 * A collector is used to gather data at the end of a stream operation.
 * This method returns a collector allowing streams to be gathered into
 * an {@link ImmutableMap}.
 * <p>
 * This returns a map by converting each stream element to a key and value.
 * If the same key is generated more than once the merge function is applied to the
 * values and the return value of the function is used as the value in the map.
 * @param <T> the type of the stream elements
 * @param <K> the type of the keys in the result map
 * @param <V> the type of the values in the result map
 * @param keyExtractor function to produce keys from stream elements
 * @param valueExtractor function to produce values from stream elements
 * @param mergeFn function to merge values with the same key
 * @return the immutable map collector
 */
public static <T, K, V> Collector<T, Map<K, V>, ImmutableMap<K, V>> toImmutableMap(
        Function<? super T, ? extends K> keyExtractor, Function<? super T, ? extends V> valueExtractor,
        BiFunction<? super V, ? super V, ? extends V> mergeFn) {

    return Collector.of(HashMap<K, V>::new,
            (map, val) -> map.merge(keyExtractor.apply(val), valueExtractor.apply(val), mergeFn),
            (m1, m2) -> mergeMaps(m1, m2, mergeFn), map -> ImmutableMap.copyOf(map),
            Collector.Characteristics.UNORDERED);
}

From source file:com.github.steveash.guavate.Guavate.java

/**
 * Collector used at the end of a stream to build an immutable sorted map.
 * <p>//w ww  .  ja  va  2s.  co  m
 * A collector is used to gather data at the end of a stream operation.
 * This method returns a collector allowing streams to be gathered into
 * an {@link ImmutableSortedMap}.
 * <p>
 * This returns a map by converting each stream element to a key and value.
 * The input stream must resolve to unique keys.
 * See {@link Collectors#toMap(Function, Function)} for more details.
 * @param <T> the type of the stream elements
 * @param <K> the type of the keys in the result map
 * @param <V> the type of the values in the result map
 * @param keyExtractor function to produce keys from stream elements
 * @param valueExtractor function to produce values from stream elements
 * @return the immutable sorted map collector
 * @throws IllegalArgumentException if the same key is generated twice
 */
public static <T, K extends Comparable<?>, V> Collector<T, ?, ImmutableSortedMap<K, V>> toImmutableSortedMap(
        Function<? super T, ? extends K> keyExtractor, Function<? super T, ? extends V> valueExtractor) {

    return Collector.of((Supplier<ImmutableSortedMap.Builder<K, V>>) ImmutableSortedMap::naturalOrder,
            (builder, val) -> builder.put(keyExtractor.apply(val), valueExtractor.apply(val)),
            (l, r) -> l.putAll(r.build()), ImmutableSortedMap.Builder<K, V>::build,
            Collector.Characteristics.UNORDERED);
}

From source file:com.github.steveash.guavate.Guavate.java

/**
 * Collector used at the end of a stream to build an immutable multimap.
 * <p>/*  w  ww  . j ava  2s  . co m*/
 * A collector is used to gather data at the end of a stream operation.
 * This method returns a collector allowing streams to be gathered into
 * an {@link ImmutableListMultimap}.
 * <p>
 * This returns a multimap by converting each stream element to a key and value.
 * Stream elements may be converted to the same key, with the values forming a multimap list.
 * @param <T> the type of the stream elements
 * @param <K> the type of the keys in the result multimap
 * @param <V> the type of the values in the result multimap
 * @param keyExtractor function to produce keys from stream elements
 * @param valueExtractor function to produce values from stream elements
 * @return the immutable multimap collector
 */
public static <T, K, V> Collector<T, ?, ImmutableListMultimap<K, V>> toImmutableListMultimap(
        Function<? super T, ? extends K> keyExtractor, Function<? super T, ? extends V> valueExtractor) {

    return Collector.of(ImmutableListMultimap.Builder<K, V>::new,
            (builder, val) -> builder.put(keyExtractor.apply(val), valueExtractor.apply(val)),
            (l, r) -> l.putAll(r.build()), ImmutableListMultimap.Builder<K, V>::build,
            Collector.Characteristics.UNORDERED);
}