001    /*
002     *  This file is part of the Jikes RVM project (http://jikesrvm.org).
003     *
004     *  This file is licensed to You under the Eclipse Public License (EPL);
005     *  You may not use this file except in compliance with the License. You
006     *  may obtain a copy of the License at
007     *
008     *      http://www.opensource.org/licenses/eclipse-1.0.php
009     *
010     *  See the COPYRIGHT.txt file distributed with this work for information
011     *  regarding copyright ownership.
012     */
013    package org.jikesrvm.util;
014    
015    import java.util.Iterator;
016    import java.util.NoSuchElementException;
017    
018    /**
019     * A generic iterator containing no items.<p>
020     *
021     * NOTE: This class is only necessary until Java 7. Java 7's Collections
022     * class provides an {@code emptyIterator()} method.<p>
023     *
024     * TODO Remove this class when we require Java 7 to build and all supported
025     * class libraries support Java 7.
026     */
027    public final class EmptyIterator<T> implements Iterator<T> {
028    
029      @SuppressWarnings("unchecked")
030      private static final EmptyIterator<?> INSTANCE = new EmptyIterator();
031    
032      /**
033       * Clients must use {@link #getInstance()} to obtain an instance.
034       */
035      private EmptyIterator() {
036      }
037    
038      @Override
039      public boolean hasNext() {
040        return false;
041      }
042    
043      @Override
044      public T next() {
045        throw new NoSuchElementException();
046      }
047    
048      @Override
049      public void remove() {
050        throw new IllegalStateException();
051      }
052    
053      @SuppressWarnings("unchecked")
054      public static <U> Iterator<U> getInstance() {
055        return (Iterator<U>) INSTANCE;
056      }
057    }