Adding Streams.toArray

This commit is contained in:
U-EUR\jwi 2020-02-14 22:25:06 +01:00
parent 3ce3b27dbd
commit 83dd32b901
2 changed files with 72 additions and 0 deletions

View File

@ -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);
}
}

View File

@ -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);