java.util
Class EnumSet<T,extends,Enum,T>
- Cloneable, Collection<E>, Iterable<E>, Serializable, Set<E>
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
weakly consistent and will not throw a
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
.
EnumSet | clone() - Returns a clone of the set.
|
static | extends Enum> EnumSet allOf(Class eltType) - Returns a set for the given enumeration type where
all the constants are present.
|
static | extends Enum> EnumSet complementOf(EnumSet other) - Returns a set which is the inverse of the supplied set.
|
static | extends Enum> EnumSet copyOf(Collection other) - Creates an
EnumSet using the contents of the given collection.
|
static | extends Enum> EnumSet copyOf(EnumSet other) - Returns a clone of the given set.
|
static | extends Enum> EnumSet noneOf(Class eltType) - Returns a set for the given enumeration type where
none of the constants are present.
|
static | extends Enum> EnumSet of(T first) - Creates a new
EnumSet populated with the given element.
|
static | extends Enum> EnumSet of(T first, T second) - Creates a new
EnumSet populated with the given two elements.
|
static | extends Enum> EnumSet of(T first, T second, T third) - Creates a new
EnumSet populated with the given three elements.
|
static | extends Enum> EnumSet of(T first, T second, T third, T fourth) - Creates a new
EnumSet populated with the given four elements.
|
static | extends Enum> EnumSet of(T first, T second, T third, T fourth, T fifth) - Creates a new
EnumSet populated with the given five elements.
|
static | extends Enum> EnumSet of(T first, T... rest) - Creates a new
EnumSet populated with the given elements.
|
static | extends Enum> EnumSet range(T from, T to) - Creates a new
EnumSet using the enumeration constants
starting from from and ending at to inclusive.
|
T[] toArray , add , addAll , clear , contains , containsAll , isEmpty , iterator , remove , removeAll , retainAll , size , toArray , toString |
clone , equals , extends Object> getClass , finalize , hashCode , notify , notifyAll , toString , wait , wait , wait |
clone
public EnumSet clone()
Returns a clone of the set.
- clone in interface Object
extends Enum> EnumSet allOf
public static extends Enum> EnumSet allOf(Class eltType)
Returns a set for the given enumeration type where
all the constants are present.
eltType
- the type of enumeration to use for the set.
extends Enum> EnumSet complementOf
public static extends Enum> EnumSet complementOf(EnumSet other)
Returns a set which is the inverse of the supplied set.
If a constant is present in the current set, it will not be
present in the new set and vice versa.
other
- the set to provide the complement of.
- an
EnumSet
which is the inverse of the current one.
extends Enum> EnumSet copyOf
public static extends Enum> EnumSet copyOf(Collection other)
Creates an
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.
- an
EnumSet
containing elements from the given collection.
extends Enum> EnumSet copyOf
public static extends Enum> EnumSet copyOf(EnumSet other)
Returns a clone of the given set.
other
- the set to clone.
- an
EnumSet
that is a clone of the given set.
extends Enum> EnumSet noneOf
public static extends Enum> EnumSet noneOf(Class eltType)
Returns a set for the given enumeration type where
none of the constants are present.
eltType
- the type of enumeration to use for the set.
- an
EnumSet
with none of the bits set.
extends Enum> EnumSet of
public static extends Enum> EnumSet of(T first)
Creates a new
EnumSet
populated with the given element.
first
- the element to use to populate the new set.
extends Enum> EnumSet of
public static extends Enum> EnumSet of(T first,
T second)
Creates a new
EnumSet
populated with the given two elements.
first
- the first element to use to populate the new set.second
- the second element to use.
- an
EnumSet
containing the elements.
extends Enum> EnumSet of
public static extends Enum> EnumSet of(T first,
T second,
T third)
Creates a new
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.
- an
EnumSet
containing the elements.
extends Enum> EnumSet of
public static extends Enum> EnumSet of(T first,
T second,
T third,
T fourth)
Creates a new
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.
- an
EnumSet
containing the elements.
extends Enum> EnumSet of
public static extends Enum> EnumSet of(T first,
T second,
T third,
T fourth,
T fifth)
Creates a new
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.
- an
EnumSet
containing the elements.
extends Enum> EnumSet of
public static extends Enum> EnumSet of(T first,
T... rest)
Creates a new
EnumSet
populated with the given elements.
first
- the first element to use to populate the new set.rest
- the other elements to use.
- an
EnumSet
containing the elements.
extends Enum> EnumSet range
public static extends Enum> EnumSet range(T from,
T to)
Creates a new
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
.
- an
EnumSet
containing the specified range of elements.
EnumSet.java - Set of enum objects
Copyright (C) 2004, 2005 Free Software Foundation, Inc.
This file is part of GNU Classpath.
GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING. If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.
Linking this library statically or dynamically with other modules is
making a combined work based on this library. Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.
As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module. An independent module is a module which is not derived from
or based on this library. If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so. If you do not wish to do so, delete this
exception statement from your version.