nl.vpro.util.CountedMaxOffsetIterator Maven / Gradle / Ivy
package nl.vpro.util;
import lombok.Singular;
import java.util.List;
import java.util.Optional;
import java.util.function.Predicate;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
/**
* @author Michiel Meeuwissen
* @since 2.23
*/
public class CountedMaxOffsetIterator extends MaxOffsetIterator implements CountedIterator {
private final CountedIterator wrappedCountedIterator;
@lombok.Builder(builderClassName = "Builder", builderMethodName = "_countedBuilder")
private CountedMaxOffsetIterator(
@NonNull CountedIterator wrapped,
@Nullable Number max,
@Nullable Number offset,
@Nullable @Singular List callbacks,
@Nullable Predicate countPredicate,
boolean autoClose) {
super(wrapped, max, offset, countPredicate, true, callbacks, autoClose);
this.wrappedCountedIterator = wrapped;
}
@Override
public @NonNull Optional getSize() {
return wrappedCountedIterator.getSize().map(i -> Math.min(i - getOffset(), max));
}
@Override
public Long getCount() {
return wrappedCountedIterator.getCount() - getOffset();
}
@Override
public @NonNull Optional getTotalSize() {
return wrappedCountedIterator.getTotalSize();
}
}