Java Code Examples for java.util.stream.collector#Characteristics

The following examples show how to use java.util.stream.collector#Characteristics . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MapUtils.java    From BungeeChat2 with GNU General Public License v3.0 5 votes vote down vote up
/**
 * A variant of {@link Collectors#toMap(Function, Function)} for immutable maps.
 *
 * <p>Note this variant throws {@link IllegalArgumentException} upon duplicate keys, rather than
 * {@link IllegalStateException}
 *
 * @param <T> type of the input elements
 * @param <K> output type of the key mapping function
 * @param <V> output type of the value mapping function
 * @param keyMapper a mapping function to produce keys
 * @param valueMapper a mapping function to produce values
 * @return a {@code Collector} which collects elements into a {@code Map} whose keys and values
 *     are the result of applying mapping functions to the input elements
 * @throws IllegalArgumentException upon duplicate keys
 */
public static <T, K, V>
    Collector<T, ImmutableMap.Builder<K, V>, ImmutableMap<K, V>> immutableMapCollector(
        Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends V> valueMapper)
        throws IllegalArgumentException {
  return new Collector<T, ImmutableMap.Builder<K, V>, ImmutableMap<K, V>>() {
    public Supplier<ImmutableMap.Builder<K, V>> supplier() {
      return ImmutableMap.Builder<K, V>::new;
    }

    public BiConsumer<ImmutableMap.Builder<K, V>, T> accumulator() {
      return (builder, element) -> {
        K key = keyMapper.apply(element);
        V value = valueMapper.apply(element);

        builder.put(key, value);
      };
    }

    public BinaryOperator<ImmutableMap.Builder<K, V>> combiner() {
      return (builder1, builder2) -> {
        builder1.putAll(builder2.build());

        return builder1;
      };
    }

    public Function<ImmutableMap.Builder<K, V>, ImmutableMap<K, V>> finisher() {
      return ImmutableMap.Builder<K, V>::build;
    }

    public Set<Collector.Characteristics> characteristics() {
      return ImmutableSet.of();
    }
  };
}
 
Example 2
Source File: MoreCollectors.java    From mill with MIT License 4 votes vote down vote up
private static Collector.Characteristics[] setToArray(Set<Collector.Characteristics> characteristics) {
    return characteristics.toArray(new Collector.Characteristics[characteristics.size()]);
}
 
Example 3
Source File: EvaluationResult.java    From inception with Apache License 2.0 4 votes vote down vote up
@Override
public Set<Collector.Characteristics> characteristics()
{
    return Collections.emptySet();
}
 
Example 4
Source File: MoreCollectors.java    From mill with MIT License 4 votes vote down vote up
private static Collector.Characteristics[] setToArray(Set<Collector.Characteristics> characteristics) {
    return characteristics.toArray(new Collector.Characteristics[characteristics.size()]);
}
 
Example 5
Source File: Collectors.java    From ProjectAres with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public Set<Collector.Characteristics> characteristics() {
    return emptySet();
}
 
Example 6
Source File: Filter.java    From vind with Apache License 2.0 4 votes vote down vote up
@Override
public Set<Collector.Characteristics> characteristics() {
    return EnumSet.of(Collector.Characteristics.UNORDERED);
}
 
Example 7
Source File: AggregationPlan.java    From Mycat2 with GNU General Public License v3.0 4 votes vote down vote up
public Map<AggregationKey, AggregationGroup> groupBy(Scanner scan, int[] groupedFieldsIndexes, String[] aggCallNames, String[] columnNames, List<List<Integer>> args, boolean concurrent) {
    EnumSet<Collector.Characteristics> characteristics = getCharacteristics(concurrent);
    Stream<DataAccessor> stream = scan.stream();
    stream = concurrent ? stream.parallel() : stream;
    return stream.collect(new Collector<DataAccessor, Map<AggregationKey, AggregationGroup>, Map<AggregationKey, AggregationGroup>>() {

        @Override
        public Supplier<Map<AggregationKey, AggregationGroup>> supplier() {
            return () -> concurrent ? new ConcurrentHashMap<>() : new HashMap<>();
        }

        @Override
        public BiConsumer<Map<AggregationKey, AggregationGroup>, DataAccessor> accumulator() {
            return new BiConsumer<Map<AggregationKey, AggregationGroup>, DataAccessor>() {
                private AggregationGroup createGroup(AggregationKey aggregationKey) {
                    return AggregationGroup.of(aggCallNames, columnNames, args);
                }

                @Override
                public void accept(Map<AggregationKey, AggregationGroup> aggregationKeyAggregationGroupMap,
                                   DataAccessor dataAccessor) {
                    AggregationKey key = AggregationKey.of(dataAccessor, groupedFieldsIndexes);
                    AggregationGroup aggregationGroup = aggregationKeyAggregationGroupMap.computeIfAbsent(key, this::createGroup);
                    for (final AggregationCallExp cc : aggregationGroup.getColumns()) {
                        if (concurrent) {
                            synchronized (cc) {
                                cc.accept(dataAccessor);
                            }
                        } else {
                            cc.accept(dataAccessor);
                        }
                    }
                }
            };
        }

        @Override
        public BinaryOperator<Map<AggregationKey, AggregationGroup>> combiner() {
            return (m1, m2) -> {
                for (Map.Entry<AggregationKey, AggregationGroup> e : m2.entrySet())
                    m1.merge(e.getKey(), e.getValue(), AggregationGroup::merge);
                return m1;

            };
        }

        @Override
        public Function<Map<AggregationKey, AggregationGroup>, Map<AggregationKey, AggregationGroup>> finisher() {
            return aggregationKeyAggregationGroupMap -> aggregationKeyAggregationGroupMap;
        }

        @Override
        public Set<Characteristics> characteristics() {
            return characteristics;
        }
    });
}
 
Example 8
Source File: AggregationPlan.java    From Mycat2 with GNU General Public License v3.0 4 votes vote down vote up
@NotNull
public EnumSet<Collector.Characteristics> getCharacteristics(boolean concurrent) {
    return concurrent ?
            EnumSet.of(Collector.Characteristics.CONCURRENT, Collector.Characteristics.UNORDERED, Collector.Characteristics.IDENTITY_FINISH)
            : EnumSet.of(Collector.Characteristics.UNORDERED, Collector.Characteristics.IDENTITY_FINISH);
}
 
Example 9
Source File: KeySetCollector.java    From COSE-JAVA with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public Set<Collector.Characteristics> characteristics() {
  return Collections.emptySet();
}
 
Example 10
Source File: JsonUtils.java    From flow with Apache License 2.0 4 votes vote down vote up
@Override
public Set<Collector.Characteristics> characteristics() {
    return arrayCollectorCharacteristics;
}