public class Option<T>
extends java.lang.Object
implements java.lang.Iterable<T>
for(Value val : tryGetItem())
list.addItem(val);
// as opposed to
Value val = tryGetItem();
if(val != null)
list.addItem(val);
// or the potential NullPointerException situation:
Value val = tryGetItem();
list.addItem(val);
Example usage (method of a class storing key/value pairs):
class Storage<KeyT, ValueT> {
public Option<ValueT> get(KeyT key) {
ValueT value = find(key);
if(value == null)
return Option.none();
else
return Option.some(value);
}
...
}
Modifier and Type | Class and Description |
---|---|
static class |
Option.EmptyOptionException
A checked exception that is thrown if the value getter
is used on an empty Option.
|
Modifier and Type | Field and Description |
---|---|
private static Option |
none_ |
private T |
value_ |
Modifier | Constructor and Description |
---|---|
private |
Option() |
private |
Option(T val) |
Modifier and Type | Method and Description |
---|---|
void |
apply(Functional.Pr1<? super T> f)
opt.apply(f) is the same as for(Object val : opt) f.invoke(val);
|
boolean |
empty()
Checks whether this instance contains a value.
|
boolean |
equals(java.lang.Object other) |
<R> Option<R> |
flatMap(Functional.Fn1<Option<R>,? super T> f)
Essentially the map operation from functional programming where Option
is a list that contains either 0 or 1 elements.
|
T |
getOrElse(Functional.Fn0<? extends T> f)
Gets the value of this Option or if no value exists returns the result of f
|
T |
getOrElse(T other)
Gets the value of this Option or if no value exists, returns other
|
int |
hashCode() |
static <U,C extends java.util.Collection<U>> |
inserter(C collection)
A convenience procedure that can be used with Option.apply to
add the value of an Option into a Collection:
|
java.util.Iterator<T> |
iterator()
Creates an iterator that treats the Option as a list that contains
either 0 or 1 elements.
|
<R> Option<R> |
map(Delegate<R,? super T> f)
Essentially the map operation from functional programming where Option
is a list that contains either 0 or 1 elements.
|
<R> Option<R> |
map(Functional.Fn1<R,? super T> f)
Essentially the map operation from functional programming where Option
is a list that contains either 0 or 1 elements.
|
<R> R |
mapOrElse(Delegate<R,? super T> f,
Functional.Fn0<R> g)
Maps the value through f or if no value exists returns the result of g
|
<R> R |
mapOrElse(Delegate<R,? super T> f,
R other)
Maps the value through f or if no value exists returns other
|
<R> R |
mapOrElse(Functional.Fn1<R,? super T> f,
Functional.Fn0<R> g)
Maps the value through f or if no value exists returns the result of g
|
<R> R |
mapOrElse(Functional.Fn1<R,? super T> f,
R other)
Maps the value through f or if no value exists returns other
|
static <T> Option<T> |
none()
Returns an Option that represents null
|
Option<T> |
or(Option<? extends T> other)
Replaces this with other if this is empty, otherwise
yields this.
|
Option<T> |
or(T other)
Replaces this with other if this is empty, otherwise
yields this.
|
static <U extends java.lang.Runnable> |
runner()
A convenience procedure that can be used with Option.apply to
run the value of an Option<T extends Runnable>
|
static <T> Option<T> |
some(T val)
Creates an Option that contains a non-null value
|
static <T,C extends java.util.Collection<Option<T>>> |
somes(C col)
Creates a collection containing only the actual values from a collection of options
|
T |
value()
Throws a checked exception if the value is null,
in order to prevent situations where NullPointerException
is thrown from arising.
|
private Option()
private Option(T val)
public static <T> Option<T> some(T val)
The
- value to encapsulatepublic static <T> Option<T> none()
public java.util.Iterator<T> iterator()
iterator
in interface java.lang.Iterable<T>
public <R> Option<R> map(Delegate<R,? super T> f)
f
- A function to call on the value contained in this instance.public <R> Option<R> map(Functional.Fn1<R,? super T> f)
f
- A function to call on the value contained in this instance.public <R> Option<R> flatMap(Functional.Fn1<Option<R>,? super T> f)
f
- A function to call on the value contained in this instance.public void apply(Functional.Pr1<? super T> f)
f
- The function that is invoked if a value existspublic T getOrElse(Functional.Fn0<? extends T> f)
f
- The function to call if this Option is emptypublic T getOrElse(T other)
other
- The value to return if this Option is emptypublic <R> R mapOrElse(Delegate<R,? super T> f, Functional.Fn0<R> g)
f
- The function to call if a value existsg
- The function to call otherwisepublic <R> R mapOrElse(Delegate<R,? super T> f, R other)
f
- The function to call if a value existsother
- The value to return otherwisepublic <R> R mapOrElse(Functional.Fn1<R,? super T> f, Functional.Fn0<R> g)
f
- The function to call if a value existsg
- The function to call otherwisepublic <R> R mapOrElse(Functional.Fn1<R,? super T> f, R other)
f
- The function to call if a value existsother
- The value to return otherwisepublic Option<T> or(Option<? extends T> other)
other
- public Option<T> or(T other)
other
- public T value() throws Option.EmptyOptionException
Option.EmptyOptionException
public boolean empty()
public boolean equals(java.lang.Object other)
equals
in class java.lang.Object
public int hashCode()
hashCode
in class java.lang.Object
public static <T,C extends java.util.Collection<Option<T>>> java.util.Collection<T> somes(C col)
col
- The Collection to filterpublic static <U,C extends java.util.Collection<U>> Functional.Pr1<U> inserter(C collection)
List<String> foo;
getOpt(bar).apply(Option.inserter(foo));
collection
- A Collection into which the value will be added if it existspublic static <U extends java.lang.Runnable> Functional.Pr1<U> runner()
getProc().apply(Option.runner());
Copyright 2004-2015 Wandora Team