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

com.annimon.stream.iterator.LazyIterator Maven / Gradle / Ivy

The newest version!
package com.annimon.stream.iterator;

import java.util.Iterator;

/**
 * Lazily gets {@code Iterator} from the given {@link Iterable}.
 * 
 * @param  the type of the elements
 */
public class LazyIterator implements Iterator {
    private final Iterable iterable;
    private Iterator iterator;

    public LazyIterator(Iterable iterable) {
        this.iterable = iterable;
    }

    private void ensureIterator() {
        if (iterator != null) {
            return;
        }
        // Lazily creates Iterator object.
        iterator = iterable.iterator();
    }

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

    @Override
    public T next() {
        ensureIterator();
        return iterator.next();
    }

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




© 2015 - 2025 Weber Informatics LLC | Privacy Policy