Example usage for java.util.stream Collector Collector

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

Introduction

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

Prototype

Collector

Source Link

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 w w  . j a  v a 2 s  .  co  m*/
        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.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(//ww  w .  ja  v  a 2 s.c om
                            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:org.lightjason.agentspeak.common.CPath.java

/**
 * collector factory/* w w w  .j  a  v  a2 s . com*/
 *
 * @return collector
 */
private static Collector<String, List<String>, List<String>> collectorfactory() {
    return new Collector<String, List<String>, List<String>>() {
        @Override
        public final Supplier<List<String>> supplier() {
            return CopyOnWriteArrayList<String>::new;
        }

        @Override
        public final BiConsumer<List<String>, String> accumulator() {
            return List::add;
        }

        @Override
        public final BinaryOperator<List<String>> combiner() {
            return (i, j) -> {
                i.addAll(j);
                return i;
            };
        }

        @Override
        public final Function<List<String>, List<String>> finisher() {
            return i -> i;
        }

        @Override
        public final Set<Characteristics> characteristics() {
            return Collections.emptySet();
        }
    };
}