Skip to content

Commit

Permalink
Add toArray function
Browse files Browse the repository at this point in the history
  • Loading branch information
aNNiMON committed Feb 16, 2016
1 parent 726d913 commit e3e9067
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 13 deletions.
40 changes: 29 additions & 11 deletions src/main/java/com/annimon/stream/Stream.java
Original file line number Diff line number Diff line change
Expand Up @@ -563,10 +563,7 @@ public int compare(T o1, T o2) {
* @return the new stream
*/
public Stream<T> sorted(Comparator<? super T> comparator) {
final List<T> list = new ArrayList<T>();
while (iterator.hasNext()) {
list.add(iterator.next());
}
final List<T> list = collectToList();
Collections.sort(list, comparator);
return new Stream<T>(list);
}
Expand Down Expand Up @@ -723,6 +720,24 @@ public Optional<T> reduce(BiFunction<T, T, T> accumulator) {
}
return foundAny ? Optional.of(result) : (Optional<T>) Optional.empty();
}

/**
* Collects elements to an array.
*
* <p>This is a terminal operation.
*
* @return the result of collect elements
* @see #toArray(com.annimon.stream.function.IntFunction)
*/
public Object[] toArray() {
return toArray(new IntFunction<Object[]>() {

@Override
public Object[] apply(int value) {
return new Object[value];
}
});
}

/**
* Collects elements to an array, the {@code generator} constructor of provided.
Expand All @@ -732,14 +747,9 @@ public Optional<T> reduce(BiFunction<T, T, T> accumulator) {
* @param <R> the type of the result
* @param generator the array constructor reference that accommodates future array of assigned size
* @return the result of collect elements
* @see #collect(com.annimon.stream.Collector)
* @see #collect(com.annimon.stream.function.Supplier, com.annimon.stream.function.BiConsumer)
*/
public <R> R[] collect(IntFunction<R[]> generator) {
Collection<T> container = new ArrayList<T>();
while (iterator.hasNext()) {
container.add(iterator.next());
}
public <R> R[] toArray(IntFunction<R[]> generator) {
final List<T> container = collectToList();
final int size = container.size();

if (size >= MAX_ARRAY_SIZE) throw new IllegalArgumentException(BAD_SIZE);
Expand Down Expand Up @@ -927,5 +937,13 @@ static <E> E[] newArray(int length, E... array) {
return Arrays.copyOf(array, length);
}

private List<T> collectToList() {
final List<T> container = new ArrayList<T>();
while (iterator.hasNext()) {
container.add(iterator.next());
}
return container;
}

//</editor-fold>
}
15 changes: 13 additions & 2 deletions src/test/java/com/annimon/stream/StreamTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -740,10 +740,21 @@ public void testFindFirstAfterFiltering() {
}

@Test
public void testCollectToArrayTerminationOperation() {
public void testToArray() {
Object[] objects = Stream.ofRange(0, 200)
.filter(Functions.remainder(4))
.toArray();

assertEquals(50, objects.length);
assertNotNull(objects[10]);
assertThat(objects[0], instanceOf(Integer.class));
}

@Test
public void testToArrayWithGenerator() {
Integer[] numbers = Stream.ofRange(1, 1000)
.filter(Functions.remainder(2))
.collect(Functions.arrayGenerator(Integer[].class));
.toArray(Functions.arrayGenerator(Integer[].class));

assertTrue(numbers.length > 0);
assertNotNull(numbers[100]);
Expand Down

0 comments on commit e3e9067

Please sign in to comment.