Class SingletonEnumeration<E>

  • Type Parameters:
    E - The type of the element in this enumeration.
    All Implemented Interfaces:
    java.util.Enumeration<E>

    @Immutable
    public class SingletonEnumeration<E>
    extends java.lang.Object
    implements java.util.Enumeration<E>
    A simple implementation of the Enumeration interface that contains a single element.

    This enumeration can only return the singleton element once, after which it will return false for subsequent calls to hasMoreElements() and throw a NoSuchElementException on subsequent calls to nextElement().

    Example Usage

    
     String item = "Hello";
     SingletonEnumeration<String> enumeration = new SingletonEnumeration<>(item);
    
     while (enumeration.hasMoreElements()) {
         System.out.println(enumeration.nextElement());
     }
     

    The output of the example would be:

     Hello
     
    Since:
    1.0.0
    Author:
    Mercy
    • Constructor Detail

      • SingletonEnumeration

        public SingletonEnumeration​(E element)
    • Method Detail

      • hasMoreElements

        public boolean hasMoreElements()
        Specified by:
        hasMoreElements in interface java.util.Enumeration<E>
      • nextElement

        public E nextElement()
        Specified by:
        nextElement in interface java.util.Enumeration<E>