Package cdc.enums

Class AbstractMask<M extends AbstractMask<M,V>,V>

java.lang.Object
cdc.enums.AbstractMask<M,V>
Type Parameters:
M - The mask type.
V - The enum type.
All Implemented Interfaces:
Mask<M,V>
Direct Known Subclasses:
BooleanMask, EnumMask, NullableBooleanMask, NullableEnumMask

public class AbstractMask<M extends AbstractMask<M,V>,V> extends Object implements Mask<M,V>
Mask (set) of values belonging to a list type.

Objects of this class are immutable.

A typical implementation should look like this:


 public final class FooMask extends AbstractEnumMask<FooMask, Foo> {
     public static final Support<FooMask, Foo> SUPPORT = support(FooMask.class, FooMask::new, FooType, Nullable.FALSE);

     private FooMask(Support<FooMask, Foo> support,
                     Set<Foo> values) {
         super(support, values);
     }
 }
 
For a standard enum, one can declare:

 public final class FooMask extends AbstractEnumMask<FooMask, Foo> {
     public static final Support<FooMask, Foo> SUPPORT = support(FooMask.class, FooMask::new, Foo.class, Nullable.FALSE);

     private FooMask(Support<FooMask, Foo> support,
                     Set<Foo> values) {
         super(support, values);
     }
 }
 
