public abstract class EnumSet4D<T extends java.lang.Enum<T>>
extends java.util.AbstractSet<T>
implements java.lang.Cloneable
Provides an efficient mechanism for recording a set of enumeration constants. As enumerations have a known set of possible values, certain assumptions can be made when creating a set of constants. The maximum size of the set will always be equal to the number of constants, and each value will always be one of these constants. As a result, the set only needs to store whether a particular constant is present or not rather than the values themselves. Each constant can thus be represented by a single bit.
This class is designed to provide an alternative to using integer bit flags
by providing a typesafe Collection
interface with an underlying
implementation that utilises the assumptions above to give an equivalent level
of efficiency. The values in a EnumSet
must all be from the same
Enum
type, which allows the contents to be packed into a bit vector.
A containment test is then simply a matter of inspecting the appropriate bit, while
addition involves setting the same. Such basic operations take place in constant
time.
The Iterator
implementation traverses the values in the natural order
of the enumeration provided by each constant's Enum#ordinal()
. It is
ConcurrentModificationException
.
This means that concurrent changes to the set may or may not be noticeable during
traversal.
As is usual with most collections, the set is not synchronized by default. This
can be remedied by using the Collections.synchronizedSet(Set)
method. Null
elements are not supported and attempts to add one will throw a NullPointerException
.
Modifier and Type | Method and Description |
---|---|
static <T extends java.lang.Enum<T>> |
allOf(java.lang.Class<T> eltType)
Returns a set for the given enumeration type where
all the constants are present.
|
EnumSet4D<T> |
clone()
Returns a clone of the set.
|
static <T extends java.lang.Enum<T>> |
complementOf(EnumSet4D<T> other)
Returns a set which is the inverse of the supplied set.
|
static <T extends java.lang.Enum<T>> |
copyOf(java.util.Collection<T> other)
Creates an
EnumSet using the contents of the given collection. |
static <T extends java.lang.Enum<T>> |
copyOf(java.util.EnumSet<T> other)
Returns a clone of the given set.
|
static <T extends java.lang.Enum<T>> |
noneOf(java.lang.Class<T> eltType)
Returns a set for the given enumeration type where
none of the constants are present.
|
static <T extends java.lang.Enum<T>> |
of(T first)
Creates a new
EnumSet populated with the given element. |
static <T extends java.lang.Enum<T>> |
of(T first,
T... rest)
Creates a new
EnumSet populated with the given elements. |
static <T extends java.lang.Enum<T>> |
of(T first,
T second)
Creates a new
EnumSet populated with the given two elements. |
static <T extends java.lang.Enum<T>> |
of(T first,
T second,
T third)
Creates a new
EnumSet populated with the given three elements. |
static <T extends java.lang.Enum<T>> |
of(T first,
T second,
T third,
T fourth)
Creates a new
EnumSet populated with the given four elements. |
static <T extends java.lang.Enum<T>> |
of(T first,
T second,
T third,
T fourth,
T fifth)
Creates a new
EnumSet populated with the given five elements. |
static <T extends java.lang.Enum<T>> |
range(T from,
T to)
Creates a new
EnumSet using the enumeration constants
starting from from and ending at to inclusive. |
add, addAll, clear, contains, containsAll, isEmpty, iterator, remove, retainAll, size, toArray, toArray, toString
finalize, getClass, notify, notifyAll, wait, wait, wait
public EnumSet4D<T> clone()
clone
in class java.lang.Object
public static <T extends java.lang.Enum<T>> EnumSet4D<T> allOf(java.lang.Class<T> eltType)
eltType
- the type of enumeration to use for the set.EnumSet
with all the bits set.java.lang.NullPointerException
- if the element type is null
.public static <T extends java.lang.Enum<T>> EnumSet4D<T> noneOf(java.lang.Class<T> eltType)
eltType
- the type of enumeration to use for the set.EnumSet
with none of the bits set.java.lang.NullPointerException
- if the element type is null
.public static <T extends java.lang.Enum<T>> java.util.EnumSet<T> copyOf(java.util.EnumSet<T> other)
other
- the set to clone.EnumSet
that is a clone of the given set.java.lang.NullPointerException
- if other
is null
.public static <T extends java.lang.Enum<T>> EnumSet4D<T> copyOf(java.util.Collection<T> other)
EnumSet
using the contents of the given collection.
If the collection is also an EnumSet
, this method works the
same as copyOf(EnumSet)
. Otherwise, the elements of the collection
are inspected and used to populate the new set.other
- the collection to use to populate the new set.EnumSet
containing elements from the given collection.java.lang.NullPointerException
- if other
is null
.java.lang.IllegalArgumentException
- if the collection is empty.public static <T extends java.lang.Enum<T>> EnumSet4D<T> complementOf(EnumSet4D<T> other)
other
- the set to provide the complement of.EnumSet
which is the inverse of the current one.java.lang.NullPointerException
- if other
is null
.public static <T extends java.lang.Enum<T>> EnumSet4D<T> of(T first)
EnumSet
populated with the given element.first
- the element to use to populate the new set.EnumSet
containing the element.java.lang.NullPointerException
- if first
is null
.public static <T extends java.lang.Enum<T>> EnumSet4D<T> of(T first, T second)
EnumSet
populated with the given two elements.first
- the first element to use to populate the new set.second
- the second element to use.EnumSet
containing the elements.java.lang.NullPointerException
- if any of the parameters are null
.public static <T extends java.lang.Enum<T>> EnumSet4D<T> of(T first, T second, T third)
EnumSet
populated with the given three elements.first
- the first element to use to populate the new set.second
- the second element to use.third
- the third element to use.EnumSet
containing the elements.java.lang.NullPointerException
- if any of the parameters are null
.public static <T extends java.lang.Enum<T>> EnumSet4D<T> of(T first, T second, T third, T fourth)
EnumSet
populated with the given four elements.first
- the first element to use to populate the new set.second
- the second element to use.third
- the third element to use.fourth
- the fourth element to use.EnumSet
containing the elements.java.lang.NullPointerException
- if any of the parameters are null
.public static <T extends java.lang.Enum<T>> EnumSet4D<T> of(T first, T second, T third, T fourth, T fifth)
EnumSet
populated with the given five elements.first
- the first element to use to populate the new set.second
- the second element to use.third
- the third element to use.fourth
- the fourth element to use.fifth
- the fifth element to use.EnumSet
containing the elements.java.lang.NullPointerException
- if any of the parameters are null
.public static <T extends java.lang.Enum<T>> EnumSet4D<T> of(T first, T... rest)
EnumSet
populated with the given elements.first
- the first element to use to populate the new set.rest
- the other elements to use.EnumSet
containing the elements.java.lang.NullPointerException
- if any of the parameters are null
.public static <T extends java.lang.Enum<T>> EnumSet4D<T> range(T from, T to)
EnumSet
using the enumeration constants
starting from from
and ending at to
inclusive.
The two may be the same, but they must be in the correct order.
So giving the first constant twice would give a set with just that
constant set, while supplying the first and second constant will give
a set with those two elements. However, specifying the second as
the from
element followed by an earlier element as the
to
element will result in an error.from
- the element to start from.to
- the element to end at (may be the same as from
.EnumSet
containing the specified range of elements.java.lang.NullPointerException
- if any of the parameters are null
.java.lang.IllegalArgumentException
- if first.compareTo(last) > 0
.