diff --git a/classlib/src/main/java/org/teavm/classlib/java/util/TTemplateCollections.java b/classlib/src/main/java/org/teavm/classlib/java/util/TTemplateCollections.java index ed7ad959e..d9c36c79f 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/util/TTemplateCollections.java +++ b/classlib/src/main/java/org/teavm/classlib/java/util/TTemplateCollections.java @@ -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 extends AbstractImmutableList implements RandomAccess { + public static class ImmutableArrayList extends AbstractImmutableList implements RandomAccess { private final T[] list; @SafeVarargs - ImmutableArrayList(T... list) { + public ImmutableArrayList(T... list) { this.list = list; } diff --git a/classlib/src/main/java/org/teavm/classlib/java/util/stream/TStream.java b/classlib/src/main/java/org/teavm/classlib/java/util/stream/TStream.java index 5e682638b..0e32bb9f6 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/util/stream/TStream.java +++ b/classlib/src/main/java/org/teavm/classlib/java/util/stream/TStream.java @@ -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 extends TBaseStream> { return new TGenericConcatStream<>(a, b); } } + + @SuppressWarnings("unchecked") + default List toList() { + return (List) new TTemplateCollections.ImmutableArrayList<>(toArray()); + } } diff --git a/tests/src/test/java/org/teavm/classlib/java/util/stream/StreamTest.java b/tests/src/test/java/org/teavm/classlib/java/util/stream/StreamTest.java index 5f88205db..8b65eb717 100644 --- a/tests/src/test/java/org/teavm/classlib/java/util/stream/StreamTest.java +++ b/tests/src/test/java/org/teavm/classlib/java/util/stream/StreamTest.java @@ -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 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 iter = list.iterator(); + iter.next(); + try { + iter.remove(); + fail("UOE expected on iterator.remove"); + } catch (UnsupportedOperationException e) { + // ok + } + } } \ No newline at end of file