ru.yandex.bolts.collection
Class Option<T>

java.lang.Object
  extended by java.util.AbstractCollection<E>
      extended by ru.yandex.bolts.collection.impl.AbstractCollectionF<E>
          extended by ru.yandex.bolts.collection.impl.AbstractListF<T>
              extended by ru.yandex.bolts.collection.Option<T>
All Implemented Interfaces:
Serializable, Iterable<T>, Collection<T>, List<T>, CollectionF<T>, IterableF<T>, ListF<T>
Direct Known Subclasses:
Option.None, Option.Some

public abstract class Option<T>
extends AbstractListF<T>
implements Serializable

Port of scala Option.

Author:
Stepan Koltsov
See Also:
Option, Serialized Form

Nested Class Summary
static class Option.None<T>
          None.
static class Option.Some<T>
          Some.
 
Field Summary
 
Fields inherited from class ru.yandex.bolts.collection.impl.AbstractListF
modCount
 
Method Summary
 Option<T> filter(Function1B<? super T> p)
          Return collection with only elements that match predicate.
<B> ListF<B>
flatMap(Function<? super T,? extends Collection<B>> f)
          Flat map
<U> Option<U>
flatMapO(Function<? super T,Option<U>> f)
          Flat map variant that accept mapper that returns Option instead of Collection
abstract  T get()
          Get the value.
 T get(int index)
          Answers the element at the specified location in this List.
static
<U> Function<Option<U>,U>
getF()
          Delegate to get().
static
<U> Function<Option<U>,U>
getM()
          Deprecated.  
 T getOrElse(Function0<T> u)
          If this is some return value of this, or evaluate function and return it value otherwise.
 T getOrElse(T u)
          If this is some return value of this, or return given value otherwise.
 T getOrNull()
          getOrElse(null), but works with types better.
<E extends Throwable>
T
getOrThrow(E e)
          Throw specified exception if isEmpty().
<E extends Throwable>
T
getOrThrow(Function0<E> e)
          Throw specified exception if isEmpty().
 T getOrThrow(String message)
          Throw exception with specified message if this is empty
 T getOrThrow(String message, Object param)
          Get or throw exception if this is empty.
 boolean isDefined()
          true iff this is some.
static
<U> Function1B<Option<U>>
isDefinedF()
          Delegate to isDefined().
static
<U> Function1B<Option<U>>
isDefinedP()
          Deprecated.  
abstract  boolean isEmpty()
          true iff this is none.
static
<U> Function1B<Option<U>>
isEmptyF()
          Delegate to isEmpty().
static
<U> Function1B<Option<U>>
isEmptyP()
          Deprecated.  
<U> Option<U>
map(Function<? super T,U> f)
          Map
static
<T> Option<T>
none()
          Return singleton none object.
static
<T> Option<T>
notNull(T x)
          Some if not null, None otherwise.
static
<T> Function<T,Option<T>>
notNullF()
          Delegate to notNull(Object).
static
<T> Function<T,Option<T>>
notNullM()
          Deprecated.  
 Option<T> orElse(Function0<Option<T>> elseOption)
          this if this is some, or evaluate function and return option otherwise.
 Option<T> orElse(Option<T> elseOption)
          this if this is some, or given option otherwise.
 int size()
           
static
<T> Option<T>
some(T x)
          Construct some containing given value.
 SetF<T> toSet()
           
<F> Option<F>
uncheckedCast()
          This object with different type parameters
 SetF<T> unique()
          Return set of elements of this
 
Methods inherited from class ru.yandex.bolts.collection.impl.AbstractListF
add, add, addAll, clear, collect, drop, emptyCollection, emptyList, equals, filter2, first, firstO, hashCode, honestPlus, indexOf, iterator, last, lastIndexOf, lastO, length, listIterator, listIterator, newMutableCollection, plus, plus, plus, plus, plus1, readOnlyIterator, readOnlyListIterator, remove, removeRange, reverse, reverseIterator, set, subList, take, toList, unmodifiable, zip, zipWithIndex
 
Methods inherited from class ru.yandex.bolts.collection.impl.AbstractCollectionF
addAll, addF, addOp, containsF, containsP, eq, equals, exists, find, foldLeft, foldRight, forAll, forEach, groupBy, max, max, min, min, mkString, mkString, reduceLeft, reduceRight, single, singleO, singletonCollection, sort, sort, sortBy, sortByDesc, toArray, toBooleanArray, toByteArray, toCharArray, toDoubleArray, toFloatArray, toIntArray, toLongArray, toMap, toMapMappingToKey, toMapMappingToValue, toShortArray
 
Methods inherited from class java.util.AbstractCollection
addAll, contains, containsAll, remove, removeAll, retainAll, toArray, toArray, toString
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface ru.yandex.bolts.collection.CollectionF
addAll, addF, addOp, containsF, containsP, groupBy, max, max, min, min, single, singleO, sort, sort, sortBy, sortByDesc, toArray, toBooleanArray, toByteArray, toCharArray, toDoubleArray, toFloatArray, toIntArray, toLongArray, toMap, toMapMappingToKey, toMapMappingToValue, toShortArray
 
