jedi.option
Interface Option<T>

All Superinterfaces:
Iterable<T>, Serializable
All Known Implementing Classes:
None, Some

public interface Option<T>
extends Iterable<T>, Serializable

An optional value inspired by sensible languages like Scala.


Method Summary
 List<T> asList()
          An empty list for None or an immutable list with Some.get().
 Option<T> filter(Filter<? super T> f)
          If this option is Some and the given functor f yields false on its value, return None.
<R> Option<R>
flatMap(Functor<? super T,Option<R>> mappingFunction)
           
 void forEach(Command<? super T> command)
          Apply the given Command to the option's value if it is not None.
 T getOrElse(Functor0<? extends T> generator)
          If the option is Some return its value, otherwise return the result of evaluating the generator expression.
 T getOrElse(T defaultValue)
          If the option is Some return its value, otherwise return a default value.
<R> Option<R>
map(Functor<? super T,R> mappingFunction)
          If the option is Some, return a function applied to its value, wrapped in a Some: Some(f(this.get)), otherwise return None.
 void match(Command<? super T> someCommand, Command0 noneCommand)
          A match strategy based on Commands.
<R1,R2 extends R1>
R1
match(Functor<? super T,R1> someFunctor, Functor0<R2> noneFunctor)
          A match strategy based on Functors.
 void match(OptionMatcher<? super T> matcher)
          Accept a for this Option.
 T unsafeGet()
          Get the value returning null for None.
 
Methods inherited from interface java.lang.Iterable
iterator
 

Method Detail

match

void match(OptionMatcher<? super T> matcher)
Accept a for this Option. In languages like Scala we can do this kind of thing:
 x match {
   case Some(s) => s
   case None => "?"
 }
 
In Java, such elegance is not possible so this method is actually a Visitor implementation:
 x.match(new OptionMatcher() {
   public void caseSome(T value) { // excellent we can use the value }
   public void caseNone() { // no value, deal with it }
 });
 


match

void match(Command<? super T> someCommand,
           Command0 noneCommand)
A match strategy based on Commands. Since annotated methods become commands in Jedi, this approach is a very simple solution to dealing with Some and None.

Parameters:
someCommand - a command that will receive the callback in the event that Some is matched
noneCommand - a command that will receive the callback in the event that None is matched

match

<R1,R2 extends R1> R1 match(Functor<? super T,R1> someFunctor,
                            Functor0<R2> noneFunctor)
A match strategy based on Functors.

Parameters:
someFunctor - the functor to execute if this Option is a Same
noneFunctor - the functor to execute if this Option is a None
Returns:
the result of executing the functor

asList

List<T> asList()
An empty list for None or an immutable list with Some.get().


getOrElse

T getOrElse(Functor0<? extends T> generator)
If the option is Some return its value, otherwise return the result of evaluating the generator expression.

Parameters:
generator - the default expression.

getOrElse

T getOrElse(T defaultValue)
If the option is Some return its value, otherwise return a default value.


map

<R> Option<R> map(Functor<? super T,R> mappingFunction)
If the option is Some, return a function applied to its value, wrapped in a Some: Some(f(this.get)), otherwise return None.

Parameters:
mappingFunction - the function to apply

flatMap

<R> Option<R> flatMap(Functor<? super T,Option<R>> mappingFunction)

forEach

void forEach(Command<? super T> command)
Apply the given Command to the option's value if it is not None. Do nothing if it is None.

Parameters:
command - the Command to apply.

filter

Option<T> filter(Filter<? super T> f)
If this option is Some and the given functor f yields false on its value, return None. Otherwise return this option.

Parameters:
f - the filter used for testing.

unsafeGet

T unsafeGet()
Get the value returning null for None.



Copyright © 2011. All Rights Reserved.