Package cdc.enums

Class AbstractMask<M extends AbstractMask<M,​V>,​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 Detail

      • values

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

      • AbstractMask

        protected AbstractMask​(MaskSupport<M,​V> support,
                               Set<V> values)
    • Method Detail

      • 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.
      • 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.
      • 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.
      • 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.
      • 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.
      • 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.
      • 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.