Methods inherited from interface ru.yandex.bolts.collection.IterableF
exists, find, foldLeft, foldRight, forAll, forEach, mkString, mkString, reduceLeft, reduceRight
 
Methods inherited from interface java.util.List
addAll, contains, containsAll, remove, removeAll, retainAll, toArray, toArray
 

Method Detail

isEmpty

public abstract boolean isEmpty()
true iff this is none.

Specified by:
isEmpty in interface Collection<T>
Specified by:
isEmpty in interface List<T>
Overrides:
isEmpty in class AbstractCollection<T>

isDefined

public final boolean isDefined()
true iff this is some.


get

public abstract T get()
               throws NoSuchElementException
Get the value.

Throws:
NoSuchElementException - if this is none.

getOrElse

public final T getOrElse(T u)
If this is some return value of this, or return given value otherwise.


getOrElse

public final T getOrElse(Function0<T> u)
If this is some return value of this, or evaluate function and return it value otherwise.


getOrNull

public final T getOrNull()
getOrElse(null), but works with types better.


orElse

public final Option<T> orElse(Option<T> elseOption)
this if this is some, or given option otherwise.


orElse

public final Option<T> orElse(Function0<Option<T>> elseOption)
this if this is some, or evaluate function and return option otherwise.


getOrThrow

public final <E extends Throwable> T getOrThrow(E e)
                   throws E extends Throwable
Throw specified exception if isEmpty().

Throws:
E extends Throwable

getOrThrow

public final <E extends Throwable> T getOrThrow(Function0<E> e)
                   throws E extends Throwable
Throw specified exception if isEmpty().

Throws:
E extends Throwable

getOrThrow

public final T getOrThrow(String message)
                   throws RuntimeException
Throw exception with specified message if this is empty

Throws:
RuntimeException

getOrThrow

public final T getOrThrow(String message,
                          Object param)
                   throws RuntimeException
Get or throw exception if this is empty. Message is constructed by concatenating given params.

Throws:
RuntimeException

size

public int size()
Specified by:
size in interface Collection<T>
Specified by:
size in interface List<T>
Specified by:
size in class AbstractCollection<T>

get

public T get(int index)
Description copied from class: AbstractListF
Answers the element at the specified location in this List.

Specified by:
get in interface List<T>
Specified by:
get in class AbstractListF<T>
Parameters:
index - the index of the element to return
Returns:
the element at the specified index

map

public final <U> Option<U> map(Function<? super T,U> f)
Description copied from interface: CollectionF
Map

Specified by:
map in interface CollectionF<T>
Overrides:
map in class AbstractCollectionF<T>

flatMapO

public final <U> Option<U> flatMapO(Function<? super T,Option<U>> f)
Description copied from interface: CollectionF
Flat map variant that accept mapper that returns Option instead of Collection

Specified by:
flatMapO in interface CollectionF<T>
Overrides:
flatMapO in class AbstractCollectionF<T>

flatMap

public <B> ListF<B> flatMap(Function<? super T,? extends Collection<B>> f)
Description copied from interface: CollectionF
Flat map

Specified by:
flatMap in interface CollectionF<T>
Overrides:
flatMap in class AbstractCollectionF<T>

filter

public final Option<T> filter(Function1B<? super T> p)
Description copied from interface: CollectionF
Return collection with only elements that match predicate.

Specified by:
filter in interface CollectionF<T>
Specified by:
filter in interface ListF<T>
Overrides:
filter in class AbstractListF<T>

toSet

public final SetF<T> toSet()

unique

public SetF<T> unique()
Description copied from interface: CollectionF
Return set of elements of this

Specified by:
unique in interface CollectionF<T>
Overrides:
unique in class AbstractCollectionF<T>

uncheckedCast

public <F> Option<F> uncheckedCast()
This object with different type parameters

Specified by:
uncheckedCast in interface CollectionF<T>
Specified by:
uncheckedCast in interface IterableF<T>
Specified by:
uncheckedCast in interface ListF<T>
Overrides:
uncheckedCast in class AbstractListF<T>

none

public static <T> Option<T> none()
Return singleton none object.


some

public static <T> Option<T> some(T x)
Construct some containing given value.


notNull

public static <T> Option<T> notNull(T x)
Some if not null, None otherwise.


isDefinedP

public static <U> Function1B<Option<U>> isDefinedP()
Deprecated. 


isDefinedF

public static <U> Function1B<Option<U>> isDefinedF()
Delegate to isDefined().


isEmptyP

public static <U> Function1B<Option<U>> isEmptyP()
Deprecated. 


isEmptyF

public static <U> Function1B<Option<U>> isEmptyF()
Delegate to isEmpty().


notNullM

public static <T> Function<T,Option<T>> notNullM()
Deprecated. 


notNullF

public static <T> Function<T,Option<T>> notNullF()
Delegate to notNull(Object).


getM

public static <U> Function<Option<U>,U> getM()
Deprecated. 


getF

public static <U> Function<Option<U>,U> getF()
Delegate to get().