Class Flags<E extends java.lang.Enum<E>,​This extends Flags<E,​This>>

  • All Implemented Interfaces:
    java.lang.Iterable<E>, java.util.Collection<E>, java.util.Set<E>, java.util.SortedSet<E>
    Direct Known Subclasses:
    GeneralFlags, NumericFlags

    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 correct enum type), or null 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 the null 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 from fromElement 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_()
      Return this.
      java.lang.Object[] toArray()
      Get an Object 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.
      • Methods inherited from class java.util.AbstractSet

        removeAll
      • Methods inherited from class java.util.AbstractCollection

        add, addAll, clear, remove, retainAll
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
      • Methods inherited from interface java.util.Collection

        parallelStream, removeIf, stream
      • Methods inherited from interface java.util.Set

        add, addAll, clear, remove, removeAll, retainAll
      • Methods inherited from interface java.util.SortedSet

        spliterator
    • Constructor Detail

      • Flags

        protected Flags​(int bits)
        Construct a new instance. This constructor should only be called during initial array construction.
        Parameters:
        bits - the bits of this set
    • 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_()
        Return this.
        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 correct enum type), or null 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.
        Specified by:
        size in interface java.util.Collection<E extends java.lang.Enum<E>>
        Specified by:
        size in interface java.util.Set<E extends java.lang.Enum<E>>
        Specified by:
        size in class java.util.AbstractCollection<E extends java.lang.Enum<E>>
        Returns:
        the flag set size
      • first

        public final E first()
        Get the first flag in the set.
        Specified by:
        first in interface java.util.SortedSet<E extends java.lang.Enum<E>>
        Returns:
        the first flag
      • last

        public final E last()
        Get the last flag in the set.
        Specified by:
        last in interface java.util.SortedSet<E extends java.lang.Enum<E>>
        Returns:
        the last flag
      • comparator

        public final java.util.Comparator<? super E> comparator()
        Get the null comparator, indicating that this set is always sorted in natural order.
        Specified by:
        comparator in interface java.util.SortedSet<E extends java.lang.Enum<E>>
        Returns:
        null
      • isEmpty

        public boolean isEmpty()
        Determine if this flag set is empty.
        Specified by:
        isEmpty in interface java.util.Collection<E extends java.lang.Enum<E>>
        Specified by:
        isEmpty in interface java.util.Set<E extends java.lang.Enum<E>>
        Overrides:
        isEmpty in class java.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.
        Specified by:
        headSet in interface java.util.SortedSet<E extends java.lang.Enum<E>>
        Parameters:
        toElement - the "to" element (must not be null)
        Returns:
        the subset
      • tailSet

        public final This tailSet​(E fromElement)
        Get the subset of flags from this set, starting from the given element.
        Specified by:
        tailSet in interface java.util.SortedSet<E extends java.lang.Enum<E>>
        Parameters:
        fromElement - the "from" element (must not be null)
        Returns:
        the subset
      • subSet

        public final This subSet​(E fromElement,
                                 E toElement)
        Get the subset of flags, starting from fromElement up to (but not including) toElement.
        Specified by:
        subSet in interface java.util.SortedSet<E extends java.lang.Enum<E>>
        Parameters:
        fromElement - the "from" element (must not be null)
        toElement - the "to" element (must not be null)
        Returns:
        the subset
      • toArray

        public final java.lang.Object[] toArray()
        Get an Object array containing all the flag values of this set.
        Specified by:
        toArray in interface java.util.Collection<E extends java.lang.Enum<E>>
        Specified by:
        toArray in interface java.util.Set<E extends java.lang.Enum<E>>
        Overrides:
        toArray in class java.util.AbstractCollection<E extends java.lang.Enum<E>>
        Returns:
        the Object array
      • toArray

        public final <T> T[] toArray​(T[] array)
        Get a typed array containing all the flag values of this set.
        Specified by:
        toArray in interface java.util.Collection<E extends java.lang.Enum<E>>
        Specified by:
        toArray in interface java.util.Set<E extends java.lang.Enum<E>>
        Overrides:
        toArray in class java.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 interface java.util.Collection<E extends java.lang.Enum<E>>
        Specified by:
        contains in interface java.util.Set<E extends java.lang.Enum<E>>
        Overrides:
        contains in class java.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 interface java.util.Collection<E extends java.lang.Enum<E>>
        Specified by:
        containsAll in interface java.util.Set<E extends java.lang.Enum<E>>
        Overrides:
        containsAll in class java.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 flag
        flag2 - 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 flag
        flag2 - the second flag
        flag3 - 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 flag
        flag2 - 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 flag
        flag2 - the second flag
        flag3 - 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 flag
        flag2 - 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 flag
        flag2 - the second flag
        flag3 - 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 interface java.util.Collection<E extends java.lang.Enum<E>>
        Specified by:
        equals in interface java.util.Set<E extends java.lang.Enum<E>>
        Overrides:
        equals in class java.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.
        Specified by:
        hashCode in interface java.util.Collection<E extends java.lang.Enum<E>>
        Specified by:
        hashCode in interface java.util.Set<E extends java.lang.Enum<E>>
        Overrides:
        hashCode in class java.util.AbstractSet<E extends java.lang.Enum<E>>
        Returns:
        the flag set hash code
      • iterator

        public final java.util.Iterator<E> iterator()
        Iterate this set in order from first to last flag.
        Specified by:
        iterator in interface java.util.Collection<E extends java.lang.Enum<E>>
        Specified by:
        iterator in interface java.lang.Iterable<E extends java.lang.Enum<E>>
        Specified by:
        iterator in interface java.util.Set<E extends java.lang.Enum<E>>
        Specified by:
        iterator in class java.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.
        Specified by:
        forEach in interface java.lang.Iterable<E extends java.lang.Enum<E>>
        Parameters:
        action - the action to apply
      • toString

        public final java.lang.String toString()
        Get a string representation of this flag set.
        Overrides:
        toString in class java.util.AbstractCollection<E extends java.lang.Enum<E>>
        Returns:
        the string representation