Interface Option<T>

  • All Superinterfaces:
    Serializable
    All Known Implementing Classes:
    None, Option.Some

    public interface Option<T>
    extends Serializable
    Indicates presence or absence of a value (null is a valid, present value). None often means "end of stream" or "none found". This is NOT a type-safe null (unless you want that, in which case use the someOrNullNoneOf(Object) static factory method). You can think of this class as OneOf1OrNone. Use Or instead of Option for chaining options or for returning an error.
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Interface Description
      static class  Option.Some<T>
      Represents the presence of a value, even if that value is null.
    • Method Summary

      All Methods Static Methods Instance Methods Abstract Methods 
      Modifier and Type Method Description
      T get()
      Return the value wrapped in this Option.
      T getOrElse​(T t)
      If this is Some, return the value wrapped in this Option.
      boolean isSome()
      Is this Some?
      <U> U match​(@NotNull Fn1<T,​U> has, @NotNull Fn0<U> hasNot)
      Pass in a function to execute if its Some and another to execute if its None.
      static <T> @NotNull Option<T> none()
      Call this instead of referring to None.NONE directly to make the generic types work out.
      static <T> @NotNull Option<T> some​(T t)
      Public static factory method for constructing the Some Option.
      static <T> @NotNull Option<T> someOrNullNoneOf​(T t)
      Construct an option, but if t is null, make it None instead of Some.
      <U> Option<U> then​(Fn1<T,​Option<U>> f)
      If this is Some, Apply the given function, else return None.
    • Method Detail

      • get

        T get()
        Return the value wrapped in this Option. Only safe to call this on Some.
      • getOrElse

        T getOrElse​(T t)
        If this is Some, return the value wrapped in this Option. Otherwise, return the given value.
      • then

        <U> Option<U> then​(Fn1<T,​Option<U>> f)
        If this is Some, Apply the given function, else return None. Use this to chain options together, failing fast at the first none() or continuing through as many operations that return some as possible.
      • isSome

        boolean isSome()
        Is this Some?
      • match

        <U> U match​(@NotNull
                    @NotNull Fn1<T,​U> has,
                    @NotNull
                    @NotNull Fn0<U> hasNot)
        Pass in a function to execute if its Some and another to execute if its None.
      • none

        @NotNull
        static <T> @NotNull Option<T> none()
        Call this instead of referring to None.NONE directly to make the generic types work out.
      • some

        @NotNull
        static <T> @NotNull Option<T> some​(T t)
        Public static factory method for constructing the Some Option.
      • someOrNullNoneOf

        @NotNull
        static <T> @NotNull Option<T> someOrNullNoneOf​(@Nullable
                                                       T t)
        Construct an option, but if t is null, make it None instead of Some.