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;
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 -> {
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;
} else {
}
isStarted = true;
consumerCanTakeMore = consumer.test(t);
return false;
}
}
return consumer.test(t);
};
var result = sourceStream.next(skippingPredicate);
if (!result) {
return false;
}
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;
public class TDropWhileStream<T> extends TWrappingStreamImpl<T, T> {
public class TDropWhileStream<T> extends TSimpleStreamImpl<T> {
private TSimpleStreamImpl<T> sourceStream;
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 */
private boolean isStarted;
TDropWhileStream(TSimpleStreamImpl<T> innerStream, Predicate<? super T> predicate) {
super(innerStream);
TDropWhileStream(TSimpleStreamImpl<T> sourceStream, Predicate<? super T> predicate) {
this.sourceStream = sourceStream;
this.predicate = predicate;
}
@Override
protected Predicate<T> wrap(Predicate<? super T> consumer) {
return t -> {
public boolean next(Predicate<? super T> consumer) {
if (!isStarted) {
var skippingPredicate = new Predicate<T>() {
boolean consumerCanTakeMore;
@Override
public boolean test(T t) {
if (predicate.test(t)) {
return true;
} else {
}
isStarted = true;
consumerCanTakeMore = consumer.test(t);
return false;
}
}
return consumer.test(t);
};
var result = sourceStream.next(skippingPredicate);
if (!result) {
return false;
}
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;
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 -> {
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;
} else {
}
isStarted = true;
consumerCanTakeMore = consumer.test(t);
return false;
}
}
return consumer.test(t);
};
var result = sourceStream.next(skippingPredicate);
if (!result) {
return false;
}
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;
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 -> {
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;
} else {
}
isStarted = true;
consumerCanTakeMore = consumer.test(t);
return false;
}
}
return consumer.test(t);
};
var result = sourceStream.next(skippingPredicate);
if (!result) {
return false;
}
if (!skippingPredicate.consumerCanTakeMore) {
return true;
}
}
return sourceStream.next(consumer);
}
}