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 theList
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
-
-
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 void
clear()
Removes all elements from the collection.boolean
equals(java.lang.Object o)
E
get(int index)
Retrieves the item at the given index.java.util.Comparator<E>
getComparator()
Always returnsnull
because there can never be aComparator
set to change from the "natural" ordering of theEnum
.java.lang.Class<E>
getEnumClass()
int
getLength()
Returns the length of the list.ListenerList<ListListener<E>>
getListListeners()
Returns the list listener list.int
hashCode()
int
indexOf(E item)
Returns the index of an item in the sequence.boolean
isEmpty()
Tests the emptiness of the collection.java.util.Iterator<E>
iterator()
void
setComparator(java.util.Comparator<E> comparator)
Unsupported because the list is always ordered in the "natural" order of the backingEnum
.E[]
toArray()
java.lang.String
toString()
-
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:Collection
Removes all elements from the collection.- Specified by:
clear
in interfaceCollection<E extends java.lang.Enum<E>>
- Specified by:
clear
in interfaceList<E extends java.lang.Enum<E>>
- See Also:
ListListener.itemsRemoved(List, int, Sequence)
-
get
public E get(int index)
Description copied from interface:Sequence
Retrieves the item at the given index.
-
indexOf
public int indexOf(E item)
Description copied from interface:Sequence
Returns the index of an item in the sequence.
-
isEmpty
public boolean isEmpty()
Description copied from interface:Collection
Tests the emptiness of the collection.- Specified by:
isEmpty
in interfaceCollection<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 interfaceList<E extends java.lang.Enum<E>>
- Specified by:
getLength
in 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 returnsnull
because there can never be aComparator
set to change from the "natural" ordering of theEnum
.- Specified by:
getComparator
in interfaceCollection<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 backingEnum
.- Specified by:
setComparator
in interfaceCollection<E extends java.lang.Enum<E>>
- Specified by:
setComparator
in 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:List
Returns the list listener list.- Specified by:
getListListeners
in interfaceList<E extends java.lang.Enum<E>>
- Returns:
- The list of listeners for this list.
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
equals
public boolean equals(java.lang.Object o)
- Overrides:
equals
in classjava.lang.Object
-
hashCode
public int hashCode()
- Overrides:
hashCode
in classjava.lang.Object
-
-