Re: [commons-lang] 01/02: Adding Streams.toArray

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Re: [commons-lang] 01/02: Adding Streams.toArray

garydgregory
Hi Jochen,

May you please add Javadocs to the new public types and methods you added:

- org.apache.commons.lang3.Streams.ArrayCollector<O>
- org.apache.commons.lang3.Streams.ArrayCollector.ArrayCollector(Class<O>)

Thank you,
Gary


On Fri, Feb 14, 2020 at 4:38 PM <[hidden email]> wrote:

> This is an automated email from the ASF dual-hosted git repository.
>
> jochen pushed a commit to branch master
> in repository https://gitbox.apache.org/repos/asf/commons-lang.git
>
> commit 83dd32b901dea25a571adcd6a976464c8a36601c
> Author: U-EUR\jwi <[hidden email]>
> AuthorDate: Fri Feb 14 22:25:06 2020 +0100
>
>     Adding Streams.toArray
> ---
>  .../java/org/apache/commons/lang3/Streams.java     | 61
> ++++++++++++++++++++++
>  .../java/org/apache/commons/lang3/StreamsTest.java | 11 ++++
>  2 files changed, 72 insertions(+)
>
> diff --git a/src/main/java/org/apache/commons/lang3/Streams.java
> b/src/main/java/org/apache/commons/lang3/Streams.java
> index c097d76..86edb2f 100644
> --- a/src/main/java/org/apache/commons/lang3/Streams.java
> +++ b/src/main/java/org/apache/commons/lang3/Streams.java
> @@ -16,7 +16,12 @@
>   */
>  package org.apache.commons.lang3;
>
> +import java.lang.reflect.Array;
> +import java.util.ArrayList;
>  import java.util.Collection;
> +import java.util.Collections;
> +import java.util.List;
> +import java.util.Set;
>  import java.util.function.BiConsumer;
>  import java.util.function.BinaryOperator;
>  import java.util.function.Consumer;
> @@ -420,4 +425,60 @@ public class Streams {
>      public static <O> FailableStream<O> stream(Collection<O> pStream) {
>          return stream(pStream.stream());
>      }
> +
> +    public static class ArrayCollector<O> implements Collector<O,
> List<O>, O[]> {
> +        private static final Set<Characteristics> characteristics =
> Collections.emptySet();
> +        private final Class<O> elementType;
> +
> +        public ArrayCollector(Class<O> pElementType) {
> +            elementType = pElementType;
> +        }
> +
> +        @Override
> +        public Supplier<List<O>> supplier() {
> +            return () -> new ArrayList<O>();
> +        }
> +
> +        @Override
> +        public BiConsumer<List<O>, O> accumulator() {
> +            return (list, o) -> {
> +                list.add(o);
> +            };
> +        }
> +
> +        @Override
> +        public BinaryOperator<List<O>> combiner() {
> +            return (left, right) -> {
> +                left.addAll(right);
> +                return left;
> +            };
> +        }
> +
> +        @Override
> +        public Function<List<O>, O[]> finisher() {
> +            return (list) -> {
> +                @SuppressWarnings("unchecked")
> +                final O[] array = (O[]) Array.newInstance(elementType,
> list.size());
> +                return list.toArray(array);
> +            };
> +        }
> +
> +        @Override
> +        public Set<Characteristics> characteristics() {
> +            return characteristics;
> +        }
> +    }
> +
> +    /**
> +     * Returns a {@code Collector} that accumulates the input elements
> into a
> +     * new array.
> +     *
> +     * @param pElementType Type of an element in the array.
> +     * @param <O> the type of the input elements
> +     * @return a {@code Collector} which collects all the input elements
> into an
> +     * array, in encounter order
> +     */
> +    public static <O extends Object> Collector<O, ?, O[]>
> toArray(Class<O> pElementType) {
> +        return new ArrayCollector<O>(pElementType);
> +    }
>  }
> diff --git a/src/test/java/org/apache/commons/lang3/StreamsTest.java
> b/src/test/java/org/apache/commons/lang3/StreamsTest.java
> index 3ce713d..da98397 100644
> --- a/src/test/java/org/apache/commons/lang3/StreamsTest.java
> +++ b/src/test/java/org/apache/commons/lang3/StreamsTest.java
> @@ -17,6 +17,7 @@
>  package org.apache.commons.lang3;
>
>  import static org.junit.jupiter.api.Assertions.assertEquals;
> +import static org.junit.jupiter.api.Assertions.assertNotNull;
>  import static org.junit.jupiter.api.Assertions.assertSame;
>  import static org.junit.jupiter.api.Assertions.fail;
>
> @@ -64,6 +65,16 @@ class StreamsTest {
>          }
>      }
>
> +    @Test
> +    void testToArray() {
> +        final String[] array = Arrays.asList("2", "3",
> "1").stream().collect(Streams.toArray(String.class));
> +        assertNotNull(array);
> +        assertEquals(3, array.length);
> +        assertEquals("2", array[0]);
> +        assertEquals("3", array[1]);
> +        assertEquals("1", array[2]);
> +    }
> +
>      protected <T extends Throwable> FailableConsumer<String, T>
> asIntConsumer(T pThrowable) {
>          return (s) -> {
>              final Integer i = Integer.valueOf(s);
>
>