Add Stream.toList

This commit is contained in:
Alexey Andreev 2022-06-23 10:41:49 +03:00
parent 496efbe7f4
commit 87324a3569
3 changed files with 50 additions and 3 deletions

View File

@ -23,16 +23,16 @@ import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Predicate;
class TTemplateCollections {
public final class TTemplateCollections {
private TTemplateCollections() {
}
static class ImmutableArrayList<T> extends AbstractImmutableList<T> implements RandomAccess {
public static class ImmutableArrayList<T> extends AbstractImmutableList<T> implements RandomAccess {
private final T[] list;
@SafeVarargs
ImmutableArrayList(T... list) {
public ImmutableArrayList(T... list) {
this.list = list;
}

View File

@ -16,6 +16,7 @@
package org.teavm.classlib.java.util.stream;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
@ -29,6 +30,7 @@ import java.util.function.ToDoubleFunction;
import java.util.function.ToIntFunction;
import java.util.function.ToLongFunction;
import java.util.function.UnaryOperator;
import org.teavm.classlib.java.util.TTemplateCollections;
import org.teavm.classlib.java.util.stream.impl.TArrayStreamImpl;
import org.teavm.classlib.java.util.stream.impl.TEmptyStreamImpl;
import org.teavm.classlib.java.util.stream.impl.TGenerateStream;
@ -150,4 +152,9 @@ public interface TStream<T> extends TBaseStream<T, TStream<T>> {
return new TGenericConcatStream<>(a, b);
}
}
@SuppressWarnings("unchecked")
default List<T> toList() {
return (List<T>) new TTemplateCollections.ImmutableArrayList<>(toArray());
}
}

View File

@ -20,6 +20,7 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.teavm.classlib.java.util.stream.Helper.appendNumbersTo;
import static org.teavm.classlib.java.util.stream.Helper.testDoubleStream;
import static org.teavm.classlib.java.util.stream.Helper.testIntStream;
@ -29,6 +30,7 @@ import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Spliterator;
import java.util.function.Function;
import java.util.stream.DoubleStream;
@ -400,4 +402,42 @@ public class StreamTest {
Stream.of(1, 2, 3, 4, 0, 5, 6).takeWhile(i -> i < 4).filter(i -> i % 2 != 0).forEach(sb::append);
assertEquals("13", sb.toString());
}
@Test
public void toList() {
List<Integer> list = Stream.of(1, 2, 3, 4, 5).filter(i -> i % 2 == 0).toList();
assertEquals(Arrays.asList(2, 4), list);
try {
list.add(23);
fail("UOE expected on list.add");
} catch (UnsupportedOperationException e) {
// ok
}
try {
list.set(0, 23);
fail("UOE expected on list.set");
} catch (UnsupportedOperationException e) {
// ok
}
try {
list.remove(0);
fail("UOE expected on list.remove");
} catch (UnsupportedOperationException e) {
// ok
}
try {
list.clear();
fail("UOE expected on list.clear");
} catch (UnsupportedOperationException e) {
// ok
}
Iterator<Integer> iter = list.iterator();
iter.next();
try {
iter.remove();
fail("UOE expected on iterator.remove");
} catch (UnsupportedOperationException e) {
// ok
}
}
}