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 */
013package org.jikesrvm.util;
014
015import java.util.Enumeration;
016import java.util.NoSuchElementException;
017
018/**
019 * Implements an empty enumeration.<p>
020 *
021 * The Collections class from Java 7 provides an {@code emptyEnumeration()} method
022 * which will make this class unnecessary once we switch to Java 7.<p>
023 *
024 * TODO Remove this class when we require Java 7 for building and from
025 * our class libraries.
026 *
027 * @param <E> type parameter
028 */
029public final class EmptyEnumeration<E> implements Enumeration<E> {
030
031  @SuppressWarnings("unchecked")
032  private static final EmptyEnumeration<?> INSTANCE = new EmptyEnumeration();
033
034  /**
035   * Non-instantiable. Use {@link #emptyEnumeration()} to
036   * create instances.
037   */
038  private EmptyEnumeration() {
039    // prevent instantiation
040  }
041
042  @Override
043  public boolean hasMoreElements() {
044    return false;
045  }
046
047  @Override
048  public E nextElement() {
049    throw new NoSuchElementException("The empty enumeration has no elements!");
050  }
051
052  @SuppressWarnings("unchecked")
053  public static <T> Enumeration<T> emptyEnumeration() {
054    return (EmptyEnumeration<T>) INSTANCE;
055  }
056
057}