E
- The Enum-Type.public interface EnumBitSetHelper<E extends Enum<E> & EnumBitSetHelper<E>> extends Comparable<E>, Serializable
EnumBitSet
.
A set of such enum values can be interpreted as an enum bit set
, which can be
stored to a bit field (i.e. an integer field in a database).
Since this is an interface it does not change the state of the element it is used on or any parameters passed to a method. Instead it always returns a newly created object. I.e. all methods that take an EnumBitSet return a new EnumBitSet. If you wish to alter such a set you can simply use the methods of that set.
Examples for usage:
User permissions:
public enum Role { READ, WRITE, EXECUTE }
BigInteger permissions = MyDBAccess.getUserPermissions(currentUser);
permissions = Role.READ.removedFrom(permissions);
MyDBAccess.setUserPermissions(currentUser, permissions);
Planetary composition:
public enum Planet { MERCURY, VENUS, EARTH, ... }
public enum Element { H, He, Li, Be, B, C, N, ... }
final Map<Planet, EnumBitSet<Element>> composition = new HashMap>>();
final EnumBitSet<Element> mercury = Element.O.union(Element.Na, Element.H, Element.He, Element.K);
composition.put(Planet.MERCURY, mercury);
Counterexample:public enum Status { SUBMITTED, REVIEWED, ACCEPTED, PUBLISHED }
Each status includes all previous ones (can't be reviewed if not submitted). A regular integer field is enough to store the status (e.g.REVIEWED.ordinal()
).
Java 8 Beta has still some bugs. It does not allow assert statements and
@SafeVarargs
in interfaces.
Modifier and Type | Method and Description |
---|---|
default BigInteger |
bitmask()
Bitmask for
this . |
default long |
bitmask64()
64 bit bitmask for
this . |
default boolean |
elementOf(BigInteger bitmask)
Returns whether this value is set in the given bitmask.
|
default boolean |
elementOf(BitSet bitset)
Returns whether this value is set in the given bitset.
|
default boolean |
elementOf(Collection<E> collection)
Returns whether this enum can be found in the given collection.
|
default boolean |
elementOf(Enum<E>... set)
Returns whether this enum constant can be found in the array of constants of the same type.
|
default boolean |
elementOf(long bitmask64)
Returns whether this value is set in the given bitmask.
|
default BigInteger |
intersect(BigInteger mask)
Bit mask with all other bits removed.
|
default BitSet |
intersect(BitSet set)
Bit mask with all other bits removed.
|
default EnumBitSet<E> |
intersect(E... set)
Creates a set with all other elements removed.
|
default EnumBitSet<E> |
intersect(EnumBitSet<E> set)
Creates a set with all other elements removed.
|
default EnumSet<E> |
intersect(EnumSet<E> set)
Bit mask with all other bits removed.
|
default long |
intersect(long mask)
Bit mask with all other bits removed.
|
String |
name()
Returns the name of this enum constant, exactly as declared in its enum declaration.
|
int |
ordinal() |
default EnumBitSet<E> |
others()
Returns a set of all elements except
this . |
default BigInteger |
removedFrom(BigInteger mask)
Removes this from the given mask.
|
default BitSet |
removedFrom(BitSet set)
Removes this from the BitSet and returns the new BitSet.
|
default EnumBitSet<E> |
removedFrom(E... set)
Creates a new EnumSet with
this removed. |
default EnumBitSet<E> |
removedFrom(EnumBitSet<E> set)
Creates a new EnumBitSet with
this removed. |
default EnumSet<E> |
removedFrom(EnumSet<E> set)
Creates a new EnumSet with
this removed. |
default long |
removedFrom(long mask)
Removes this from the given mask.
|
default BigInteger |
toBigInteger()
Bit mask for
this . |
default BitSet |
toBitSet()
Returns a BitSet with all bits set to 0, except the bit representing
this . |
default EnumBitSet<E> |
toEnumBitSet()
Returns a set containing nothing but
this . |
default EnumSet<E> |
toEnumSet()
Returns a set containing nothing but
this . |
default long |
toLong()
Bitmask for
this . |
default BigInteger |
union(BigInteger mask)
Takes the bitmasks of
this and mask , then applies logical OR. |
default BitSet |
union(BitSet set)
Creates a new EnumSet with
this added. |
default EnumBitSet<E> |
union(E... set)
Creates an EnumBitSet containing this and all other elements.
|
default EnumBitSet<E> |
union(EnumBitSet<E> set)
Takes the bitmasks of
this and a clone of mask , then applies logical
OR. |
default EnumSet<E> |
union(EnumSet<E> set)
Creates a new EnumSet with
this added. |
default long |
union(long mask)
Creates a bit mask with
this included. |
default BigInteger |
xor(BigInteger mask)
This can be used to switch one bit in a bit mask.
|
default EnumBitSet<E> |
xor(EnumBitSet<E> set)
Removes or adds this to the given set.
|
compareTo
default BigInteger bitmask()
this
. The value is based on the ordinal.1<<this.ordinal()
toEnumBitSet()
,
toBitSet()
,
toEnumSet()
default long bitmask64() throws MoreThan64ElementsException
this
. The value is based on the ordinal.1<<this.ordinal()
MoreThan64ElementsException
- If more than 64 constants are in the enum type then a long
is not
enough. The exception is only thrown if this element is not one of the first 64
elements.bitmask()
,
toEnumBitSet()
,
toBitSet()
,
toEnumSet()
default boolean elementOf(BigInteger bitmask)
bitmask
- A bitmask.default boolean elementOf(BitSet bitset)
bitset
- A bit set.default boolean elementOf(Collection<E> collection)
This is equivalent to: coll.contains(this);
collection
- A collection, not null.true
, iff this
can be found in collection
.default boolean elementOf(Enum<E>... set)
set
- A set of enum elements, all non-null and of the same enum type.true
, if this
can be found.default boolean elementOf(long bitmask64) throws MoreThan64ElementsException
Note: This does not check if the given mask is valid for the enum type of this element. It may have more bits set than this enum type has constants.
bitmask64
- A bitmask.MoreThan64ElementsException
- if this element is not one of the first 64 elements.default BigInteger intersect(BigInteger mask)
mask
- A bit mask, must be positive.mask.and(this.bitmask())
elementOf(BigInteger)
default BitSet intersect(BitSet set)
set
- A bit set.mask & this
elementOf(BitSet)
default EnumBitSet<E> intersect(E... set)
set
- A set.set & this.ordinal()
elementOf(BitSet)
default EnumBitSet<E> intersect(EnumBitSet<E> set)
set
- A set.set.clone() & this
elementOf(BigInteger)
default EnumSet<E> intersect(EnumSet<E> set)
set
- A set.mask.clone() & this
elementOf(BigInteger)
default long intersect(long mask) throws MoreThan64ElementsException
Note: This does not check if the given mask is valid for the enum type of this element. It may have more bits set than this enum type has constants.
mask
- A bit mask.mask & this.bitmask64()
MoreThan64ElementsException
- The enum type must not contain more than 64 elements.elementOf(long)
String name()
Enum.name()
int ordinal()
Enum.ordinal()
default EnumBitSet<E> others()
this
.
This is equivalent to: EnumBitSet.just(this).complement()
EnumSet
with all elements except this
.default BigInteger removedFrom(BigInteger mask)
mask
- A bit mask, must be positive.mask.andNot(this.bitmask())
default BitSet removedFrom(BitSet set)
set
- A set that may contain this
.set \ this
default EnumBitSet<E> removedFrom(E... set)
this
removed.set
- A set that may contain this
.EnumSet
with all given enums, except this
. A copy of the set
is returned even if this
is not present.default EnumBitSet<E> removedFrom(EnumBitSet<E> set)
this
removed.set
- A set that may contain this
.EnumBitSet
with all elements of the set, except this
. A copy
of the set is returned even if this
is not present.default EnumSet<E> removedFrom(EnumSet<E> set)
this
removed.set
- A set that may contain this
.EnumSet
with all elements of the set, except this
. A copy of
the set is returned even if this
is not present.default long removedFrom(long mask) throws MoreThan64ElementsException
Note: This does not check if the given mask is valid for the enum type of this element. It may have more bits set than this enum type has constants.
mask
- A bit mask.mask & ~this.bitmask64()
MoreThan64ElementsException
- if this element is not one of the first 64 elements.default BigInteger toBigInteger()
this
. The value is based on the ordinal. This is actually the same as
bitmask()
.this.bitmask()
default BitSet toBitSet()
this
.EnumSet
containing this
.default EnumBitSet<E> toEnumBitSet()
this
. EnumBitSet.just(X)
is equal to X.asEnumBitSet()
EnumSet
containing this
.default EnumSet<E> toEnumSet()
this
.EnumSet
containing this
.default long toLong() throws MoreThan64ElementsException
this
. The value is based on the ordinal. This is actually the same as
bitmask64()
.this.bitmask64()
MoreThan64ElementsException
- If more than 64 constants are in the enum type then a long
is not
enough. The exception is only thrown if this element is not one of the first 64
elements.bitmask64()
default BigInteger union(BigInteger mask)
this
and mask
, then applies logical OR. This
results in a new bit mask that also includes this
.mask
- A bit mask, must be positive.mask.or(this.bitmask())
union(BigInteger)
,
union(EnumSet)
,
union(Enum...)
default BitSet union(BitSet set)
this
added.set
- A set of enum elements.EnumSet
including all elements of the set and also this
.default EnumBitSet<E> union(E... set)
Note that there are other ways to do this. The following expressions define the same set:
Planet.EARTH.union(Planet.MARS, Planet.JUPITER)
Planet.of(Planet.EARTH, Planet.MARS, Planet.JUPITER)
set
- A list of elements to add.this
and all given elements.EnumSet.of(Enum)
default EnumBitSet<E> union(EnumBitSet<E> set)
this
and a clone of mask
, then applies logical
OR. This results in a new bit set that also includes this
.set
- A set of enum elements.mask.clone() | this
union(BigInteger)
,
union(EnumSet)
,
union(Enum...)
default EnumSet<E> union(EnumSet<E> set)
this
added.set
- A set of enum elements.EnumSet
including all elements of the set and also this
.default long union(long mask) throws MoreThan64ElementsException
this
included.mask
- A bit mask.MoreThan64ElementsException
- Thrown if the enum type contains more than 64 elements.default BigInteger xor(BigInteger mask)
mask
- A bit mask, must be positive.mask.xor(this.bitmask())
default EnumBitSet<E> xor(EnumBitSet<E> set)
set
- A set of enum elements.set.clone() XOR this