GridGain™ 4.3.1e
Enterprise "Big Data" Edition

org.gridgain.grid.lang
Class GridOpt<T>

java.lang.Object
  extended by org.gridgain.grid.lang.GridLambdaAdapter
      extended by org.gridgain.grid.lang.GridAbsPredicate
          extended by org.gridgain.grid.lang.GridOpt<T>
All Implemented Interfaces:
Serializable, Iterable<T>, GridPeerDeployAware, GridLambda, GridTypedProduct<T>

public class GridOpt<T>
extends GridAbsPredicate
implements GridTypedProduct<T>

This class provide limited version of Scala Option class. It defines an optional reference value (i.e. nullable value in Java). Using this class has two advantages: it indicates to the caller that returned reference can be null, and it gently forces the caller to check for it before using that reference. Note that calling method GridOpt.get() on the object whose method GridOpt.isNone() returns true will produce IllegalArgumentException exception.

Note that this class also implements Iterable interface so it can be used in monadic way with various method in GridFunc class. If this option's GridOpt.isNone() method returns true the iterator will be empty (which can be safely passed into any method that expects the iterable object). If this option's GridOpt.isSome() method returns true then iterator will have just one value (i.e. the value returned by GridOpt.get() method.

See Also:
GridNullOpt, GridFunc.opt(GridOpt, GridInClosure), GridFunc.opt(GridOpt, GridClosure, Object), Serialized Form
 

Method Summary
 boolean apply()
          Predicate body.
 int arity()
          Gets size of this product.
 T get()
          Gets a non-null value.
 T getOrElse(T dflt)
          Gets value if GridOpt.isSome(), otherwise given default value.
 boolean isNone()
          Tests whether this option has a null value.
 boolean isSome()
          Tests whether this option has a non-null value.
 Iterator<T> iterator()
          
static
<T> GridOpt<T>
make(T t)
          Factory method that returns a non-null option if given value is not null, or null option otherwise.
static
<T> GridOpt<T>
none()
          Factory method that creates a null option.
 GridOpt<T> orElse(GridOpt<T> o)
          If GridOpt.isSome() returns this, otherwise given option.
 T part(int n)
          Gets nth part or element for this product.
static
<T> GridOpt<T>
some(T t)
          Factory method that creates a non-null option with given value.
 String toString()
          
 
Methods inherited from class org.gridgain.grid.lang.GridAbsPredicate
andThen, andThen, andThen, uncurry, uncurry2, uncurry3
 
Methods inherited from class org.gridgain.grid.lang.GridLambdaAdapter
classLoader, deployClass, peerDeployLike
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Method Detail

iterator

public Iterator<T> iterator()

Specified by:
iterator in interface Iterable<T>

part

public T part(int n)
Gets nth part or element for this product.

Specified by:
part in interface GridTypedProduct<T>
Parameters:
n - Zero-based index of the part.
Returns:
Nth part or element for this product.

arity

public int arity()
Gets size of this product.

Specified by:
arity in interface GridTypedProduct<T>
Returns:
Size of this product.

apply

public boolean apply()
Predicate body.

Specified by:
apply in class GridAbsPredicate
Returns:
Return value.

isSome

public boolean isSome()
Tests whether this option has a non-null value.

Returns:
true if this option has a non-null value, false otherwise.

isNone

public boolean isNone()
Tests whether this option has a null value.

Returns:
true if this option has a null value, false otherwise.

get

public T get()
Gets a non-null value. If this option contains a null value this method will throw IllegalArgumentException exception. Note that this method will never return null.

Throws:
IllegalArgumentException - Thrown in case if this option contains a null value.
Returns:
A non-null value.

orElse

public GridOpt<T> orElse(GridOpt<T> o)
If GridOpt.isSome() returns this, otherwise given option.

Parameters:
o - Option to return if isNone().
Returns:
If GridOpt.isSome() returns this, otherwise given option.

getOrElse

public T getOrElse(T dflt)
Gets value if GridOpt.isSome(), otherwise given default value.

Parameters:
dflt - Default value to return if isNone().
Returns:
Value if GridOpt.isSome(), otherwise given default value.

some

public static <T> GridOpt<T> some(T t)
Factory method that creates a non-null option with given value.

Throws:
IllegalArgumentException - Thrown if given value is null.
Type Parameters:
T - Type of the value.
Parameters:
t - A non-null value.
Returns:
Option that contains given non-null value.

make

public static <T> GridOpt<T> make(T t)
Factory method that returns a non-null option if given value is not null, or null option otherwise.

Type Parameters:
T - Type of the option value.
Parameters:
t - Option value (may be null).
Returns:
An option.

none

public static <T> GridOpt<T> none()
Factory method that creates a null option. Note that this method returns predefined static final instance.

Returns:
Option that contains null value.

toString

public String toString()

Overrides:
toString in class Object

GridGain™ 4.3.1e
Enterprise "Big Data" Edition

GridGain - In-Memory Big Data
Enterprise "Big Data" Edition, ver. 4.3.1e.10112012
2012 Copyright © GridGain Systems
Follow us:   Follow GridGain on Github Join GridGain User Group Follow GridGain on Twitter Follow GridGain on Vimeo