From d4f98a57d0a9aa322a4ecca6fb19dda1093925f7 Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Sat, 27 Apr 2024 20:26:22 +0200 Subject: [PATCH] classlib: fix Stream.dropWhile --- .../doubleimpl/TDropWhileDoubleStream.java | 36 +++++++++++++------ .../util/stream/impl/TDropWhileStream.java | 36 +++++++++++++------ .../stream/intimpl/TDropWhileIntStream.java | 36 +++++++++++++------ .../stream/longimpl/TDropWhileLongStream.java | 36 +++++++++++++------ 4 files changed, 100 insertions(+), 44 deletions(-) diff --git a/classlib/src/main/java/org/teavm/classlib/java/util/stream/doubleimpl/TDropWhileDoubleStream.java b/classlib/src/main/java/org/teavm/classlib/java/util/stream/doubleimpl/TDropWhileDoubleStream.java index 05b67e741..ac3cd08d8 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/util/stream/doubleimpl/TDropWhileDoubleStream.java +++ b/classlib/src/main/java/org/teavm/classlib/java/util/stream/doubleimpl/TDropWhileDoubleStream.java @@ -17,28 +17,42 @@ package org.teavm.classlib.java.util.stream.doubleimpl; import java.util.function.DoublePredicate; -public class TDropWhileDoubleStream extends TWrappingDoubleStreamImpl { +public class TDropWhileDoubleStream extends TSimpleDoubleStreamImpl { + private TSimpleDoubleStreamImpl sourceStream; private DoublePredicate predicate; /* set to `true` as soon as we see a value `v` in the source stream for which `predicate.test(v)` is true */ private boolean isStarted; - TDropWhileDoubleStream(TSimpleDoubleStreamImpl innerStream, DoublePredicate predicate) { - super(innerStream); + TDropWhileDoubleStream(TSimpleDoubleStreamImpl sourceStream, DoublePredicate predicate) { + this.sourceStream = sourceStream; this.predicate = predicate; } @Override - protected DoublePredicate wrap(DoublePredicate consumer) { - return t -> { - if (!isStarted) { - if (predicate.test(t)) { - return true; - } else { + public boolean next(DoublePredicate consumer) { + if (!isStarted) { + var skippingPredicate = new DoublePredicate() { + boolean consumerCanTakeMore; + + @Override + public boolean test(double t) { + if (predicate.test(t)) { + return true; + } isStarted = true; + consumerCanTakeMore = consumer.test(t); + return false; } + }; + var result = sourceStream.next(skippingPredicate); + if (!result) { + return false; } - return consumer.test(t); - }; + if (!skippingPredicate.consumerCanTakeMore) { + return true; + } + } + return sourceStream.next(consumer); } } diff --git a/classlib/src/main/java/org/teavm/classlib/java/util/stream/impl/TDropWhileStream.java b/classlib/src/main/java/org/teavm/classlib/java/util/stream/impl/TDropWhileStream.java index c34535f8f..25e01f1cb 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/util/stream/impl/TDropWhileStream.java +++ b/classlib/src/main/java/org/teavm/classlib/java/util/stream/impl/TDropWhileStream.java @@ -17,28 +17,42 @@ package org.teavm.classlib.java.util.stream.impl; import java.util.function.Predicate; -public class TDropWhileStream extends TWrappingStreamImpl { +public class TDropWhileStream extends TSimpleStreamImpl { + private TSimpleStreamImpl sourceStream; private Predicate predicate; /* set to `true` as soon as we see a value `v` in the source stream for which `predicate.test(v)` is true */ private boolean isStarted; - TDropWhileStream(TSimpleStreamImpl innerStream, Predicate predicate) { - super(innerStream); + TDropWhileStream(TSimpleStreamImpl sourceStream, Predicate predicate) { + this.sourceStream = sourceStream; this.predicate = predicate; } @Override - protected Predicate wrap(Predicate consumer) { - return t -> { - if (!isStarted) { - if (predicate.test(t)) { - return true; - } else { + public boolean next(Predicate consumer) { + if (!isStarted) { + var skippingPredicate = new Predicate() { + boolean consumerCanTakeMore; + + @Override + public boolean test(T t) { + if (predicate.test(t)) { + return true; + } isStarted = true; + consumerCanTakeMore = consumer.test(t); + return false; } + }; + var result = sourceStream.next(skippingPredicate); + if (!result) { + return false; } - return consumer.test(t); - }; + if (!skippingPredicate.consumerCanTakeMore) { + return true; + } + } + return sourceStream.next(consumer); } } diff --git a/classlib/src/main/java/org/teavm/classlib/java/util/stream/intimpl/TDropWhileIntStream.java b/classlib/src/main/java/org/teavm/classlib/java/util/stream/intimpl/TDropWhileIntStream.java index b03aace3d..94522096b 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/util/stream/intimpl/TDropWhileIntStream.java +++ b/classlib/src/main/java/org/teavm/classlib/java/util/stream/intimpl/TDropWhileIntStream.java @@ -17,28 +17,42 @@ package org.teavm.classlib.java.util.stream.intimpl; import java.util.function.IntPredicate; -public class TDropWhileIntStream extends TWrappingIntStreamImpl { +public class TDropWhileIntStream extends TSimpleIntStreamImpl { + private TSimpleIntStreamImpl sourceStream; private IntPredicate predicate; /* set to `true` as soon as we see a value `v` in the source stream for which `predicate.test(v)` is true */ private boolean isStarted; - TDropWhileIntStream(TSimpleIntStreamImpl innerStream, IntPredicate predicate) { - super(innerStream); + TDropWhileIntStream(TSimpleIntStreamImpl sourceStream, IntPredicate predicate) { + this.sourceStream = sourceStream; this.predicate = predicate; } @Override - protected IntPredicate wrap(IntPredicate consumer) { - return t -> { - if (!isStarted) { - if (predicate.test(t)) { - return true; - } else { + public boolean next(IntPredicate consumer) { + if (!isStarted) { + var skippingPredicate = new IntPredicate() { + boolean consumerCanTakeMore; + + @Override + public boolean test(int t) { + if (predicate.test(t)) { + return true; + } isStarted = true; + consumerCanTakeMore = consumer.test(t); + return false; } + }; + var result = sourceStream.next(skippingPredicate); + if (!result) { + return false; } - return consumer.test(t); - }; + if (!skippingPredicate.consumerCanTakeMore) { + return true; + } + } + return sourceStream.next(consumer); } } diff --git a/classlib/src/main/java/org/teavm/classlib/java/util/stream/longimpl/TDropWhileLongStream.java b/classlib/src/main/java/org/teavm/classlib/java/util/stream/longimpl/TDropWhileLongStream.java index e822e9b34..e85fc73ec 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/util/stream/longimpl/TDropWhileLongStream.java +++ b/classlib/src/main/java/org/teavm/classlib/java/util/stream/longimpl/TDropWhileLongStream.java @@ -17,28 +17,42 @@ package org.teavm.classlib.java.util.stream.longimpl; import java.util.function.LongPredicate; -public class TDropWhileLongStream extends TWrappingLongStreamImpl { +public class TDropWhileLongStream extends TSimpleLongStreamImpl { + private TSimpleLongStreamImpl sourceStream; private LongPredicate predicate; /* set to `true` as soon as we see a value `v` in the source stream for which `predicate.test(v)` is true */ private boolean isStarted; - TDropWhileLongStream(TSimpleLongStreamImpl innerStream, LongPredicate predicate) { - super(innerStream); + TDropWhileLongStream(TSimpleLongStreamImpl sourceStream, LongPredicate predicate) { + this.sourceStream = sourceStream; this.predicate = predicate; } @Override - protected LongPredicate wrap(LongPredicate consumer) { - return t -> { - if (!isStarted) { - if (predicate.test(t)) { - return true; - } else { + public boolean next(LongPredicate consumer) { + if (!isStarted) { + var skippingPredicate = new LongPredicate() { + boolean consumerCanTakeMore; + + @Override + public boolean test(long t) { + if (predicate.test(t)) { + return true; + } isStarted = true; + consumerCanTakeMore = consumer.test(t); + return false; } + }; + var result = sourceStream.next(skippingPredicate); + if (!result) { + return false; } - return consumer.test(t); - }; + if (!skippingPredicate.consumerCanTakeMore) { + return true; + } + } + return sourceStream.next(consumer); } }