Class Variant<T,​U>

  • Type Parameters:
    T - First alternative type
    U - Second alternative type
    All Implemented Interfaces:
    Immutable, MutationBehaviour<Immutable>
    Direct Known Subclasses:
    CheckedValue

    @Beta
    @NonNullByDefault
    public class Variant<T,​U>
    extends Object
    implements Immutable
    Utility holder of a two-variant value. The class design treats both variants as equal.
    Author:
    Robert Varga
    • Constructor Detail

      • Variant

        protected Variant​(T first)
      • Variant

        protected Variant​(U second,
                          @Nullable Void dummy)
    • Method Detail

      • first

        protected final T first()
      • second

        protected final U second()
      • ofFirst

        public static <T,​U> Variant<T,​U> ofFirst​(T value)
        Create a new instance containing specified value.
        Type Parameters:
        T - First alternative type
        U - Second alternative type
        Parameters:
        value - Value
        Returns:
        A new instance
        Throws:
        NullPointerException - if value is null
      • ofSecond

        public static <T,​U> Variant<T,​U> ofSecond​(U value)
        Create a new instance containing specified value.
        Type Parameters:
        T - First alternative type
        U - Second alternative type
        Parameters:
        value - Value
        Returns:
        A new instance
        Throws:
        NullPointerException - if value is null
      • isFirst

        public final boolean isFirst()
      • getFirst

        public final T getFirst()
      • tryFirst

        public final Optional<T> tryFirst()
      • isSecond

        public final boolean isSecond()
      • getSecond

        public final U getSecond()
      • trySecond

        public final Optional<U> trySecond()
      • hashCode

        public final int hashCode()
        Overrides:
        hashCode in class Object
      • equals

        public final boolean equals​(@Nullable Object obj)
        Overrides:
        equals in class Object