Class EnumList<E extends java.lang.Enum<E>>

  • Type Parameters:
    E - The underlying enum type that backs this list.
    All Implemented Interfaces:
    java.io.Serializable, java.lang.Iterable<E>, Collection<E>, List<E>, Sequence<E>

    public class EnumList<E extends java.lang.Enum<E>>
    extends ReadOnlySequence<E>
    implements List<E>, java.io.Serializable
    A read-only implementation of the List interface that is backed by an enum.

    An EnumList cannot be modified once constructed and only ever contains all the enum constant values defined in the class. As such, the "add" and "remove" (and related) methods throw exceptions.

    This class is meant to facilitate using enum constants as elements in a dropdown list (for instance). A useful way to do this is to override the "toString()" method of the enum to provide a human-readable version of the enum constant value, which will then appear in the UI.

    See Also:
    Serialized Form
    • Constructor Summary

      Constructors 
      Constructor Description
      EnumList​(java.lang.Class<E> enumClassValue)
      Construct the full list populated by the enum constants of the given class.
    • Constructor Detail

      • EnumList

        public EnumList​(java.lang.Class<E> enumClassValue)
        Construct the full list populated by the enum constants of the given class.
        Parameters:
        enumClassValue - The enum class whose constant values are used to fully populate the list.
    • Method Detail

      • getEnumClass

        public java.lang.Class<E> getEnumClass()
        Returns:
        The class of the elements in this set.
      • get

        public E get​(int index)
        Description copied from interface: Sequence
        Retrieves the item at the given index.
        Specified by:
        get in interface Sequence<E extends java.lang.Enum<E>>
        Parameters:
        index - The index of the item to retrieve.
        Returns:
        The item at this index in the sequence.
      • indexOf

        public int indexOf​(E item)
        Description copied from interface: Sequence
        Returns the index of an item in the sequence.
        Specified by:
        indexOf in interface Sequence<E extends java.lang.Enum<E>>
        Parameters:
        item - The item to locate.
        Returns:
        The index of first occurrence of the item if it exists in the sequence; -1, otherwise.
      • isEmpty

        public boolean isEmpty()
        Description copied from interface: Collection
        Tests the emptiness of the collection.
        Specified by:
        isEmpty in interface Collection<E extends java.lang.Enum<E>>
        Returns:
        true if the collection contains no elements; false, otherwise.
      • getLength

        public int getLength()
        Description copied from interface: List
        Returns the length of the list.
        Specified by:
        getLength in interface List<E extends java.lang.Enum<E>>
        Specified by:
        getLength in interface Sequence<E extends java.lang.Enum<E>>
        Returns:
        The number of items in the list, or -1 if the list's length is not known. In this case, the iterator must be used to retrieve the contents of the list.
      • toArray

        public E[] toArray()
        Returns:
        An array of the elements in this set.
      • getComparator

        public java.util.Comparator<E> getComparator()
        Always returns null because there can never be a Comparator set to change from the "natural" ordering of the Enum.
        Specified by:
        getComparator in interface Collection<E extends java.lang.Enum<E>>
        Returns:
        null always.
        See Also:
        Collection.setComparator(Comparator)
      • setComparator

        @UnsupportedOperation
        public void setComparator​(java.util.Comparator<E> comparator)
        Unsupported because the list is always ordered in the "natural" order of the backing Enum.
        Specified by:
        setComparator in interface Collection<E extends java.lang.Enum<E>>
        Specified by:
        setComparator in interface List<E extends java.lang.Enum<E>>
        Parameters:
        comparator - The comparator used to order elements in the collection, or null if the collection is unsorted.
        Throws:
        java.lang.UnsupportedOperationException - always.
        See Also:
        ListListener.comparatorChanged(List, Comparator)
      • iterator

        public java.util.Iterator<E> iterator()
        Specified by:
        iterator in interface java.lang.Iterable<E extends java.lang.Enum<E>>
      • getListListeners

        public ListenerList<ListListener<E>> getListListeners()
        Description copied from interface: List
        Returns the list listener list.
        Specified by:
        getListListeners in interface List<E extends java.lang.Enum<E>>
        Returns:
        The list of listeners for this list.
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • equals

        public boolean equals​(java.lang.Object o)
        Overrides:
        equals in class java.lang.Object
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.Object