Class Option<A>

java.lang.Object
org.opencastproject.util.data.Option<A>
All Implemented Interfaces:
Iterable<A>

public abstract class Option<A> extends Object implements Iterable<A>
The option type encapsulates on optional value. It contains either some value or is empty. Please make sure to NEVER wrap null into a some. Instead use none.
  • Method Details

    • fold

      public abstract <B> B fold(Option.Match<A,B> visitor)
      Safe decomposition of the option type.
    • foreach

      public abstract Option<A> foreach(Function<? super A,Void> f)
    • fmap

      public abstract <B> Option<B> fmap(Function<? super A,? extends B> f)
    • map

      public <B> Option<B> map(Function<? super A,? extends B> f)
    • bind

      public abstract <B> Option<B> bind(Function<A,Option<B>> f)
      Monadic bind operation m a -> (a -> m b) -> m b.
    • flatMap

      public <B> Option<B> flatMap(Function<A,Option<B>> f)
    • isSome

      public abstract boolean isSome()
    • isNone

      public boolean isNone()
    • orElse

      public Option<A> orElse(Option<A> none)
      If this is none return none else this.
    • orElse

      public Option<A> orElse(Function0<Option<A>> none)
      Lazy version of orElse(Option).
    • orError

      public <T extends Throwable> Option<A> orError(T none) throws T
      Throw none if none.
      Throws:
      T
    • and

      public <B> Option<Tuple<A,B>> and(Option<B> b)
    • get

      public abstract A get()
      Get the contained value or throw an exception.
    • getOrElse

      public abstract A getOrElse(A none)
      Get the contained value in case of being "some" or return parameter none otherwise.
    • getOrElse

      public abstract A getOrElse(Function0<A> none)
      Get the contained value in case of being "some" or return the result of evaluating none otherwise.
    • getOrElseNull

      public abstract A getOrElseNull()
      To interface with legacy applications or frameworks that still use null values.
    • list

      public abstract List<A> list()
      Transform an option into a list, either with a single element or an empty list.
    • toOpt

      public abstract com.entwinemedia.fn.data.Opt<A> toOpt()
    • hashCode

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

      public abstract boolean equals(Object o)
      Overrides:
      equals in class Object
    • some

      public static <A> Option<A> some(A a)
      Create a new some.
    • none

      public static <A> Option<A> none()
      Create a new none.
    • none

      public static <A> Option<A> none(A example)
      Create a none with the type of example. This saves some nasty typing, e.g. Option.<String>none() vs. none("").

      Please note that this constructor is only due to Java's insufficient type inference.

    • none

      public static <A> Option<A> none(Class<A> clazz)
      Create a none with the given type.
    • option

      public static <A> Option<A> option(A a)
      Wrap an arbitrary object into an option with null being mapped to none.
    • fromOpt

      public static <A> Option<A> fromOpt(com.entwinemedia.fn.data.Opt<A> a)
      Convert an Opt into an Option.
    • error

      @Deprecated public static <A> Function0<A> error(String message)
      Deprecated.
      use orError(Throwable) or orElse(Function0) instead since it saves the need for creating new objects just for the sake of type soundness. Java unfortunately lacks a bottom type.
      Use this function in getOrElse if it is an error being none.
    • eq

      public static Function<String,Boolean> eq(String compare)
      Create an equals function.
         some("abc").map(eq("bcd")).getOrElse(false) // false
         some("abc").map(eq("abc")).getOrElse(false) // true