All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.codepoetics.protonpack.TakeWhileSpliterator Maven / Gradle / Ivy

There is a newer version: 1.16
Show newest version
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 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 getComparator() {
        return source.getComparator();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy