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

nl.vpro.util.CountedMaxOffsetIterator Maven / Gradle / Ivy

There is a newer version: 5.3.1
Show newest version
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();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy