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

com.fasterxml.aalto.util.EmptyIterator Maven / Gradle / Ivy

There is a newer version: 1.3.3
Show newest version
package com.fasterxml.aalto.util;

import java.util.Iterator;
import java.util.NoSuchElementException;

/**
 * Simple implementation of "null iterator", iterator that has nothing to
 * iterate over.
 */
public final class EmptyIterator implements Iterator
{
    final static EmptyIterator sInstance = new EmptyIterator();

    private EmptyIterator() { }

    /**
     * Since the actual type has no effect (as this iterator
     * never returns any value objects), we can just cast away
     * here: bit unclean, but safe.
     */
    @SuppressWarnings("unchecked")
    public static  EmptyIterator getInstance() {
        return (EmptyIterator) sInstance;
    }

    @Override
    public boolean hasNext() { return false; }

    @Override
    public T next() {
        throw new NoSuchElementException();
    }

    @Override
    public void remove() {
        // could as well throw IllegalOperationException...
        throw new IllegalStateException();
    }
}