Class BitFieldSet<E extends Enum<E>>
- All Implemented Interfaces:
Serializable, Cloneable, Iterable<E>, Collection<E>, Set<E>
Modification allows access and manipulation of the bit mask for the elements so this class can be easily converted between long/int and BitFieldSet to use as efficient option flags in implementation but convenient enum sets for manipulation.
If the Enum implements BitField then each field can have 1..N bits up to a maximum total of 64 bits per enum.
The class provides methods for setting and getting values from these fields as long, int, short or byte values, either signed or unsigned.
- Since:
- 1.5
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate classBitFieldSet.EnumBitFieldIterator<E extends Enum<E>>private classBitFieldSet.EnumBitSetIterator<E extends Enum<E>>private static classBitFieldSet.SerializationProxy<E extends Enum<E>>This class is used to serialize all EnumSet instances, regardless of implementation type.(package private) static class -
Field Summary
FieldsModifier and TypeFieldDescription(package private) final long[]All bit masks for each field since some can span more than one(package private) longBit vector representation of this set.The class of all the elements of this set.private static final long(package private) final inttotal number of bits used by all fields(package private) final E[]All values comprising T(package private) static final Enum<?>[] -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleanAdds the specified element to this set if it is not already present.final booleanbooleanbooleanbooleanboolean(package private) voidaddAll()booleanaddAll(Collection<? extends E> c) Adds all of the elements in the specified collection to this set.(package private) voidbooleanall(long mask) static booleanall(long flags, long mask) booleanfinal booleanbooleanbooleanbooleanbooleanlongstatic <E extends Enum<E>>
BitFieldSet<E> Creates an enum set containing all of the elements in the specified element type.booleanandNotMask(long mask) static longandNotMask(long flags, long mask) booleanany(long mask) static booleanany(long flags, long mask) booleanfinal booleanbooleanbooleanbooleanbooleanvoidclear()Removes all of the elements from this set.clone()Returns a copy of this set.voidstatic <E extends Enum<E>>
BitFieldSet<E> complementOf(BitFieldSet<E> s) Creates an enum set with the same element type as the specified enum set, initially containing all the elements of this type that are not contained in the specified set.booleanReturns true if this set contains the specified element.booleancontainsAll(Collection<?> c) Returns true if this set contains all of the elements in the specified collection.static <E extends Enum<E>>
BitFieldSet<E> copyOf(BitFieldSet<E> s) Creates an enum set with the same element type as the specified enum set, initially containing the same elements (if any).static <E extends Enum<E>>
BitFieldSet<E> copyOf(Collection<E> c) Creates an enum set initialized from the specified collection.booleanCompares the specified object with this set for equality.longReturns unsigned value for the field, except if the field is 64 bitsstatic <E extends Enum<E>>
bytegetBitField(byte elements, E e1) static <E extends Enum<E>>
intgetBitField(int elements, E e1) static <E extends Enum<E>>
longgetBitField(long elements, E e1) Returns signed value for the field, except if the field is 64 bitsstatic <E extends Enum<E>>
shortgetBitField(short elements, E e1) static <E extends Enum<E>>
long[]getBitMasks(Class<E> elementType) Returns all of the values comprising E.byteintlongReturns signed value for the field, except if the field is 64 bitsshortlong(package private) static <E extends Enum<E>>
longgetSignedBitField(long elements, E e1, int maxBits, String typeName) static intgetTotalBits(long[] bitMasks) byteintstatic <E extends Enum<E>>
E[]getUniverse(Class<E> elementType) Returns all of the values comprising E.longgetUnsigned(E e1, int maxBits, String typeName) static <E extends Enum<E>>
longgetUnsignedBitField(long elements, E e1, int maxBits, String typeName) Returns unsigned value for the field, except if the field is 64 bitsshortstatic <E extends Enum<E>>
intintMask(E e1) booleanisEmpty()iterator()Returns an iterator over the elements contained in this set.static <E extends Enum<E>>
longlongMask(E e1) longfinal longlonglonglonglongstatic longnextBitMask(int nextAvailableBit, int bits) booleannone(long mask) static booleannone(long flags, long mask) booleanfinal booleanbooleanbooleanbooleanbooleanstatic <E extends Enum<E>>
BitFieldSet<E> Creates an empty enum set with the specified element type.static <E extends Enum<E>>
BitFieldSet<E> of(E e) Creates an enum set initially containing the specified element.static <E extends Enum<E>>
BitFieldSet<E> of(E e1, E e2) Creates an enum set initially containing the specified elements.static <E extends Enum<E>>
BitFieldSet<E> of(E first, E... rest) Creates an enum set initially containing the specified elements.static <E extends Enum<E>>
BitFieldSet<E> of(E e1, E e2, E e3) Creates an enum set initially containing the specified elements.static <E extends Enum<E>>
BitFieldSet<E> of(E e1, E e2, E e3, E e4) Creates an enum set initially containing the specified elements.static <E extends Enum<E>>
BitFieldSet<E> of(E e1, E e2, E e3, E e4, E e5) Creates an enum set initially containing the specified elements.static <E extends Enum<E>>
BitFieldSet<E> Creates an enum set initially containing the specified elements.static <T extends Enum<T>>
BitFieldSet<T> Create a bit enum set from a bit maskbooleanorMask(long mask) static longorMask(long flags, long mask) static <E extends Enum<E>>
BitFieldSet<E> range(E from, E to) Creates an enum set initially containing all of the elements in the range defined by the two specified endpoints.private voidreadObject(ObjectInputStream stream) final booleanbooleanbooleanbooleanbooleanbooleanRemoves the specified element from this set if it is present.booleanremoveAll(Collection<?> c) Removes from this set all of its elements that are contained in the specified collection.booleanreplaceAll(long mask) Deprecated.booleanretainAll(Collection<?> c) Retains only the elements in this set that are contained in the specified collection.booleansetAll(long mask) static <E extends Enum<E>>
bytesetBitField(byte elements, E e1, byte value) static <E extends Enum<E>>
intsetBitField(int elements, E e1, int value) static <E extends Enum<E>>
longsetBitField(long elements, E e1, int value) static <E extends Enum<E>>
shortsetBitField(short elements, E e1, short value) voidsetBitField(E e1, byte value) voidsetBitField(E e1, int value) voidsetBitField(E e1, long value) voidsetBitField(E e1, short value) (package private) static <E extends Enum<E>>
longsetSigned(long elements, E e1, long value) Set a signed value for the fieldbooleanSet a signed value for the field(package private) static <E extends Enum<E>>
longSet a signed value for the field(package private) static <E extends Enum<E>>
longsetUnsigned(long elements, E e1, long value) Set an unsigned value for the fieldbooleansetUnsigned(E e1, long value) Set a signed value for the field(package private) static <E extends Enum<E>>
longsetUnsigned(Class<E> elementType, long[] bitMasks, long elements, E e1, long value) Set an unsigned value for the fieldvoidsetUnsignedField(E e1, byte value) voidsetUnsignedField(E e1, int value) voidsetUnsignedField(E e1, long value) voidsetUnsignedField(E e1, short value) intsize()Returns the number of elements in this set.bytetoByte()inttoInt()longtoLong()shorttoShort()toString()(package private) final voidThrows an exception if e is not of the correct type for this enum set.(package private) ObjectMethods inherited from class AbstractSet
hashCodeMethods inherited from class AbstractCollection
toArray, toArrayMethods inherited from interface Collection
parallelStream, removeIf, stream, toArrayMethods inherited from interface Set
spliterator, toArray, toArray
-
Field Details
-
serialVersionUID
private static final long serialVersionUID- See Also:
-
elements
long elementsBit vector representation of this set. The 2^k bit indicates the presence of universe[k] in this set. -
elementType
-
universe
-
bitMasks
final long[] bitMasksAll bit masks for each field since some can span more than one -
totalBits
final int totalBitstotal number of bits used by all fields -
ZERO_LENGTH_ENUM_ARRAY
-
-
Constructor Details
-
BitFieldSet
-
-
Method Details
-
nextBitMask
public static long nextBitMask(int nextAvailableBit, int bits) -
getUniverse
Returns all of the values comprising E. The result is cloned and slower than SharedSecrets use but works in Java 11 and Java 8 because SharedSecrets are not shared publicly- Type Parameters:
E- type of enum- Parameters:
elementType- class of enum- Returns:
- array of enum values
-
getBitMasks
Returns all of the values comprising E. The result is cloned and slower than SharedSecrets use but works in Java 11 and Java 8 because SharedSecrets are not shared publicly- Type Parameters:
E- type of enum- Parameters:
elementType- class of enum- Returns:
- array of bit masks for enum values
-
getTotalBits
public static int getTotalBits(long[] bitMasks) -
addRange
-
addAll
void addAll() -
complement
public void complement() -
toLong
public long toLong() -
toInt
public int toInt() -
toShort
public short toShort() -
toByte
public byte toByte() -
allBitsMask
public long allBitsMask() -
orMask
public boolean orMask(long mask) -
replaceAll
Deprecated.usesetAll(long)Set all bit fields to values in mask- Parameters:
mask- bit fields values- Returns:
- true if any field values were modified
-
setAll
public boolean setAll(long mask) -
toString
-
andNotMask
public boolean andNotMask(long mask) -
any
public boolean any(long mask) -
none
public boolean none(long mask) -
all
public boolean all(long mask) -
longMask
-
intMask
-
get
Returns unsigned value for the field, except if the field is 64 bits- Parameters:
e1- field to get- Returns:
- unsigned value
-
setUnsigned
Set a signed value for the field- Parameters:
e1- fieldvalue- value to set- Returns:
- true if elements changed by operation
-
setSigned
Set a signed value for the field- Parameters:
e1- fieldvalue- value to set- Returns:
- true if elements changed by operation
-
setBitField
-
setBitField
-
setBitField
-
setBitField
-
setUnsignedField
-
setUnsignedField
-
setUnsignedField
-
setUnsignedField
-
getUnsigned
-
getSigned
-
getLong
Returns signed value for the field, except if the field is 64 bits- Parameters:
e1- field to get- Returns:
- unsigned value
-
getInt
-
getShort
-
getByte
-
getUInt
-
getUShort
-
getUByte
-
orMask
public static long orMask(long flags, long mask) -
andNotMask
public static long andNotMask(long flags, long mask) -
any
public static boolean any(long flags, long mask) -
all
public static boolean all(long flags, long mask) -
none
public static boolean none(long flags, long mask) -
mask
-
mask
-
mask
-
mask
-
mask
-
mask
-
add
-
add
-
add
-
add
-
add
-
remove
-
remove
-
remove
-
remove
-
remove
-
any
-
any
-
any
-
any
-
any
-
any
-
all
-
all
-
all
-
all
-
all
-
all
-
none
-
none
-
none
-
none
-
none
-
none
-
iterator
Returns an iterator over the elements contained in this set. The iterator traverses the elements in their natural order (which is the order in which the enum constants are declared). The returned Iterator is a "snapshot" iterator that will never throwConcurrentModificationException; the elements are traversed as they existed when this call was invoked.NOTE: bit field iteration requires skipping fields whose bits are all 0 so constant time is violated
- Specified by:
iteratorin interfaceCollection<E extends Enum<E>>- Specified by:
iteratorin interfaceIterable<E extends Enum<E>>- Specified by:
iteratorin interfaceSet<E extends Enum<E>>- Specified by:
iteratorin classAbstractCollection<E extends Enum<E>>- Returns:
- an iterator over the elements contained in this set
-
size
-
isEmpty
-
contains
Returns true if this set contains the specified element.- Specified by:
containsin interfaceCollection<E extends Enum<E>>- Specified by:
containsin interfaceSet<E extends Enum<E>>- Overrides:
containsin classAbstractCollection<E extends Enum<E>>- Parameters:
e- element to be checked for containment in this collection- Returns:
- true if this set contains the specified element
-
add
Adds the specified element to this set if it is not already present.- Specified by:
addin interfaceCollection<E extends Enum<E>>- Specified by:
addin interfaceSet<E extends Enum<E>>- Overrides:
addin classAbstractCollection<E extends Enum<E>>- Parameters:
e- element to be added to this set- Returns:
- true if the set changed as a result of the call
- Throws:
NullPointerException- if e is null
-
remove
Removes the specified element from this set if it is present.- Specified by:
removein interfaceCollection<E extends Enum<E>>- Specified by:
removein interfaceSet<E extends Enum<E>>- Overrides:
removein classAbstractCollection<E extends Enum<E>>- Parameters:
e- element to be removed from this set, if present- Returns:
- true if the set contained the specified element
-
containsAll
Returns true if this set contains all of the elements in the specified collection.- Specified by:
containsAllin interfaceCollection<E extends Enum<E>>- Specified by:
containsAllin interfaceSet<E extends Enum<E>>- Overrides:
containsAllin classAbstractCollection<E extends Enum<E>>- Parameters:
c- collection to be checked for containment in this set- Returns:
- true if this set contains all of the elements in the specified collection
- Throws:
NullPointerException- if the specified collection is null
-
addAll
Adds all of the elements in the specified collection to this set.- Specified by:
addAllin interfaceCollection<E extends Enum<E>>- Specified by:
addAllin interfaceSet<E extends Enum<E>>- Overrides:
addAllin classAbstractCollection<E extends Enum<E>>- Parameters:
c- collection whose elements are to be added to this set- Returns:
- true if this set changed as a result of the call
- Throws:
NullPointerException- if the specified collection or any of its elements are null
-
removeAll
Removes from this set all of its elements that are contained in the specified collection.- Specified by:
removeAllin interfaceCollection<E extends Enum<E>>- Specified by:
removeAllin interfaceSet<E extends Enum<E>>- Overrides:
removeAllin classAbstractSet<E extends Enum<E>>- Parameters:
c- elements to be removed from this set- Returns:
- true if this set changed as a result of the call
- Throws:
NullPointerException- if the specified collection is null
-
retainAll
Retains only the elements in this set that are contained in the specified collection.- Specified by:
retainAllin interfaceCollection<E extends Enum<E>>- Specified by:
retainAllin interfaceSet<E extends Enum<E>>- Overrides:
retainAllin classAbstractCollection<E extends Enum<E>>- Parameters:
c- elements to be retained in this set- Returns:
- true if this set changed as a result of the call
- Throws:
NullPointerException- if the specified collection is null
-
clear
-
of
public static <T extends Enum<T>> BitFieldSet<T> of(@NotNull @NotNull Class<T> enumClass, long mask) Create a bit enum set from a bit mask- Type Parameters:
T- enum type- Parameters:
enumClass- class of the enummask- bit mask for items- Returns:
- bit enum set
-
clone
Returns a copy of this set. -
typeCheck
Throws an exception if e is not of the correct type for this enum set. -
writeReplace
Object writeReplace() -
readObject
- Throws:
InvalidObjectException
-
equals
Compares the specified object with this set for equality. Returns true if the given object is also a set, the two sets have the same size, and every member of the given set is contained in this set.- Specified by:
equalsin interfaceCollection<E extends Enum<E>>- Specified by:
equalsin interfaceSet<E extends Enum<E>>- Overrides:
equalsin classAbstractSet<E extends Enum<E>>- Parameters:
o- object to be compared for equality with this set- Returns:
- true if the specified object is equal to this set
-
noneOf
Creates an empty enum set with the specified element type.- Type Parameters:
E- The class of the elements in the set- Parameters:
elementType- the class object of the element type for this enum set- Returns:
- An empty enum set of the specified type.
- Throws:
NullPointerException- if elementType is null
-
setSigned
Set a signed value for the field- Parameters:
e1- fieldvalue- value to set
-
setSigned
-
setUnsigned
Set an unsigned value for the field- Parameters:
e1- fieldvalue- value to set
-
setUnsigned
-
setBitField
-
setBitField
-
setBitField
-
setBitField
-
getUnsignedBitField
public static <E extends Enum<E>> long getUnsignedBitField(long elements, E e1, int maxBits, String typeName) Returns unsigned value for the field, except if the field is 64 bits- Type Parameters:
E- type of enum- Parameters:
elements- bit mask for elementse1- field to getmaxBits- maximum bits for typetypeName- name of type- Returns:
- unsigned value
-
getSignedBitField
-
getBitField
Returns signed value for the field, except if the field is 64 bits- Type Parameters:
E- type of enum- Parameters:
elements- bit mask for elementse1- field to get- Returns:
- unsigned value
-
getBitField
-
getBitField
-
getBitField
-
allOf
Creates an enum set containing all of the elements in the specified element type.- Type Parameters:
E- The class of the elements in the set- Parameters:
elementType- the class object of the element type for this enum set- Returns:
- An enum set containing all the elements in the specified type.
- Throws:
NullPointerException- if elementType is null
-
copyOf
Creates an enum set with the same element type as the specified enum set, initially containing the same elements (if any).- Type Parameters:
E- The class of the elements in the set- Parameters:
s- the enum set from which to initialize this enum set- Returns:
- A copy of the specified enum set.
- Throws:
NullPointerException- if s is null
-
copyOf
Creates an enum set initialized from the specified collection. If the specified collection is an BitFieldSet instance, this static factory method behaves identically tocopyOf(BitFieldSet). Otherwise, the specified collection must contain at least one element (in order to determine the new enum set's element type).- Type Parameters:
E- The class of the elements in the collection- Parameters:
c- the collection from which to initialize this enum set- Returns:
- An enum set initialized from the given collection.
- Throws:
IllegalArgumentException- if c is not an BitFieldSet instance and contains no elementsNullPointerException- if c is null
-
complementOf
Creates an enum set with the same element type as the specified enum set, initially containing all the elements of this type that are not contained in the specified set.- Type Parameters:
E- The class of the elements in the enum set- Parameters:
s- the enum set from whose complement to initialize this enum set- Returns:
- The complement of the specified set in this set
- Throws:
NullPointerException- if s is null
-
of
Creates an enum set initially containing the specified element.Overloads of this method exist to initialize an enum set with one through five elements. A sixth overloading is provided that uses the varargs feature. This overloading may be used to create an enum set initially containing an arbitrary number of elements, but is likely to run slower than the overloads that do not use varargs.
- Type Parameters:
E- The class of the specified element and of the set- Parameters:
e- the element that this set is to contain initially- Returns:
- an enum set initially containing the specified element
- Throws:
NullPointerException- if e is null
-
of
Creates an enum set initially containing the specified elements.Overloads of this method exist to initialize an enum set with one through five elements. A sixth overloading is provided that uses the varargs feature. This overloading may be used to create an enum set initially containing an arbitrary number of elements, but is likely to run slower than the overloads that do not use varargs.
- Type Parameters:
E- The class of the parameter elements and of the set- Parameters:
e1- an element that this set is to contain initiallye2- another element that this set is to contain initially- Returns:
- an enum set initially containing the specified elements
- Throws:
NullPointerException- if any parameters are null
-
of
Creates an enum set initially containing the specified elements.Overloads of this method exist to initialize an enum set with one through five elements. A sixth overloading is provided that uses the varargs feature. This overloading may be used to create an enum set initially containing an arbitrary number of elements, but is likely to run slower than the overloads that do not use varargs.
- Type Parameters:
E- The class of the parameter elements and of the set- Parameters:
e1- an element that this set is to contain initiallye2- another element that this set is to contain initiallye3- another element that this set is to contain initially- Returns:
- an enum set initially containing the specified elements
- Throws:
NullPointerException- if any parameters are null
-
of
Creates an enum set initially containing the specified elements.Overloads of this method exist to initialize an enum set with one through five elements. A sixth overloading is provided that uses the varargs feature. This overloading may be used to create an enum set initially containing an arbitrary number of elements, but is likely to run slower than the overloads that do not use varargs.
- Type Parameters:
E- The class of the parameter elements and of the set- Parameters:
e1- an element that this set is to contain initiallye2- another element that this set is to contain initiallye3- another element that this set is to contain initiallye4- another element that this set is to contain initially- Returns:
- an enum set initially containing the specified elements
- Throws:
NullPointerException- if any parameters are null
-
of
Creates an enum set initially containing the specified elements.Overloads of this method exist to initialize an enum set with one through five elements. A sixth overloading is provided that uses the varargs feature. This overloading may be used to create an enum set initially containing an arbitrary number of elements, but is likely to run slower than the overloads that do not use varargs.
- Type Parameters:
E- The class of the parameter elements and of the set- Parameters:
e1- an element that this set is to contain initiallye2- another element that this set is to contain initiallye3- another element that this set is to contain initiallye4- another element that this set is to contain initiallye5- another element that this set is to contain initially- Returns:
- an enum set initially containing the specified elements
- Throws:
NullPointerException- if any parameters are null
-
of
Creates an enum set initially containing the specified elements. This factory, whose parameter list uses the varargs feature, may be used to create an enum set initially containing an arbitrary number of elements, but it is likely to run slower than the overloads that do not use varargs.- Type Parameters:
E- The class of the parameter elements and of the set- Parameters:
first- an element that the set is to contain initiallyrest- the remaining elements the set is to contain initially- Returns:
- an enum set initially containing the specified elements
- Throws:
NullPointerException- if any of the specified elements are null, or if rest is null
-
of
public static <E extends Enum<E>> BitFieldSet<E> of(@NotNull @NotNull Class<E> declaringClass, E[] rest) Creates an enum set initially containing the specified elements. This factory, whose parameter list uses the varargs feature, may be used to create an enum set initially containing an arbitrary number of elements, but it is likely to run slower than the overloads that do not use varargs.- Type Parameters:
E- The class of the parameter elements and of the set- Parameters:
declaringClass- declaring class of enumrest- the remaining elements the set is to contain initially- Returns:
- an enum set initially containing the specified elements
- Throws:
NullPointerException- if any of the specified elements are null, or if rest is null
-
range
Creates an enum set initially containing all of the elements in the range defined by the two specified endpoints. The returned set will contain the endpoints themselves, which may be identical but must not be out of order.- Type Parameters:
E- The class of the parameter elements and of the set- Parameters:
from- the first element in the rangeto- the last element in the range- Returns:
- an enum set initially containing all of the elements in the range defined by the two specified endpoints
- Throws:
NullPointerException- iffromortoare nullIllegalArgumentException- iffrom.compareTo(to) > 0
-
setAll(long)