com.codepoetics.protonpack.TakeWhileSpliterator Maven / Gradle / Ivy
package com.codepoetics.protonpack;
import java.util.Comparator;
import java.util.Spliterator;
import java.util.function.Consumer;
import java.util.function.Predicate;
class TakeWhileSpliterator implements Spliterator {
static TakeWhileSpliterator over(Spliterator source, Predicate condition) {
return new TakeWhileSpliterator<>(source, condition);
}
private final Spliterator source;
private final Predicate condition;
private boolean conditionHolds = true;
private TakeWhileSpliterator(Spliterator source, Predicate condition) {
this.source = source;
this.condition = condition;
}
@Override
public boolean tryAdvance(Consumer super T> action) {
return conditionHolds && source.tryAdvance(e -> {
if (conditionHolds = condition.test(e)) {
action.accept(e);
}
});
}
@Override
public Spliterator trySplit() {
return null;
}
@Override
public long estimateSize() {
return conditionHolds ? source.estimateSize() : 0;
}
@Override
public int characteristics() {
return source.characteristics() &~ Spliterator.SIZED;
}
@Override
public Comparator super T> getComparator() {
return source.getComparator();
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy