Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
 package com.robrua.orianna.store;
 
An iterator that may need to be closed

Author(s):
Rob Rua (robrua@alumni.cmu.edu)
public abstract class CloseableIterator<T> implements Iterator<T>, AutoCloseable {
    

Parameters:
<T> the type to iterate
Returns:
an empty iterator
    @SuppressWarnings("unchecked")
    public static <T> CloseableIterator<T> emptyIterator() {
        return fromIterator((Iterator<T>)Collections.emptyIterator());
    }

    

Parameters:
<T> the type to iterate
iterator the non-closeable iterator to wrap
Returns:
a wrapped version of the iterator
    public static <T> CloseableIterator<T> fromIterator(final Iterator<T> iterator) {
        return new CloseableIterator<T>() {
            @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();
            }
        };
    }
New to GrepCode? Check out our FAQ X