org.littleshoot.util
Class OptionalUtilsImpl

java.lang.Object
  extended by org.littleshoot.util.OptionalUtilsImpl
All Implemented Interfaces:
OptionalUtils

public final class OptionalUtilsImpl
extends Object
implements OptionalUtils

An implementation of the OptionalUtils interface.


Constructor Summary
OptionalUtilsImpl()
           
 
Method Summary
<T> void
executeOnSome(Optional<T> optional, Closure<T> closure)
          Executes a given closure on an optional object.
<T> Collection<T>
filterNones(Collection<Optional<T>> c)
          Filters out the None elements in a collection and returns a collection of the objects inside the remaining Some elements.
<T> boolean
isNone(Optional<T> optional)
          Returns whether an optional object is a None object.
 boolean isSome(Optional optional)
          Returns whether an optional object is a Some object.
 Optional optionalOf(Object object)
          Returns an Optional from a possible null object.
<T> T
someObject(Optional<T> optional)
          Returns the object stored in an optional object if one exists.
<T1,T2> Optional<T2>
transformSome(Optional<T1> optional, Transformer<T1,T2> transformer)
          Returns an Optional object created by transforming the contents of another Optional object.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

OptionalUtilsImpl

public OptionalUtilsImpl()
Method Detail

executeOnSome

public <T> void executeOnSome(Optional<T> optional,
                              Closure<T> closure)
Executes a given closure on an optional object.

Specified by:
executeOnSome in interface OptionalUtils
Type Parameters:
T - The type of object held by the optional object.
Parameters:
optional - The optional object.
closure - The closure to execute on the object held by the optional object, if it exists.

filterNones

public <T> Collection<T> filterNones(Collection<Optional<T>> c)
Filters out the None elements in a collection and returns a collection of the objects inside the remaining Some elements.

Specified by:
filterNones in interface OptionalUtils
Type Parameters:
T - The type of the objects inside the some objects.
Parameters:
c - The collection to filter.
Returns:
A collection of type T elements.

optionalOf

public Optional optionalOf(Object object)
Returns an Optional from a possible null object. If the object is null, None is returned. Otherwise, a Some object containing the given object is returned.

Specified by:
optionalOf in interface OptionalUtils
Parameters:
object - The object, possible null.
Returns:
If object is null, then None is returned, otherwise a Some containing object is returned.

transformSome

public <T1,T2> Optional<T2> transformSome(Optional<T1> optional,
                                          Transformer<T1,T2> transformer)
Returns an Optional object created by transforming the contents of another Optional object. If the given Optional is a None, then None is returned. If the given Optional is a Some, then the contents of the Some are transformed and the result wrapped in a Some is returned.

Specified by:
transformSome in interface OptionalUtils
Parameters:
optional - The Optional object to be transformed.
transformer - The transformer used to transform the contents of a Some object.
Returns:
An Optional object whose contents, if they exists, have been transformed by the given transformer.

isNone

public <T> boolean isNone(Optional<T> optional)
Returns whether an optional object is a None object.

Specified by:
isNone in interface OptionalUtils
Parameters:
optional - The optional object.
Returns:
True if optional is a None, false otherwise.

isSome

public boolean isSome(Optional optional)
Returns whether an optional object is a Some object.

Specified by:
isSome in interface OptionalUtils
Parameters:
optional - The optional object.
Returns:
True if optional is a Some, false otherwise.

someObject

public <T> T someObject(Optional<T> optional)
             throws IllegalArgumentException
Returns the object stored in an optional object if one exists. Otherwise, an exception is thrown.

Specified by:
someObject in interface OptionalUtils
Parameters:
optional - The optional object.
Returns:
The object stored in the optional object, if it exists.
Throws:
IllegalArgumentException - If the optional object is not a Some object.


Copyright © 2011-2013 LittleShoot. All Rights Reserved.