Class Nillable<T>

  • Type Parameters:
    T - the instance type

    public abstract class Nillable<T>
    extends Object
    Represents a tri-state object: can be nil (with an optional message), absent (or null) or present. isNil(), isAbsent() and isPresent() are mutually exclusive true.
    • Method Detail

      • isNil

        public abstract boolean isNil()
        Checks if this Nillable is nil.
        Returns:
        whether or not this instance is nil
        See Also:
        isPresent(), isAbsent()
      • isPresent

        public abstract boolean isPresent()
        Checks if this Nillable is present.
        Returns:
        whether or not this instance is present
        See Also:
        isNil(), isAbsent()
      • isAbsent

        public abstract boolean isAbsent()
        Checks if this Nillable is null.
        Returns:
        whether or not this instance is null
        See Also:
        isNil(), isPresent()
      • map

        public abstract <X> Nillable<X> map​(Function<? super T,​X> fun)
        Transforms this Nillable to the target type.
        Type Parameters:
        X - the target type
        Parameters:
        fun - the transformation function
        Returns:
        the Nillable
      • isNull

        public boolean isNull()
        Checks if this Nillable is null.
        Returns:
        whether or not this instance is null
        See Also:
        isAbsent()
      • isPresentOrNil

        public boolean isPresentOrNil()
        Checks if this this instance is not absent.
        Returns:
        whether this instance is present or nil
      • hasReason

        public boolean hasReason()
        Checks if this Nillable is nil and has a reason.
        Returns:
        whether this instance has a nil reason or not
      • present

        public static <T> Nillable<T> present​(T t)
        Creates a new Nillable from a present instance.
        Type Parameters:
        T - the type
        Parameters:
        t - the object
        Returns:
        the present Nillable
        Throws:
        NullPointerException - if t is null
      • absent

        public static <T> Nillable<T> absent()
        Creates a Nillable for a absent instance.
        Type Parameters:
        T - the type
        Returns:
        the absent Nillable
      • nil

        public static <T> Nillable<T> nil​(String reason)
        Creates a new Nillable that is nil because of optionally supplied reason.
        Type Parameters:
        T - the type
        Parameters:
        reason - the reason (may be null)
        Returns:
        the nil Nillable
      • nil

        public static <T> Nillable<T> nil()
        Creates a new Nillable that is nil.
        Type Parameters:
        T - the type
        Returns:
        the nil Nillable
      • inapplicable

        public static <T> Nillable<T> inapplicable()
        Creates a new Nillable, that is nil because it is inapplicable.
        Type Parameters:
        T - the type
        Returns:
        the nil Nillable
      • missing

        public static <T> Nillable<T> missing()
        Creates a new Nillable, that is nil because it is missing.
        Type Parameters:
        T - the type
        Returns:
        the nil Nillable
      • template

        public static <T> Nillable<T> template()
        Creates a new Nillable, that is nil because it is a template value.
        Type Parameters:
        T - the type
        Returns:
        the nil Nillable
      • unknown

        public static <T> Nillable<T> unknown()
        Creates a new Nillable, that is nil because it is unknown.
        Type Parameters:
        T - the type
        Returns:
        the nil Nillable
      • withheld

        public static <T> Nillable<T> withheld()
        Creates a new Nillable, that is nil because it is withheld.
        Type Parameters:
        T - the type
        Returns:
        the nil Nillable
      • of

        public static <T> Nillable<T> of​(T obj)
        Creates a new Nillable that is either present (if obj is not null), or absent.
        Type Parameters:
        T - the type
        Parameters:
        obj - the object (may be null)
        Returns:
        the Nillable
      • of

        public static <T> Nillable<T> of​(T obj,
                                         String reason)
        Creates a new Nillable that is either present (if obj is not null), nil (if reason is not null) or absent.
        Type Parameters:
        T - the type
        Parameters:
        obj - the object (may be null)
        reason - the nil reason (may be null)
        Returns:
        the Nillable