Author:
Damien Carbonne
  • Field Details

    • support

      protected final MaskSupport<M extends AbstractMask<M,V>,V> support
      The associated support class.
    • values

      protected final Set<V> values
      The mask values.
  • Constructor Details

  • Method Details

    • support

      protected static <M extends AbstractMask<M, V>, V> MaskSupport<M,V> support(Class<M> maskClass, AbstractMask.Creator<M,V> creator, ListType<V> type, Nullable nullable)
      Creates a Support implementation.
      Type Parameters:
      M - The mask type.
      V - The enum type.
      Parameters:
      maskClass - The mask class.
      creator - The mask factory.
      type - The type.
      nullable - Nullable.TRUE if null is a valid value.
      Returns:
      A Support implementation.
    • support

      protected static <M extends AbstractMask<M, V>, V extends Enum<V>> MaskSupport<M,V> support(Class<M> maskClass, AbstractMask.Creator<M,V> creator, Class<V> enumClass, Nullable nullable)
      Creates a Support implementation for a standard enum.
      Type Parameters:
      M - The mask type.
      V - The enum type.
      Parameters:
      maskClass - The mask class.
      creator - The mask factory.
      enumClass - The enum class.
      nullable - Nullable.TRUE if null is a valid value.
      Returns:
      A Support implementation.
    • getSupport

      public MaskSupport<M,V> getSupport()
      Specified by:
      getSupport in interface Mask<M extends AbstractMask<M,V>,V>
      Returns:
      The enum type.
    • isNullable

      public boolean isNullable()
      Specified by:
      isNullable in interface Mask<M extends AbstractMask<M,V>,V>
      Returns:
      true if this mask is a nullable mask.
    • isEmpty

      public boolean isEmpty()
      Specified by:
      isEmpty in interface Mask<M extends AbstractMask<M,V>,V>
      Returns:
      true if this mask is empty.
    • isFull

      public boolean isFull()
      Specified by:
      isFull in interface Mask<M extends AbstractMask<M,V>,V>
      Returns:
      true if this mask is full. The result may change if underlying enum type is dynamic.
    • getValues

      public Set<V> getValues()
      Specified by:
      getValues in interface Mask<M extends AbstractMask<M,V>,V>
      Returns:
      The values that are set.
    • isSet

      public boolean isSet(V value)
      Description copied from interface: Mask
      Returns true if a value is contained.
      Specified by:
      isSet in interface Mask<M extends AbstractMask<M,V>,V>
      Parameters:
      value - The value.
      Returns:
      true if value is contained in this mask.
    • isClear

      public boolean isClear(V value)
      Description copied from interface: Mask
      Returns true if a value is not contained.
      Specified by:
      isClear in interface Mask<M extends AbstractMask<M,V>,V>
      Parameters:
      value - The value.
      Returns:
      true if value is not contained in this mask.
    • set

      public M set(V value, boolean enabled)
      Description copied from interface: Mask
      Sets or clears a value.
      Specified by:
      set in interface Mask<M extends AbstractMask<M,V>,V>
      Parameters:
      value - The value.
      enabled - If true sets the value, otherwise clears it.
      Returns:
      A new mask with value set or cleared.
    • set

      public M set(V value)
      Description copied from interface: Mask
      Returns a new mask augmented with a value.

      If value is already set, returns this mask.

      Specified by:
      set in interface Mask<M extends AbstractMask<M,V>,V>
      Parameters:
      value - The value.
      Returns:
      A new mask augmented with value.
    • clear

      public M clear(V value)
      Description copied from interface: Mask
      Returns a new mask reduced with a value.

      If value is already cleared, returns this mask.

      Specified by:
      clear in interface Mask<M extends AbstractMask<M,V>,V>
      Parameters:
      value - The value.
      Returns:
      A new mask reduced with value.
    • setAll

      public M setAll(boolean enabled)
      Description copied from interface: Mask
      Returns an empty or full mask.

      The full mask may change if underlying enum type is dynamic.

      Specified by:
      setAll in interface Mask<M extends AbstractMask<M,V>,V>
      Parameters:
      enabled - If true, creates a full mask. An empty mask otherwise.
      Returns:
      An empty or full mask.
    • empty

      public M empty()
      Specified by:
      empty in interface Mask<M extends AbstractMask<M,V>,V>
      Returns:
      An empty mask.
    • full

      public M full()
      Specified by:
      full in interface Mask<M extends AbstractMask<M,V>,V>
      Returns:
      A full mask at the time of call. Result may change if underlying enum type is dynamic.
    • and

      public M and(M other)
      Description copied from interface: Mask
      Returns the intersection of this mask with another one.
      Specified by:
      and in interface Mask<M extends AbstractMask<M,V>,V>
      Parameters:
      other - The other mask.
      Returns:
      The intersection of this mask with other.
    • and

      public M and(V value)
      Specified by:
      and in interface Mask<M extends AbstractMask<M,V>,V>
    • and

      public M and(V... values)
      Specified by:
      and in interface Mask<M extends AbstractMask<M,V>,V>
    • or

      public M or(M other)
      Description copied from interface: Mask
      Returns the union of this mask with another one.
      Specified by:
      or in interface Mask<M extends AbstractMask<M,V>,V>
      Parameters:
      other - The other mask.
      Returns:
      The union of this mask with other.
    • or

      public M or(V value)
      Specified by:
      or in interface Mask<M extends AbstractMask<M,V>,V>
    • or

      public M or(V... values)
      Specified by:
      or in interface Mask<M extends AbstractMask<M,V>,V>
    • not

      public M not()
      Description copied from interface: Mask
      Returns the complement of this mask (at the time of calling this method).
      Specified by:
      not in interface Mask<M extends AbstractMask<M,V>,V>
      Returns:
      The complement of this mask (at the time of calling this method).
    • contains

      public final boolean contains(M other)
      Description copied from interface: Mask
      Returns true if all values of this mask are contained in another mask.
      Specified by:
      contains in interface Mask<M extends AbstractMask<M,V>,V>
      Parameters:
      other - The other mask.
      Returns:
      true if all values of this mask are contained in other.
    • contains

      @SafeVarargs public final boolean contains(V... values)
      Specified by:
      contains in interface Mask<M extends AbstractMask<M,V>,V>
    • equals

      public boolean equals(Object other)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString(Function<? super V,String> valueToString, String separator)
      Description copied from interface: Mask
      Returns a string representation of this mask.
      Specified by:
      toString in interface Mask<M extends AbstractMask<M,V>,V>
      Parameters:
      valueToString - The function used to converter values (including null) to string.
      separator - The separator to use between values.
      Returns:
      A string representation of this mask.
    • toString

      public String toString()
      Overrides:
      toString in class Object