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

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

There is a newer version: 5.3.1
Show newest version
package nl.vpro.util;

import java.util.Iterator;
import java.util.Optional;

import org.checkerframework.checker.nullness.qual.NonNull;

/**
 * @author Michiel Meeuwissen
 * @since 1.3
 */
public  abstract class WrappedIterator implements CountedIterator {

    protected final CloseableIterator wrapped;

    public WrappedIterator(Iterator wrapped) {
        this.wrapped = CloseableIterator.of(wrapped);
    }

    @Override
    public boolean hasNext() {
        return iterator().hasNext();
    }

    @Override
    public abstract S next();

    @Override
    public void remove() {
        iterator().remove();
    }

    protected Iterator iterator() {
        return wrapped;
    }


    @SuppressWarnings({"unchecked", "rawtypes"})
    @Override
    public @NonNull Optional getSize() {
        if (wrapped instanceof CountedIterator) {
            return ((CountedIterator) wrapped).getSize();
        }
        return Optional.empty();

    }

    @SuppressWarnings("rawtypes")
    @Override
    public Long getCount() {
        if (wrapped instanceof CountedIterator) {
            return ((CountedIterator) wrapped).getCount();
        }
        throw new UnsupportedOperationException();
    }

    @SuppressWarnings({"unchecked", "rawtypes"})
    @Override
    public @NonNull Optional getTotalSize() {
        if (wrapped instanceof CountedIterator) {
            return ((CountedIterator) wrapped).getTotalSize();
        }
        return Optional.empty();

    }

    @Override
    public void close() throws Exception {
        wrapped.close();
    }


    @Override
    public String toString() {
        return "Counted[" + wrapped  + "]";
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy