classlib: fix Stream.dropWhile

This commit is contained in:
Alexey Andreev 2024-04-27 20:26:22 +02:00
parent 31674f9744
commit d4f98a57d0
4 changed files with 100 additions and 44 deletions

View File

@ -17,28 +17,42 @@ package org.teavm.classlib.java.util.stream.doubleimpl;
import java.util.function.DoublePredicate; import java.util.function.DoublePredicate;
public class TDropWhileDoubleStream extends TWrappingDoubleStreamImpl { public class TDropWhileDoubleStream extends TSimpleDoubleStreamImpl {
private TSimpleDoubleStreamImpl sourceStream;
private DoublePredicate predicate; 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 */ /* 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; private boolean isStarted;
TDropWhileDoubleStream(TSimpleDoubleStreamImpl innerStream, DoublePredicate predicate) { TDropWhileDoubleStream(TSimpleDoubleStreamImpl sourceStream, DoublePredicate predicate) {
super(innerStream); this.sourceStream = sourceStream;
this.predicate = predicate; this.predicate = predicate;
} }
@Override @Override
protected DoublePredicate wrap(DoublePredicate consumer) { public boolean next(DoublePredicate consumer) {
return t -> { if (!isStarted) {
if (!isStarted) { var skippingPredicate = new DoublePredicate() {
if (predicate.test(t)) { boolean consumerCanTakeMore;
return true;
} else { @Override
public boolean test(double t) {
if (predicate.test(t)) {
return true;
}
isStarted = 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);
} }
} }

View File

@ -17,28 +17,42 @@ package org.teavm.classlib.java.util.stream.impl;
import java.util.function.Predicate; import java.util.function.Predicate;
public class TDropWhileStream<T> extends TWrappingStreamImpl<T, T> { public class TDropWhileStream<T> extends TSimpleStreamImpl<T> {
private TSimpleStreamImpl<T> sourceStream;
private Predicate<? super T> predicate; private Predicate<? super T> predicate;
/* set to `true` as soon as we see a value `v` in the source stream for which `predicate.test(v)` is true */ /* 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; private boolean isStarted;
TDropWhileStream(TSimpleStreamImpl<T> innerStream, Predicate<? super T> predicate) { TDropWhileStream(TSimpleStreamImpl<T> sourceStream, Predicate<? super T> predicate) {
super(innerStream); this.sourceStream = sourceStream;
this.predicate = predicate; this.predicate = predicate;
} }
@Override @Override
protected Predicate<T> wrap(Predicate<? super T> consumer) { public boolean next(Predicate<? super T> consumer) {
return t -> { if (!isStarted) {
if (!isStarted) { var skippingPredicate = new Predicate<T>() {
if (predicate.test(t)) { boolean consumerCanTakeMore;
return true;
} else { @Override
public boolean test(T t) {
if (predicate.test(t)) {
return true;
}
isStarted = 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);
} }
} }

View File

@ -17,28 +17,42 @@ package org.teavm.classlib.java.util.stream.intimpl;
import java.util.function.IntPredicate; import java.util.function.IntPredicate;
public class TDropWhileIntStream extends TWrappingIntStreamImpl { public class TDropWhileIntStream extends TSimpleIntStreamImpl {
private TSimpleIntStreamImpl sourceStream;
private IntPredicate predicate; 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 */ /* 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; private boolean isStarted;
TDropWhileIntStream(TSimpleIntStreamImpl innerStream, IntPredicate predicate) { TDropWhileIntStream(TSimpleIntStreamImpl sourceStream, IntPredicate predicate) {
super(innerStream); this.sourceStream = sourceStream;
this.predicate = predicate; this.predicate = predicate;
} }
@Override @Override
protected IntPredicate wrap(IntPredicate consumer) { public boolean next(IntPredicate consumer) {
return t -> { if (!isStarted) {
if (!isStarted) { var skippingPredicate = new IntPredicate() {
if (predicate.test(t)) { boolean consumerCanTakeMore;
return true;
} else { @Override
public boolean test(int t) {
if (predicate.test(t)) {
return true;
}
isStarted = 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);
} }
} }

View File

@ -17,28 +17,42 @@ package org.teavm.classlib.java.util.stream.longimpl;
import java.util.function.LongPredicate; import java.util.function.LongPredicate;
public class TDropWhileLongStream extends TWrappingLongStreamImpl { public class TDropWhileLongStream extends TSimpleLongStreamImpl {
private TSimpleLongStreamImpl sourceStream;
private LongPredicate predicate; 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 */ /* 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; private boolean isStarted;
TDropWhileLongStream(TSimpleLongStreamImpl innerStream, LongPredicate predicate) { TDropWhileLongStream(TSimpleLongStreamImpl sourceStream, LongPredicate predicate) {
super(innerStream); this.sourceStream = sourceStream;
this.predicate = predicate; this.predicate = predicate;
} }
@Override @Override
protected LongPredicate wrap(LongPredicate consumer) { public boolean next(LongPredicate consumer) {
return t -> { if (!isStarted) {
if (!isStarted) { var skippingPredicate = new LongPredicate() {
if (predicate.test(t)) { boolean consumerCanTakeMore;
return true;
} else { @Override
public boolean test(long t) {
if (predicate.test(t)) {
return true;
}
isStarted = 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);
} }
} }