LangCollectors.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */

  17. package org.apache.commons.lang3.stream;

  18. import java.util.Arrays;
  19. import java.util.Collections;
  20. import java.util.Objects;
  21. import java.util.Set;
  22. import java.util.StringJoiner;
  23. import java.util.function.BiConsumer;
  24. import java.util.function.BinaryOperator;
  25. import java.util.function.Function;
  26. import java.util.function.Supplier;
  27. import java.util.stream.Collector;
  28. import java.util.stream.Collectors;
  29. import java.util.stream.Stream;

  30. import org.apache.commons.lang3.StringUtils;

  31. /**
  32.  * Implementations of {@link Collector} that implement various reduction operations.
  33.  * <p>
  34.  * This class is called {@code LangCollectors} instead of {@code Collectors} to avoid clashes with {@link Collectors}.
  35.  * </p>
  36.  *
  37.  * @since 3.13.0
  38.  */
  39. public final class LangCollectors {

  40.     /**
  41.      * Simple implementation class for {@code Collector}.
  42.      *
  43.      * @param <T> the type of elements to be collected
  44.      * @param <R> the type of the result
  45.      */
  46.     private static final class SimpleCollector<T, A, R> implements Collector<T, A, R> {

  47.         private final BiConsumer<A, T> accumulator;
  48.         private final Set<Characteristics> characteristics;
  49.         private final BinaryOperator<A> combiner;
  50.         private final Function<A, R> finisher;
  51.         private final Supplier<A> supplier;

  52.         private SimpleCollector(final Supplier<A> supplier, final BiConsumer<A, T> accumulator, final BinaryOperator<A> combiner, final Function<A, R> finisher,
  53.             final Set<Characteristics> characteristics) {
  54.             this.supplier = supplier;
  55.             this.accumulator = accumulator;
  56.             this.combiner = combiner;
  57.             this.finisher = finisher;
  58.             this.characteristics = characteristics;
  59.         }

  60.         @Override
  61.         public BiConsumer<A, T> accumulator() {
  62.             return accumulator;
  63.         }

  64.         @Override
  65.         public Set<Characteristics> characteristics() {
  66.             return characteristics;
  67.         }

  68.         @Override
  69.         public BinaryOperator<A> combiner() {
  70.             return combiner;
  71.         }

  72.         @Override
  73.         public Function<A, R> finisher() {
  74.             return finisher;
  75.         }

  76.         @Override
  77.         public Supplier<A> supplier() {
  78.             return supplier;
  79.         }
  80.     }

  81.     private static final Set<Collector.Characteristics> CH_NOID = Collections.emptySet();

  82.     /**
  83.      * Delegates to {@link Stream#collect(Collector)} for a Stream on the given array.
  84.      *
  85.      * @param <T>       The type of the array elements.
  86.      * @param <R>       the type of the result.
  87.      * @param <A>       the intermediate accumulation type of the {@code Collector}.
  88.      * @param collector the {@code Collector} describing the reduction.
  89.      * @param array     The array, assumed to be unmodified during use.
  90.      * @return the result of the reduction
  91.      * @see Stream#collect(Collector)
  92.      * @see Arrays#stream(Object[])
  93.      * @see Collectors
  94.      * @since 3.16.0
  95.      */
  96.     public static <T, R, A> R collect(final Collector<? super T, A, R> collector, final T... array) {
  97.         return Arrays.stream(array).collect(collector);
  98.     }

  99.     /**
  100.      * Returns a {@code Collector} that concatenates the input elements, separated by the specified delimiter, in encounter order.
  101.      * <p>
  102.      * This is a variation of {@link Collectors#joining()} that works with any element class, not just {@code CharSequence}.
  103.      * </p>
  104.      * <p>
  105.      * For example:
  106.      * </p>
  107.      *
  108.      * <pre>
  109.      * Stream.of(Long.valueOf(1), Long.valueOf(2), Long.valueOf(3))
  110.      *    .collect(LangCollectors.joining())
  111.      * returns "123"
  112.      * </pre>
  113.      *
  114.      * @return A {@code Collector} which concatenates Object elements, separated by the specified delimiter, in encounter order.
  115.      */
  116.     public static Collector<Object, ?, String> joining() {
  117.         return new SimpleCollector<>(StringBuilder::new, StringBuilder::append, StringBuilder::append, StringBuilder::toString, CH_NOID);
  118.     }

  119.     /**
  120.      * Returns a {@code Collector} that concatenates the input elements, separated by the specified delimiter, in encounter order.
  121.      * <p>
  122.      * This is a variation of {@link Collectors#joining(CharSequence)} that works with any element class, not just {@code CharSequence}.
  123.      * </p>
  124.      * <p>
  125.      * For example:
  126.      * </p>
  127.      *
  128.      * <pre>
  129.      * Stream.of(Long.valueOf(1), Long.valueOf(2), Long.valueOf(3))
  130.      *   .collect(LangCollectors.joining("-"))
  131.      * returns "1-2-3"
  132.      * </pre>
  133.      *
  134.      * @param delimiter the delimiter to be used between each element.
  135.      * @return A {@code Collector} which concatenates Object elements, separated by the specified delimiter, in encounter order.
  136.      */
  137.     public static Collector<Object, ?, String> joining(final CharSequence delimiter) {
  138.         return joining(delimiter, StringUtils.EMPTY, StringUtils.EMPTY);
  139.     }

  140.     /**
  141.      * Returns a {@code Collector} that concatenates the input elements, separated by the specified delimiter, with the
  142.      * specified prefix and suffix, in encounter order.
  143.      * <p>
  144.      * This is a variation of {@link Collectors#joining(CharSequence, CharSequence, CharSequence)} that works with any
  145.      * element class, not just {@code CharSequence}.
  146.      * </p>
  147.      * <p>
  148.      * For example:
  149.      * </p>
  150.      *
  151.      * <pre>
  152.      * Stream.of(Long.valueOf(1), Long.valueOf(2), Long.valueOf(3))
  153.      *   .collect(LangCollectors.joining("-", "[", "]"))
  154.      * returns "[1-2-3]"
  155.      * </pre>
  156.      *
  157.      * @param delimiter the delimiter to be used between each element
  158.      * @param prefix the sequence of characters to be used at the beginning of the joined result
  159.      * @param suffix the sequence of characters to be used at the end of the joined result
  160.      * @return A {@code Collector} which concatenates CharSequence elements, separated by the specified delimiter, in
  161.      *         encounter order
  162.      */
  163.     public static Collector<Object, ?, String> joining(final CharSequence delimiter, final CharSequence prefix, final CharSequence suffix) {
  164.         return joining(delimiter, prefix, suffix, Objects::toString);
  165.     }

  166.     /**
  167.      * Returns a {@code Collector} that concatenates the input elements, separated by the specified delimiter, with the specified prefix and suffix, in
  168.      * encounter order.
  169.      * <p>
  170.      * This is a variation of {@link Collectors#joining(CharSequence, CharSequence, CharSequence)} that works with any element class, not just
  171.      * {@code CharSequence}.
  172.      * </p>
  173.      * <p>
  174.      * For example:
  175.      * </p>
  176.      *
  177.      * <pre>{@code
  178.      * Stream.of(Long.valueOf(1), null, Long.valueOf(3))
  179.      *   .collect(LangCollectors.joining("-", "[", "]", o -> Objects.toString(o, "NUL")))
  180.      * returns "[1-NUL-3]"
  181.      * }</pre>
  182.      *
  183.      * @param delimiter the delimiter to be used between each element
  184.      * @param prefix    the sequence of characters to be used at the beginning of the joined result
  185.      * @param suffix    the sequence of characters to be used at the end of the joined result
  186.      * @param toString  A function that takes an Object and returns a non-null String.
  187.      * @return A {@code Collector} which concatenates CharSequence elements, separated by the specified delimiter, in encounter order
  188.      */
  189.     public static Collector<Object, ?, String> joining(final CharSequence delimiter, final CharSequence prefix, final CharSequence suffix,
  190.         final Function<Object, String> toString) {
  191.         return new SimpleCollector<>(() -> new StringJoiner(delimiter, prefix, suffix), (a, t) -> a.add(toString.apply(t)), StringJoiner::merge,
  192.             StringJoiner::toString, CH_NOID);
  193.     }

  194.     private LangCollectors() {
  195.         // No instance
  196.     }

  197. }