Package org.wildfly.common.flags
Class Flags<E extends java.lang.Enum<E>,This extends Flags<E,This>>
- java.lang.Object
-
- java.util.AbstractCollection<E>
-
- java.util.AbstractSet<E>
-
- org.wildfly.common.flags.Flags<E,This>
-
- All Implemented Interfaces:
java.lang.Iterable<E>
,java.util.Collection<E>
,java.util.Set<E>
,java.util.SortedSet<E>
public abstract class Flags<E extends java.lang.Enum<E>,This extends Flags<E,This>> extends java.util.AbstractSet<E> implements java.util.SortedSet<E>
A base class for implementing value-like flags and flag sets, where flags and flag sets may be used without allocation overhead.
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
Flags(int bits)
Construct a new instance.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected abstract E
castItemOrNull(java.lang.Object obj)
Get the item (cast to the correctenum
type), ornull
if it is not of the correct type.protected abstract This
castThis(java.lang.Object obj)
Cast the given object to this class, throwing an exception if the cast fails.java.util.Comparator<? super E>
comparator()
Get thenull
comparator, indicating that this set is always sorted in natural order.This
complement()
Get the complement of this set.boolean
contains(E flag)
Determine if this flag set contains the given flag.boolean
contains(java.lang.Object o)
Determine if this flag set contains the given object.boolean
containsAll(E flag1, E flag2)
Determine if this flag set contains all of the given flags.boolean
containsAll(E flag1, E flag2, E flag3)
Determine if this flag set contains all of the given flags.boolean
containsAll(java.util.Collection<?> c)
Determine if this flag set contains all of the objects in the given collection.boolean
containsAll(This other)
Determine if this flag set contains all of the flags in the given flag set.boolean
containsAny(E flag1, E flag2)
Determine if this flag set contains any of the given flags.boolean
containsAny(E flag1, E flag2, E flag3)
Determine if this flag set contains any of the given flags.boolean
containsAny(This other)
Determine if this flag set contains any of the flags in the given flag set.java.util.Iterator<E>
descendingIterator()
Iterate this set in order from last to first flag.boolean
equals(java.lang.Object o)
Determine if this flag set is equal to the given object.boolean
equals(java.util.Set<?> o)
Determine if this flag set is equal to the given set.boolean
equals(This o)
Determine if this flag set is equal to the given flag set.E
first()
Get the first flag in the set.void
forEach(java.util.function.Consumer<? super E> action)
Apply the given action for every flag in this set.int
hashCode()
Get the hash code of this flag set.This
headSet(E toElement)
Get the subset of flags from this set, up to (but not including) the given element.boolean
isEmpty()
Determine if this flag set is empty.protected abstract E
itemOf(int index)
Get the flag item with the given index.java.util.Iterator<E>
iterator()
Iterate this set in order from first to last flag.E
last()
Get the last flag in the set.int
size()
Get the size of the flag set.This
subSet(E fromElement, E toElement)
Get the subset of flags, starting fromfromElement
up to (but not including)toElement
.This
tailSet(E fromElement)
Get the subset of flags from this set, starting from the given element.protected abstract This
this_()
Returnthis
.java.lang.Object[]
toArray()
Get anObject
array containing all the flag values of this set.<T> T[]
toArray(T[] array)
Get a typed array containing all the flag values of this set.java.lang.String
toString()
Get a string representation of this flag set.protected abstract This
value(int bits)
Get the set value of the given bit combination.This
with(E flag)
Return a set which includes all of the flags in this set and the given additional flag.This
with(E... flags)
Return a set which includes all of the flags in this set and the given additional flags.This
with(E flag1, E flag2)
Return a set which includes all of the flags in this set and the given additional flags.This
with(E flag1, E flag2, E flag3)
Return a set which includes all of the flags in this set and the given additional flags.This
with(This other)
Return a set which includes all of the flags in this set and the given additional flags.This
without(E flag)
Return a set which includes all of the flags except for the given flag.This
without(This other)
Return a set which includes all of the flags except for the given flags.
-
-
-
Method Detail
-
value
protected abstract This value(int bits)
Get the set value of the given bit combination. The bit combination may contain extraneous one-bits so any bits beyond the bit of the last flag should be masked off if an array is used for lookup.- Parameters:
bits
- the bit combination (possibly with extra bits)- Returns:
- the set instance
-
this_
protected abstract This this_()
Returnthis
.- Returns:
this
-
itemOf
protected abstract E itemOf(int index)
Get the flag item with the given index.- Parameters:
index
- the index- Returns:
- the flag
-
castItemOrNull
protected abstract E castItemOrNull(java.lang.Object obj)
Get the item (cast to the correctenum
type), ornull
if it is not of the correct type.- Parameters:
obj
- the object to cast- Returns:
- the cast object, or
null
-
castThis
protected abstract This castThis(java.lang.Object obj)
Cast the given object to this class, throwing an exception if the cast fails.- Parameters:
obj
- the object to cast- Returns:
- the cast object
-
size
public final int size()
Get the size of the flag set.
-
first
public final E first()
Get the first flag in the set.
-
last
public final E last()
Get the last flag in the set.
-
comparator
public final java.util.Comparator<? super E> comparator()
Get thenull
comparator, indicating that this set is always sorted in natural order.
-
isEmpty
public boolean isEmpty()
Determine if this flag set is empty.- Specified by:
isEmpty
in interfacejava.util.Collection<E extends java.lang.Enum<E>>
- Specified by:
isEmpty
in interfacejava.util.Set<E extends java.lang.Enum<E>>
- Overrides:
isEmpty
in classjava.util.AbstractCollection<E extends java.lang.Enum<E>>
- Returns:
true
if the flag set is empty,false
otherwise
-
headSet
public final This headSet(E toElement)
Get the subset of flags from this set, up to (but not including) the given element.
-
tailSet
public final This tailSet(E fromElement)
Get the subset of flags from this set, starting from the given element.
-
subSet
public final This subSet(E fromElement, E toElement)
Get the subset of flags, starting fromfromElement
up to (but not including)toElement
.
-
toArray
public final java.lang.Object[] toArray()
Get anObject
array containing all the flag values of this set.
-
toArray
public final <T> T[] toArray(T[] array)
Get a typed array containing all the flag values of this set.- Specified by:
toArray
in interfacejava.util.Collection<E extends java.lang.Enum<E>>
- Specified by:
toArray
in interfacejava.util.Set<E extends java.lang.Enum<E>>
- Overrides:
toArray
in classjava.util.AbstractCollection<E extends java.lang.Enum<E>>
- Type Parameters:
T
- the element type- Parameters:
array
- the array to populate or clone- Returns:
- the populated array
-
contains
public final boolean contains(E flag)
Determine if this flag set contains the given flag.- Parameters:
flag
- the flag- Returns:
true
if the flag is contained by this set
-
contains
public final boolean contains(java.lang.Object o)
Determine if this flag set contains the given object.- Specified by:
contains
in interfacejava.util.Collection<E extends java.lang.Enum<E>>
- Specified by:
contains
in interfacejava.util.Set<E extends java.lang.Enum<E>>
- Overrides:
contains
in classjava.util.AbstractCollection<E extends java.lang.Enum<E>>
- Parameters:
o
- the object- Returns:
true
if the object is contained by this set
-
containsAll
public final boolean containsAll(java.util.Collection<?> c)
Determine if this flag set contains all of the objects in the given collection.- Specified by:
containsAll
in interfacejava.util.Collection<E extends java.lang.Enum<E>>
- Specified by:
containsAll
in interfacejava.util.Set<E extends java.lang.Enum<E>>
- Overrides:
containsAll
in classjava.util.AbstractCollection<E extends java.lang.Enum<E>>
- Parameters:
c
- the collection- Returns:
true
if all of the collection's objects are contained by this set
-
containsAll
public final boolean containsAll(This other)
Determine if this flag set contains all of the flags in the given flag set.- Parameters:
other
- the flag set- Returns:
true
if all of the given set's flags are contained by this set
-
containsAll
public final boolean containsAll(E flag1, E flag2)
Determine if this flag set contains all of the given flags.- Parameters:
flag1
- the first flagflag2
- the second flag- Returns:
true
if all of the given flags are contained by this set
-
containsAll
public final boolean containsAll(E flag1, E flag2, E flag3)
Determine if this flag set contains all of the given flags.- Parameters:
flag1
- the first flagflag2
- the second flagflag3
- the third flag- Returns:
true
if all of the given flags are contained by this set
-
containsAny
public final boolean containsAny(This other)
Determine if this flag set contains any of the flags in the given flag set.- Parameters:
other
- the flag set- Returns:
true
if all of the given set's flags are contained by this set
-
containsAny
public final boolean containsAny(E flag1, E flag2)
Determine if this flag set contains any of the given flags.- Parameters:
flag1
- the first flagflag2
- the second flag- Returns:
true
if any of the given flags are contained by this set
-
containsAny
public final boolean containsAny(E flag1, E flag2, E flag3)
Determine if this flag set contains any of the given flags.- Parameters:
flag1
- the first flagflag2
- the second flagflag3
- the third flag- Returns:
true
if any of the given flags are contained by this set
-
complement
public final This complement()
Get the complement of this set.- Returns:
- the complement of this set
-
with
public final This with(E flag)
Return a set which includes all of the flags in this set and the given additional flag.- Parameters:
flag
- the additional flag- Returns:
- the combined set
-
with
public final This with(E flag1, E flag2)
Return a set which includes all of the flags in this set and the given additional flags.- Parameters:
flag1
- the first flagflag2
- the second flag- Returns:
- the combined set
-
with
public final This with(E flag1, E flag2, E flag3)
Return a set which includes all of the flags in this set and the given additional flags.- Parameters:
flag1
- the first flagflag2
- the second flagflag3
- the third flag- Returns:
- the combined set
-
with
@SafeVarargs public final This with(E... flags)
Return a set which includes all of the flags in this set and the given additional flags.- Parameters:
flags
- the additional flags- Returns:
- the combined set
-
with
public final This with(This other)
Return a set which includes all of the flags in this set and the given additional flags.- Parameters:
other
- the additional flags- Returns:
- the combined set
-
without
public final This without(E flag)
Return a set which includes all of the flags except for the given flag.- Parameters:
flag
- the flag- Returns:
- the reduced set
-
without
public final This without(This other)
Return a set which includes all of the flags except for the given flags.- Parameters:
other
- the flags- Returns:
- the reduced set
-
equals
public final boolean equals(java.lang.Object o)
Determine if this flag set is equal to the given object.- Specified by:
equals
in interfacejava.util.Collection<E extends java.lang.Enum<E>>
- Specified by:
equals
in interfacejava.util.Set<E extends java.lang.Enum<E>>
- Overrides:
equals
in classjava.util.AbstractSet<E extends java.lang.Enum<E>>
- Parameters:
o
- the other object- Returns:
true
if the object is equal to this set,false
otherwise
-
equals
public final boolean equals(java.util.Set<?> o)
Determine if this flag set is equal to the given set.- Parameters:
o
- the other set- Returns:
true
if the set is equal to this set,false
otherwise
-
equals
public final boolean equals(This o)
Determine if this flag set is equal to the given flag set.- Parameters:
o
- the other flag set- Returns:
true
if the flag set is equal to this set,false
otherwise
-
hashCode
public final int hashCode()
Get the hash code of this flag set.
-
iterator
public final java.util.Iterator<E> iterator()
Iterate this set in order from first to last flag.- Specified by:
iterator
in interfacejava.util.Collection<E extends java.lang.Enum<E>>
- Specified by:
iterator
in interfacejava.lang.Iterable<E extends java.lang.Enum<E>>
- Specified by:
iterator
in interfacejava.util.Set<E extends java.lang.Enum<E>>
- Specified by:
iterator
in classjava.util.AbstractCollection<E extends java.lang.Enum<E>>
- Returns:
- the iterator
-
descendingIterator
public final java.util.Iterator<E> descendingIterator()
Iterate this set in order from last to first flag.- Returns:
- the iterator
-
forEach
public void forEach(java.util.function.Consumer<? super E> action)
Apply the given action for every flag in this set.
-
-