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

com.happy3w.toolkits.iterator.EndWhenIterator Maven / Gradle / Ivy

There is a newer version: 1.2.0
Show newest version
package com.happy3w.toolkits.iterator;

import com.happy3w.java.ext.NeedFindIterator;
import com.happy3w.java.ext.NullableOptional;

import java.util.Iterator;
import java.util.function.Predicate;

public class EndWhenIterator extends NeedFindIterator implements IEasyIterator {
    private final Iterator innerIterator;
    private final Predicate endPredicate;

    public EndWhenIterator(Iterator innerIterator, Predicate endPredicate) {
        this.innerIterator = innerIterator;
        this.endPredicate = endPredicate;
    }

    @Override
    protected NullableOptional findNext() {
        if (innerIterator.hasNext()) {
            T next = innerIterator.next();
            if (!endPredicate.test(next)) {
                return NullableOptional.of(next);
            }
        }
        return NullableOptional.empty();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy