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

com.robrua.orianna.store.CloseableIterator Maven / Gradle / Ivy

There is a newer version: 2.4.6
Show newest version
package com.robrua.orianna.store;

import java.util.Collections;
import java.util.Iterator;

/**
 * An iterator that may need to be closed
 *
 * @author Rob Rua ([email protected])
 */
public abstract class CloseableIterator implements Iterator, AutoCloseable {
    /**
     * @param 
     *            the type to iterate
     * @return an empty iterator
     */
    @SuppressWarnings("unchecked")
    public static  CloseableIterator emptyIterator() {
        return fromIterator((Iterator)Collections.emptyIterator());
    }

    /**
     * @param 
     *            the type to iterate
     * @param iterator
     *            the non-closeable iterator to wrap
     * @return a wrapped version of the iterator
     */
    public static  CloseableIterator fromIterator(final Iterator iterator) {
        return new CloseableIterator() {

            @Override
            public void close() throws Exception {
                // Do nothing
            }

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

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

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy