edu.rice.cs.plt.tuple
Class Option<T>

java.lang.Object
  extended by edu.rice.cs.plt.tuple.Tuple
      extended by edu.rice.cs.plt.tuple.Option<T>
All Implemented Interfaces:
Serializable
Direct Known Subclasses:
Null, Wrapper

public abstract class Option<T>
extends Tuple

A wrapper for optional values. This provides a strictly-typed alternative to using null to represent the absence of a value. Options have two variants: "some" and "none." The "some" case is represented by Wrappers; the "none" case is represented by the Null singleton. Option values may be decomposed by invoking unwrap() or unwrap(Object), or by using an OptionVisitor.

See Also:
Serialized Form

Constructor Summary
protected Option()
           
 
Method Summary
abstract
<Ret> Ret
apply(OptionVisitor<? super T,? extends Ret> visitor)
          Calls the appropriate case in the visitor.
 boolean isNone()
          Determine whether this Option is a "none" case.
abstract  boolean isSome()
          Determine whether this Option is a "some" case.
static
<T> Option<T>
none()
          Return the "none" case singleton, cast to the appropriate type.
static
<T> Option<T>
some(T val)
          Create a "some" case wrapper for the given value.
abstract  T unwrap()
          Get the value wrapped by this Option, or throw an OptionUnwrapException if there is no wrapped value.
static
<T> T
unwrap(Option<? extends T> opt, T forNone)
          A more general form of the instance method unwrap(Object), allowing forNone to have a different type than opt (the result has a common supertype).
abstract  T unwrap(T forNone)
          Get the value wrapped by this Option, or forNone if there is no wrapped value.
static
<T> Option<T>
wrap(T val)
          Treat a possibly-null value as an Option: if the value is null, produce a "none"; otherwise, produce a "some" wrapping the value.
 
Methods inherited from class edu.rice.cs.plt.tuple.Tuple
generateHashCode, hashCode
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Option

protected Option()
Method Detail

apply

public abstract <Ret> Ret apply(OptionVisitor<? super T,? extends Ret> visitor)
Calls the appropriate case in the visitor.


isSome

public abstract boolean isSome()
Determine whether this Option is a "some" case. Mutually exclusive with isNone().


isNone

public final boolean isNone()
Determine whether this Option is a "none" case. Mutually exclusive with isSome().


unwrap

public abstract T unwrap()
                  throws OptionUnwrapException
Get the value wrapped by this Option, or throw an OptionUnwrapException if there is no wrapped value.

Throws:
OptionUnwrapException

unwrap

public abstract T unwrap(T forNone)
Get the value wrapped by this Option, or forNone if there is no wrapped value.


some

public static <T> Option<T> some(T val)
Create a "some" case wrapper for the given value.


none

public static <T> Option<T> none()
Return the "none" case singleton, cast to the appropriate type.


wrap

public static <T> Option<T> wrap(T val)
Treat a possibly-null value as an Option: if the value is null, produce a "none"; otherwise, produce a "some" wrapping the value.


unwrap

public static <T> T unwrap(Option<? extends T> opt,
                           T forNone)
A more general form of the instance method unwrap(Object), allowing forNone to have a different type than opt (the result has a common supertype).