Class EnumSet

java.lang.Object
java.util.AbstractCollection<E>
java.util.AbstractSet<Enum<?>>
com.github.tommyettinger.ds.EnumSet
All Implemented Interfaces:
EnhancedCollection<Enum<?>>, Iterable<Enum<?>>, Collection<Enum<?>>, Set<Enum<?>>
Direct Known Subclasses:
EnumFloatMap.Keys, EnumIntMap.Keys, EnumLongMap.Keys, EnumOrderedSet

public class EnumSet extends AbstractSet<Enum<?>> implements Set<Enum<?>>, Iterable<Enum<?>>, EnhancedCollection<Enum<?>>
A naturally-ordered Set of Enum items. Unlike EnumSet, this does not require a Class at construction time, which can be useful for serialization purposes. Instead of storing a Class, this holds a "key universe" (which is almost always the same as an array returned by calling values() on an Enum type), and key universes are ideally shared between compatible EnumSets. No allocation is done unless this is changing its table size and/or key universe. The natural order of Enum items is in ascending order of their Enum.ordinal() values.
The key universe is an important concept here; it is simply an array of all possible Enum values the EnumSet can use as keys, in the specific order they are declared. You almost always get a key universe by calling MyEnum.values(), but you can also use Class.getEnumConstants() for an Enum class. You can and generally should reuse key universes in order to avoid allocations and/or save memory; the method noneOf(Enum[]) creates an empty EnumSet with a given key universe. If you need to use the zero-argument constructor, you can, and the key universe will be obtained from the first key placed into the EnumSet, though it won't be shared at first. You can also set the key universe with clearToUniverse(Enum[]), in the process of clearing the map.
This class tries to be as compatible as possible with EnumSet, though this expands on that where possible.
  • Field Details

  • Constructor Details

    • EnumSet

      public EnumSet()
      Empty constructor; using this will postpone allocating any internal arrays until add(Enum) is first called (potentially indirectly).
    • EnumSet

      public EnumSet(Enum<?>[] universe, boolean ignoredToDistinguish)
      Initializes this set so that it has exactly enough capacity as needed to contain each Enum constant defined in universe, assuming universe stores every possible constant in one Enum type. You almost always obtain universe from calling values() on an Enum type, and you can share one reference to one Enum array across many EnumSet instances if you don't modify the shared array. Sharing the same universe helps save some memory if you have (very) many EnumSet instances.
      Because the boolean parameter here is easy to forget, you may want to prefer calling noneOf(Enum[]) instead of using this directly.
      Parameters:
      universe - almost always, the result of calling values() on an Enum type; used directly, not copied
      ignoredToDistinguish - an ignored boolean that differentiates this constructor, which defined a key universe, from one that takes contents
    • EnumSet

      public EnumSet(Class<? extends Enum<?>> universeClass)
      Initializes this set so that it has exactly enough capacity as needed to contain each Enum constant defined by the Class universeClass, assuming universeClass is non-null. This simply calls EnumSet(Enum[], boolean) for convenience. Note that this constructor allocates a new array of Enum constants each time it is called, where if you use EnumSet(Enum[], boolean) (or its equivalent, noneOf(Enum[])), you can reuse an unmodified array to reduce allocations.
      Parameters:
      universeClass - the Class of an Enum type that defines the universe of valid Enum items this can hold
    • EnumSet

      public EnumSet(Enum<?>[] contents)
      Initializes this set so that it holds the given Enum values, with the universe of possible Enum constants this can hold determined by the type of the first Enum in contents.
      This is different from EnumSet(Enum[], boolean) in that this takes constants and puts them in the set, while the other constructor takes all possible Enum constants, usually from calling values(). You can also specify the contents of a new EnumSet conveniently using with(Enum[]), which allows passing items as varargs.
      Parameters:
      contents - an array of Enum items to place into this set
    • EnumSet

      public EnumSet(Iterator<? extends Enum<?>> contents)
      Initializes this set so that it holds the given Enum values, with the universe of possible Enum constants this can hold determined by the type of the first Enum in contents.
      Parameters:
      contents - a Collection of Enum items to place into this set
    • EnumSet

      public EnumSet(Collection<? extends Enum<?>> contents)
      Initializes this set so that it holds the given Enum values, with the universe of possible Enum constants this can hold determined by the type of the first Enum in contents.
      Parameters:
      contents - a Collection of Enum items to place into this set
    • EnumSet

      public EnumSet(EnumSet other)
      Copy constructor; uses a direct reference to the enum values that may be cached in other, but copies other fields.
      Parameters:
      other - another EnumSet that will have most of its data copied, but its cached values() results will be used directly
  • Method Details

    • size

      public int size()
      Returns the number of elements in this set (its cardinality). If this set contains more than Integer.MAX_VALUE elements, returns Integer.MAX_VALUE.
      Specified by:
      size in interface Collection<Enum<?>>
      Specified by:
      size in interface Set<Enum<?>>
      Specified by:
      size in class AbstractCollection<Enum<?>>
      Returns:
      the number of elements in this set (its cardinality)
    • isEmpty

      public boolean isEmpty()
      Returns true if this set contains no elements.
      Specified by:
      isEmpty in interface Collection<Enum<?>>
      Specified by:
      isEmpty in interface Set<Enum<?>>
      Overrides:
      isEmpty in class AbstractCollection<Enum<?>>
      Returns:
      true if this set contains no elements
    • contains

      public boolean contains(Object item)
      Returns true if this set contains the specified element. More formally, returns true if and only if this set contains an element e such that Objects.equals(item, e).
      Specified by:
      contains in interface Collection<Enum<?>>
      Specified by:
      contains in interface Set<Enum<?>>
      Overrides:
      contains in class AbstractCollection<Enum<?>>
      Parameters:
      item - element whose presence in this set is to be tested
      Returns:
      true if this set contains the specified element
    • iterator

      public Iterator<Enum<?>> iterator()
      Returns an iterator for the items in the set. The elements are returned in the order of their Enum.ordinal() values. Remove is supported.

      Use the EnumSet.EnumSetIterator constructor for nested or multithreaded iteration.

      Specified by:
      iterator in interface Collection<Enum<?>>
      Specified by:
      iterator in interface Iterable<Enum<?>>
      Specified by:
      iterator in interface Set<Enum<?>>
      Specified by:
      iterator in class AbstractCollection<Enum<?>>
    • add

      public boolean add(Enum<?> item)
      Adds the specified element to this set if it is not already present (optional operation). More formally, adds the specified element e to this set if the set contains no element e2 such that Objects.equals(e, e2). If this set already contains the element, the call leaves the set unchanged and returns false. In combination with the restriction on constructors, this ensures that sets never contain duplicate elements.

      The stipulation above does not imply that sets must accept all elements; sets may refuse to add any particular element, including null, and throw an exception, as described in the specification for Collection.add. Individual set implementations should clearly document any restrictions on the elements that they may contain.

      Specified by:
      add in interface Collection<Enum<?>>
      Specified by:
      add in interface Set<Enum<?>>
      Overrides:
      add in class AbstractCollection<Enum<?>>
      Parameters:
      item - element to be added to this set
      Returns:
      true if this set did not already contain the specified element
    • remove

      public boolean remove(Object item)
      Removes the specified element from this set if it is present (optional operation). More formally, removes an element e such that Objects.equals(item, e), if this set contains such an element. Returns true if this set contained the element (or equivalently, if this set changed as a result of the call). (This set will not contain the element once the call returns.)
      Specified by:
      remove in interface Collection<Enum<?>>
      Specified by:
      remove in interface Set<Enum<?>>
      Overrides:
      remove in class AbstractCollection<Enum<?>>
      Parameters:
      item - object to be removed from this set, if present
      Returns:
      true if this set contained the specified element
      Throws:
      ClassCastException - if the type of the specified element is incompatible with this set (optional)
      NullPointerException - if the specified element is null and this set does not permit null elements (optional)
      UnsupportedOperationException - if the remove operation is not supported by this set
    • retainAll

      public boolean retainAll(Collection<?> c)
      Removes all items from this unless they are also in the given Collection c.
      Specified by:
      retainAll in interface Collection<Enum<?>>
      Specified by:
      retainAll in interface Set<Enum<?>>
      Overrides:
      retainAll in class AbstractCollection<Enum<?>>
      Parameters:
      c - usually another EnumSet, but not required to be
    • addAll

      public boolean addAll(Collection<? extends Enum<?>> c)
      Adds all the elements in the specified collection to this collection.
      Specified by:
      addAll in interface Collection<Enum<?>>
      Specified by:
      addAll in interface Set<Enum<?>>
      Overrides:
      addAll in class AbstractCollection<Enum<?>>
      Parameters:
      c - usually another EnumSet, but not required to be
    • containsAll

      public boolean containsAll(Collection<?> c)
      Returns true if this EnumSet contains all items in the given Collection, or false otherwise.
      Specified by:
      containsAll in interface Collection<Enum<?>>
      Specified by:
      containsAll in interface Set<Enum<?>>
      Overrides:
      containsAll in class AbstractCollection<Enum<?>>
      Parameters:
      c - usually another EnumSet, but not required to be
    • removeAll

      public boolean removeAll(Collection<?> c)
      Removes from this EnumSet every element in the given Collection.
      Specified by:
      removeAll in interface Collection<Enum<?>>
      Specified by:
      removeAll in interface Set<Enum<?>>
      Overrides:
      removeAll in class AbstractSet<Enum<?>>
      Parameters:
      c - usually another EnumSet, but not required to be
      Returns:
      true if this set changed as a result of the call
    • addAll

      public boolean addAll(Enum<?>[] c)
      Adds all Enum items in the given array to this set. Returns true if this set was modified at all in the process (that is, if any items in c were not already present in this set).
      Specified by:
      addAll in interface EnhancedCollection<Enum<?>>
      See Also:
    • clear

      public void clear()
      Removes all the elements from this set. The set will be empty after this call returns. This does not change the universe of possible Enum items this can hold.
      Specified by:
      clear in interface Collection<Enum<?>>
      Specified by:
      clear in interface Set<Enum<?>>
      Overrides:
      clear in class AbstractCollection<Enum<?>>
    • clearToUniverse

      public void clearToUniverse(Enum<?>[] universe)
      Removes all the elements from this set and can reset the universe of possible Enum items this can hold. The set will be empty after this call returns. This changes the universe of possible Enum items this can hold to match universe. If universe is null, this resets this set to the state it would have after EnumSet() was called. If the table this would need is the same size as or smaller than the current table (such as if universe is the same as the universe here), this will not allocate, but will still clear any items this holds and will set the universe to the given one. Otherwise, this allocates and uses a new table of a larger size, with nothing in it, and uses the given universe. This always uses universe directly, without copying.
      This can be useful to allow an EnumSet that was created with EnumSet() to share a universe with other EnumSets.
      Parameters:
      universe - the universe of possible Enum items this can hold; almost always produced by values() on an Enum
    • clearToUniverse

      public void clearToUniverse(Class<? extends Enum<?>> universe)
      Removes all the elements from this set and can reset the universe of possible Enum items this can hold. The set will be empty after this call returns. This changes the universe of possible Enum items this can hold to match the Enum constants in universe. If universe is null, this resets this set to the state it would have after EnumSet() was called. If the table this would need is the same size as or smaller than the current table (such as if universe is the same as the universe here), this will not allocate, but will still clear any items this holds and will set the universe to the given one. Otherwise, this allocates and uses a new table of a larger size, with nothing in it, and uses the given universe. This calls Class.getEnumConstants() if universe is non-null, which allocates a new array.
      You may want to prefer calling clearToUniverse(Enum[]) (the overload that takes an array), because it can be used to share one universe array between many EnumSet instances. This overload, given a Class, has to call Class.getEnumConstants() and thus allocate a new array each time this is called.
      Parameters:
      universe - the Class of an Enum type that stores the universe of possible Enum items this can hold
    • getUniverse

      public Enum<?>[] getUniverse()
      Gets the current key universe; this is a technically-mutable array, but should never be modified. To set the universe on an existing EnumSet (with existing contents), you can use clearToUniverse(Enum[]). If an EnumSet has not been initialized, just adding an item will set its key universe to match the given item.
      Returns:
      the current key universe
    • nextOrdinal

      public int nextOrdinal(int minOrdinal)
      Returns the first ordinal equal to or greater than the minOrdinal of an Enum contained in the set. If no such Enum exists, or if minOrdinal is invalid (such as if it is negative or greater than the highest ordinal in the Enum type this holds), then -1 is returned.
      Parameters:
      minOrdinal - the index to start looking at; does not need to have an Enum present there, but must be non-negative
      Returns:
      the first ordinal of an Enum contained in the set on or after the specified starting point, or -1 if none can be found
    • truncate

      public void truncate(int newSize)
      Reduces the size of the set to the specified size. If the set is already smaller than the specified size, no action is taken. This indiscriminately removes items from the set until the requested newSize is reached, or until the set is empty.
      Because the iteration order is not guaranteed by an unordered set, this can remove essentially any item(s) from the set if it is larger than newSize. Most commonly, it removes from the start of the iteration order.
      Parameters:
      newSize - the target size to try to reach by removing items, if smaller than the current size
    • nextEnum

      public Enum<?> nextEnum(int minOrdinal)
      Returns the first Enum contained in the set with an ordinal equal to or greater than minOrdinal. If no such Enum exists, or if minOrdinal is invalid (such as if it is negative or greater than the highest ordinal in the Enum type this holds), then null is returned.
      Parameters:
      minOrdinal - the index to start looking at; does not need to have an Enum present there, but must be non-negative
      Returns:
      the first Enum contained in the set on or after the specified starting point, or null if none can be found
    • nextEnum

      public Enum<?> nextEnum(Enum<?> from)
      Returns the first Enum contained in the set on or after the point where the given Enum from would occur. If no such Enum exists then null is returned.
      Parameters:
      from - the Enum to start looking at; does not need to be present in the set
      Returns:
      the first Enum contained in the set on or after the specified starting point, or null if none can be found
    • toString

      public String toString()
      Overrides:
      toString in class AbstractCollection<Enum<?>>
    • with

      public static EnumSet with(Enum<?> item)
      Builds an EnumSet that contains only the given element.
      Parameters:
      item - the one item to initialize the EnumSet with
      Returns:
      a new EnumSet containing item
    • with

      public static EnumSet with(Enum<?> item0, Enum<?> item1)
      Creates a new EnumSet that holds only the given items, but can be resized.
      Parameters:
      item0 - an Enum item
      item1 - an Enum item
      Returns:
      a new EnumSet that holds the given items
    • with

      public static EnumSet with(Enum<?> item0, Enum<?> item1, Enum<?> item2)
      Creates a new EnumSet that holds only the given items, but can be resized.
      Parameters:
      item0 - an Enum item
      item1 - an Enum item
      item2 - an Enum item
      Returns:
      a new EnumSet that holds the given items
    • with

      public static EnumSet with(Enum<?> item0, Enum<?> item1, Enum<?> item2, Enum<?> item3)
      Creates a new EnumSet that holds only the given items, but can be resized.
      Parameters:
      item0 - an Enum item
      item1 - an Enum item
      item2 - an Enum item
      item3 - an Enum item
      Returns:
      a new EnumSet that holds the given items
    • with

      public static EnumSet with(Enum<?> item0, Enum<?> item1, Enum<?> item2, Enum<?> item3, Enum<?> item4)
      Creates a new EnumSet that holds only the given items, but can be resized.
      Parameters:
      item0 - an Enum item
      item1 - an Enum item
      item2 - an Enum item
      item3 - an Enum item
      item4 - an Enum item
      Returns:
      a new EnumSet that holds the given items
    • with

      public static EnumSet with(Enum<?> item0, Enum<?> item1, Enum<?> item2, Enum<?> item3, Enum<?> item4, Enum<?> item5)
      Creates a new EnumSet that holds only the given items, but can be resized.
      Parameters:
      item0 - an Enum item
      item1 - an Enum item
      item2 - an Enum item
      item3 - an Enum item
      item4 - an Enum item
      item5 - an Enum item
      Returns:
      a new EnumSet that holds the given items
    • with

      public static EnumSet with(Enum<?> item0, Enum<?> item1, Enum<?> item2, Enum<?> item3, Enum<?> item4, Enum<?> item5, Enum<?> item6)
      Creates a new EnumSet that holds only the given items, but can be resized.
      Parameters:
      item0 - an Enum item
      item1 - an Enum item
      item2 - an Enum item
      item3 - an Enum item
      item4 - an Enum item
      item5 - an Enum item
      item6 - an Enum item
      Returns:
      a new EnumSet that holds the given items
    • with

      public static EnumSet with(Enum<?> item0, Enum<?> item1, Enum<?> item2, Enum<?> item3, Enum<?> item4, Enum<?> item5, Enum<?> item6, Enum<?> item7)
      Creates a new EnumSet that holds only the given items, but can be resized.
      Parameters:
      item0 - an Enum item
      item1 - an Enum item
      item2 - an Enum item
      item3 - an Enum item
      item4 - an Enum item
      item5 - an Enum item
      item6 - an Enum item
      Returns:
      a new EnumSet that holds the given items
    • with

      public static EnumSet with(Enum<?>... array)
      Builds an EnumSet that contains the unique elements from the given array or varargs.
      Parameters:
      array - an array or varargs of Enum constants, which should all have the same Enum type
      Returns:
      a new EnumSet containing each unique item from array
    • parse

      public static EnumSet parse(String str, String delimiter, PartialParser<Enum<?>> parser)
      Calls parse(String, String, PartialParser, boolean) with brackets set to false.
      The parser is often produced by PartialParser.enumParser(ObjToObjFunction).
      Parameters:
      str - a String that will be parsed in full
      delimiter - the delimiter between items in str
      parser - a PartialParser that returns an Enum item from a section of str
      Returns:
      a new collection parsed from str
    • parse

      public static EnumSet parse(String str, String delimiter, PartialParser<Enum<?>> parser, boolean brackets)
      Creates a new collection and fills it by calling EnhancedCollection.addLegible(String, String, PartialParser, int, int) on either all of str (if brackets is false) or str without its first and last chars (if brackets is true). Each item is expected to be separated by delimiter.
      The parser is often produced by PartialParser.enumParser(ObjToObjFunction).
      Parameters:
      str - a String that will be parsed in full (depending on brackets)
      delimiter - the delimiter between items in str
      parser - a PartialParser that returns an Enum item from a section of str
      brackets - if true, the first and last chars in str will be ignored
      Returns:
      a new collection parsed from str
    • parse

      public static EnumSet parse(String str, String delimiter, PartialParser<Enum<?>> parser, int offset, int length)
      Creates a new collection and fills it by calling EnhancedCollection.addLegible(String, String, PartialParser, int, int) with the given five parameters as-is.
      The parser is often produced by PartialParser.enumParser(ObjToObjFunction).
      Parameters:
      str - a String that will have the given section parsed
      delimiter - the delimiter between items in str
      parser - a PartialParser that returns an Enum item from a section of str
      offset - the first position to parse in str, inclusive
      length - how many chars to parse, starting from offset
      Returns:
      a new collection parsed from str
    • of

      public static EnumSet of(Enum<?> item)
      Alias of with(Enum) for compatibility.
      Parameters:
      item - the one item to initialize the EnumSet with
      Returns:
      a new EnumSet containing item
    • of

      public static EnumSet of(Enum<?>... array)
      Alias of with(Enum[]) for compatibility.
      Parameters:
      array - an array or varargs of Enum constants, which should all have the same Enum type
      Returns:
      a new EnumSet containing each unique item from array
    • noneOf

      public static EnumSet noneOf(Enum<?>[] universe)
      Creates a new EnumSet using the given result of calling values() on an Enum type (the universe), but with no items initially stored in the set. You can reuse the universe between EnumSet instances as long as it is not modified.
      This is the same as calling EnumSet(Enum[], boolean).
      Parameters:
      universe - almost always, the result of calling values() on an Enum type; used directly, not copied
      Returns:
      a new EnumSet with the specified universe of possible items, but none present in the set
    • allOf

      public static EnumSet allOf(Enum<?>[] universe)
      Creates a new EnumSet using the given result of calling values() on an Enum type (the universe), and with all possible items initially stored in the set. You can reuse the universe between EnumSet instances as long as it is not modified.
      Parameters:
      universe - almost always, the result of calling values() on an Enum type; used directly, not copied
      Returns:
      a new EnumSet with the specified universe of possible items, and all of them present in the set
    • noneOf

      public static EnumSet noneOf(Class<? extends Enum<?>> clazz)
      Creates a new EnumSet using the constants from the given Class (of an Enum type), but with no items initially stored in the set.
      This is the same as calling EnumSet(Class).
      Parameters:
      clazz - the Class of any Enum type; you can get this from a constant with Enum.getDeclaringClass()
      Returns:
      a new EnumSet with the specified universe of possible items, but none present in the set
    • allOf

      public static EnumSet allOf(Class<? extends Enum<?>> clazz)
      Creates a new EnumSet using the constants from the given Class (of an Enum type), and with all possible items initially stored in the set.
      Parameters:
      clazz - the Class of any Enum type; you can get this from a constant with Enum.getDeclaringClass()
      Returns:
      a new EnumSet with the specified universe of possible items, and all of them present in the set
    • complementOf

      public static EnumSet complementOf(EnumSet other)
      Given another EnumSet, this creates a new EnumSet with the same universe as other, but with any elements present in other absent in the new set, and any elements absent in other present in the new set.
      Parameters:
      other - another EnumSet that this will copy
      Returns:
      a complemented copy of other
    • copyOf

      public static EnumSet copyOf(Collection<? extends Enum<?>> contents)
      Creates an EnumSet holding any Enum items in the given contents, which may be any Collection of Enum, including another EnumSet. If given an EnumSet, this will copy its Enum universe and other information even if it is empty.
      Parameters:
      contents - a Collection of Enum values, which may be another EnumSet
      Returns:
      a new EnumSet containing the unique items in contents
    • range

      public static <E extends Enum<E>> EnumSet range(Enum<E> start, Enum<E> end)
      Creates an EnumSet holding Enum items between the ordinals of start and end. If the ordinal of end is less than the ordinal of start, this throws an IllegalArgumentException. If start and end are the same, this just inserts that one Enum.
      Type Parameters:
      E - the shared Enum type of both start and end
      Parameters:
      start - the starting inclusive Enum to insert
      end - the ending inclusive Enum to insert
      Returns:
      a new EnumSet containing start, end, and any Enum constants with ordinals between them
      Throws:
      IllegalArgumentException - if the ordinal of end is less than the ordinal of start