public abstract class

Option

extends Object
implements Supplier<T> Iterable<T>
java.lang.Object
   ↳ com.atlassian.plugin.util.Option<A>

Class Overview

A class that encapsulates null (missing) values. An Option may be either Option.Some some value or None not.

If it is a value it may be tested with the isDefined() method, but more often it is useful to either return the value or an alternative if not set, or map or filter.

Mapping a None of type A to type B will simply return None of type B if performed on a None of type A. Similarly, filtering will always fail on a None.

While this class is public and abstract it does not expose a constructor as only the concrete Option.Some Some and None subclasses are meant to be used.

Summary

Public Methods
static <T> Predicate<T> defined()
Filters defined options only.
final boolean equals(Object obj)
final boolean exists(Predicate<A> p)
Returns this Option if it is nonempty and applying the predicate to this option's value returns true.
final Option<A> filter(Predicate<? super A> p)
Returns this Option if it is nonempty and applying the predicate to this option's value returns true.
static <T> Iterable<Option<T>> filterNone(Iterable<Option<T>> options)
Filter out undefined options.
static <T> Option<T> find(Iterable<Option<T>> options)
Find the first option that isDefined, or if there aren't any, then none.
final <B> Option<B> flatMap(Function<? super A, Option<B>> f)
Apply f to the value if defined.
abstract <B> B fold(Supplier<? extends B> none, Function<? super A, B> some)
If this is a some value apply the some function, otherwise get the none value.
abstract A get()
Get the value if defined.
final A getOrElse(Supplier<A> supplier)
Get the value if defined or call the supplier and return its value if not.
final <B extends A> A getOrElse(B other)
Get the value if defined, otherwise returns other.
final A getOrNull()
Get the value if defined or null if not.
final int hashCode()
abstract boolean isDefined()
final boolean isEmpty()
final Iterator<A> iterator()
final <B> Option<B> map(Function<? super A, B> f)
Apply f to the value if defined.
static <A> Option<A> none()
Factory method for None instances.
static <A> Option<A> none(Class<A> type)
Factory method for None instances where the type token is handy.
static <A> Supplier<Option<A>> noneSupplier()
Supplies None as required.
static <A> Option<A> option(A a)
Factory method for Option instances.
static <A> Option<A> some(A value)
Factory method for Option.Some Some instances.
final String toString()
[Expand]
Inherited Methods
From class java.lang.Object
From interface com.google.common.base.Supplier
From interface java.lang.Iterable

Public Methods

public static Predicate<T> defined ()

Filters defined options only.

Returns
  • the filtered options

public final boolean equals (Object obj)

public final boolean exists (Predicate<A> p)

Returns this Option if it is nonempty and applying the predicate to this option's value returns true. Otherwise, return none().

Parameters
p the predicate to test

public final Option<A> filter (Predicate<? super A> p)

Returns this Option if it is nonempty and applying the predicate to this option's value returns true. Otherwise, return none().

Parameters
p the predicate to test

public static Iterable<Option<T>> filterNone (Iterable<Option<T>> options)

Filter out undefined options.

Parameters
options many options that may or may not be defined
Returns
  • the filtered options

public static Option<T> find (Iterable<Option<T>> options)

Find the first option that isDefined, or if there aren't any, then none.

Parameters
options an Iterable of options to search through

public final Option<B> flatMap (Function<? super A, Option<B>> f)

Apply f to the value if defined.

Transforms to an option of the functions result type.

Parameters
f function to apply to wrapped value
Returns
  • value returned from f

public abstract B fold (Supplier<? extends B> none, Function<? super A, B> some)

If this is a some value apply the some function, otherwise get the none value.

Parameters
none the supplier of the None type
some the function to apply if this is a Option.Some Some
Returns
  • the appropriate value

public abstract A get ()

Get the value if defined. Throw an exception otherwise.

Returns
  • the wrapped value
Throws
NoSuchElementException if this is a none

public final A getOrElse (Supplier<A> supplier)

Get the value if defined or call the supplier and return its value if not.

Returns
  • the wrapped value or the value from the Supplier

public final A getOrElse (B other)

Get the value if defined, otherwise returns other.

Parameters
other value to return if this is a none
Returns
  • wrapped value if this is a Option.Some Some, otherwise returns other

public final A getOrNull ()

Get the value if defined or null if not.

Although the use of null is discouraged, code written to use Option must often interface with code that expects and returns nulls.

public final int hashCode ()

public abstract boolean isDefined ()

Returns
  • true if this is a Option.Some Some, false otherwise.

public final boolean isEmpty ()

Returns
  • false if this is a Option.Some Some, true otherwise.

public final Iterator<A> iterator ()

Returns
  • a single element iterator if this is a Option.Some Some, an empty one otherwise.

public final Option<B> map (Function<? super A, B> f)

Apply f to the value if defined.

Transforms to an option of the functions result type.

Parameters
f function to apply to wrapped value
Returns
  • new wrapped value

public static Option<A> none ()

Factory method for None instances.

Returns
  • a None

public static Option<A> none (Class<A> type)

Factory method for None instances where the type token is handy. Allows calling in-line where the type inferencer would otherwise complain.

Parameters
type token of the right type, unused, only here for the type inferencer
Returns
  • a None

public static Supplier<Option<A>> noneSupplier ()

Supplies None as required. Useful as the zero value for folds.

Returns
  • a Supplier of None instances

public static Option<A> option (A a)

Factory method for Option instances.

Parameters
a the value to hold
Returns
  • a Option.Some Some if the parameter is not null or a None if it is

public static Option<A> some (A value)

Factory method for Option.Some Some instances.

Parameters
value the value to hold
Returns
  • a Option.Some Some if the parameter is not null
Throws
NullPointerException if the parameter is null

public final String toString ()