downstreamAccumulator.accept(container, t); }; return PartialCollector.grouping(mapFactory, downstream).asInt(accumulator);
/** * Adapts a {@link Collector} accepting elements of type {@code U} to an * {@code IntCollector} by applying a mapping function to each input element * before accumulation. * * @param <U> type of elements accepted by downstream collector * @param <A> intermediate accumulation type of the downstream collector * @param <R> result type of collector * @param mapper a function to be applied to the input elements * @param downstream a collector which will accept mapped values * @return a collector which applies the mapping function to the input * elements and provides the mapped results to the downstream * collector */ static <U, A, R> IntCollector<?, R> mappingToObj(IntFunction<U> mapper, Collector<U, A, R> downstream) { BiConsumer<A, U> accumulator = downstream.accumulator(); if (downstream instanceof MergingCollector) { return new IntCollectorImpl<>(downstream.supplier(), (acc, i) -> accumulator.accept(acc, mapper.apply(i)), ((MergingCollector<U, A, R>) downstream).merger(), downstream.finisher(), downstream .characteristics()); } return Box.partialCollector(downstream).asInt((box, i) -> accumulator.accept(box.a, mapper.apply(i))); }
/** * Returns an {@code IntCollector} which produces a boolean array containing * the results of applying the given predicate to the input elements, in * encounter order. * * @param predicate a non-interfering, stateless predicate to apply to each * input element. The result values of this predicate are collected * to the resulting boolean array. * @return an {@code IntCollector} which collects the results of the * predicate function to the boolean array, in encounter order. * @since 0.3.8 */ static IntCollector<?, boolean[]> toBooleanArray(IntPredicate predicate) { return PartialCollector.booleanArray().asInt((box, t) -> { if (predicate.test(t)) box.a.set(box.b); box.b = StrictMath.addExact(box.b, 1); }); } }
/** * Returns an {@code IntCollector} which partitions the input numbers * according to an {@code IntPredicate}, reduces the values in each * partition according to another {@code IntCollector}, and organizes them * into a {@code Map<Boolean, D>} whose values are the result of the * downstream reduction. * * <p> * There are no guarantees on the type, mutability, serializability, or * thread-safety of the {@code Map} returned. * * @param <A> the intermediate accumulation type of the downstream collector * @param <D> the result type of the downstream reduction * @param predicate a predicate used for classifying input elements * @param downstream an {@code IntCollector} implementing the downstream * reduction * @return an {@code IntCollector} implementing the cascaded partitioning * operation */ static <A, D> IntCollector<?, Map<Boolean, D>> partitioningBy(IntPredicate predicate, IntCollector<A, D> downstream) { ObjIntConsumer<A> downstreamAccumulator = downstream.intAccumulator(); ObjIntConsumer<BooleanMap<A>> accumulator = (result, t) -> downstreamAccumulator.accept( predicate.test(t) ? result.trueValue : result.falseValue, t); return BooleanMap.partialCollector(downstream).asInt(accumulator); }
/** * Returns an {@code IntCollector} that converts the input numbers to * strings and concatenates them, separated by the specified delimiter, with * the specified prefix and suffix, in encounter order. * * @param delimiter the delimiter to be used between each element * @param prefix the sequence of characters to be used at the beginning of * the joined result * @param suffix the sequence of characters to be used at the end of the * joined result * @return An {@code IntCollector} which concatenates the input numbers, * separated by the specified delimiter, in encounter order */ static IntCollector<?, String> joining(CharSequence delimiter, CharSequence prefix, CharSequence suffix) { return PartialCollector.joining(delimiter, prefix, suffix, true).asInt( StreamExInternals.joinAccumulatorInt(delimiter)); }
/** * Returns an {@code IntCollector} that converts the input numbers to * strings and concatenates them, separated by the specified delimiter, in * encounter order. * * @param delimiter the delimiter to be used between each element * @return An {@code IntCollector} which concatenates the input numbers, * separated by the specified delimiter, in encounter order */ static IntCollector<?, String> joining(CharSequence delimiter) { return PartialCollector.joining(delimiter, null, null, false).asInt( StreamExInternals.joinAccumulatorInt(delimiter)); }
/** * Returns an {@code IntCollector} that produces the sum of the input * elements. If no elements are present, the result is 0. * * @return an {@code IntCollector} that produces the sum of the input * elements */ static IntCollector<?, Integer> summing() { return PartialCollector.intSum().asInt((box, i) -> box[0] += i); }
/** * Returns an {@code IntCollector} that counts the number of input elements * and returns the result as {@code Integer}. If no elements are present, * the result is 0. * * @return an {@code IntCollector} that counts the input elements */ static IntCollector<?, Integer> countingInt() { return PartialCollector.intSum().asInt((box, i) -> box[0]++); }
/** * Returns an {@code IntCollector} that counts the number of input elements * and returns the result as {@code Long}. If no elements are present, the * result is 0. * * @return an {@code IntCollector} that counts the input elements */ static IntCollector<?, Long> counting() { return PartialCollector.longSum().asInt((box, i) -> box[0]++); }