Class Possible<T>

  • Type Parameters:
    T - The type of the stored value.

    public final class Possible<T>
    extends Object
    A container for a value which may or may not be absent. If isAbsent() is true, no value is present.

    Possible is extremely similar to Optional and exists solely for (de)serialization purposes. By combining Possible and Optional, we can model the four different possibilities for field "types" used by Discord.

    Field name (in Discord docs) Type (in Discord docs) Java Type Meaning
    normal_field T T Always present, never null
    optional_field? T Possible<T> Sometimes present, but if present, never null
    nullable_field ?T Optional<T> Always present, but possibly null
    optional_and_nullable_field? ?T Possible<Optional<T>> Somtimes present, and even if present, possibly null

    You probably don't need to use this class!

    • Method Detail

      • of

        public static <T> Possible<T> of​(T value)
        Returns a Possible with the given value.
        Type Parameters:
        T - The type the value.
        Parameters:
        value - The value the returned Possible will store.
        Returns:
        A Possible with the given value.
      • absent

        public static <T> Possible<T> absent()
        Returns a Possible with no value.
        Type Parameters:
        T - The type of the non-existent value.
        Returns:
        An absent Possible.
      • flatOpt

        public static <T> Optional<T> flatOpt​(Possible<Optional<T>> possible)
        Flattens a Possible<Optional<T>> into a Optional<T>. The returned Optional contains a value iff the given Possible is not absent and its stored Optional is present.
        Type Parameters:
        T - The type of the inner value.
        Parameters:
        possible - The Possible to flatten.
        Returns:
        An Optional containing if the inner value, if any.
      • isAbsent

        public boolean isAbsent()
        If no value is stored, returns true, otherwise false.
        Returns:
        True if no value is stored, otherwise false.
      • get

        public T get()
        Gets the stored value, if any.
        Returns:
        The stored value, if any.
        Throws:
        NoSuchElementException - If no value is stored.
      • toOptional

        public Optional<T> toOptional()
        Converts this Possible to an Optional. The returned Optional is present iff this Possible is not absent.
        Returns:
        An Optional containing this Possible's value, if it has one.
      • equals

        public boolean equals​(@Nullable
                              Object o)
        Overrides:
        equals in class Object
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class Object