Package org.apache.pivot.collections
Class EnumList<E extends java.lang.Enum<E>>
- java.lang.Object
-
- org.apache.pivot.collections.ReadOnlySequence<E>
-
- org.apache.pivot.collections.EnumList<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 theListinterface that is backed by an enum.An
EnumListcannot 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
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface org.apache.pivot.collections.List
List.ListListenerList<T>
-
Nested classes/interfaces inherited from interface org.apache.pivot.collections.Sequence
Sequence.Tree<T>
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclear()Removes all elements from the collection.booleanequals(java.lang.Object o)Eget(int index)Retrieves the item at the given index.java.util.Comparator<E>getComparator()Always returnsnullbecause there can never be aComparatorset to change from the "natural" ordering of theEnum.java.lang.Class<E>getEnumClass()intgetLength()Returns the length of the list.ListenerList<ListListener<E>>getListListeners()Returns the list listener list.inthashCode()intindexOf(E item)Returns the index of an item in the sequence.booleanisEmpty()Tests the emptiness of the collection.java.util.Iterator<E>iterator()voidsetComparator(java.util.Comparator<E> comparator)Unsupported because the list is always ordered in the "natural" order of the backingEnum.E[]toArray()java.lang.StringtoString()-
Methods inherited from class org.apache.pivot.collections.ReadOnlySequence
add, defaultException, insert, remove, remove, update
-
-
-
-
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.
-
clear
@UnsupportedOperation public void clear()
Description copied from interface:CollectionRemoves all elements from the collection.- Specified by:
clearin interfaceCollection<E extends java.lang.Enum<E>>- Specified by:
clearin interfaceList<E extends java.lang.Enum<E>>- See Also:
ListListener.itemsRemoved(List, int, Sequence)
-
get
public E get(int index)
Description copied from interface:SequenceRetrieves the item at the given index.
-
indexOf
public int indexOf(E item)
Description copied from interface:SequenceReturns the index of an item in the sequence.
-
isEmpty
public boolean isEmpty()
Description copied from interface:CollectionTests the emptiness of the collection.- Specified by:
isEmptyin interfaceCollection<E extends java.lang.Enum<E>>- Returns:
trueif the collection contains no elements;false, otherwise.
-
getLength
public int getLength()
Description copied from interface:ListReturns the length of the list.- Specified by:
getLengthin interfaceList<E extends java.lang.Enum<E>>- Specified by:
getLengthin interfaceSequence<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 returnsnullbecause there can never be aComparatorset to change from the "natural" ordering of theEnum.- Specified by:
getComparatorin interfaceCollection<E extends java.lang.Enum<E>>- Returns:
nullalways.- 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 backingEnum.- Specified by:
setComparatorin interfaceCollection<E extends java.lang.Enum<E>>- Specified by:
setComparatorin interfaceList<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()
-
getListListeners
public ListenerList<ListListener<E>> getListListeners()
Description copied from interface:ListReturns the list listener list.- Specified by:
getListListenersin interfaceList<E extends java.lang.Enum<E>>- Returns:
- The list of listeners for this list.
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
equals
public boolean equals(java.lang.Object o)
- Overrides:
equalsin classjava.lang.Object
-
hashCode
public int hashCode()
- Overrides:
hashCodein classjava.lang.Object
-
-