E - the type of instance that can be contained. Option is naturally covariant on
this type, so it is safe to cast an Option<T> to Option<S> for any
supertype S of T.public final class Option<E>
extends java.lang.Object
implements java.io.Serializable
null".
A non-null Option<E> reference can be used as a replacement for a nullable T
reference. It allows you to represent "a T that must be present" and a "a T that
might be absent" as two distinct types in your program, which can aid clarity.
Protelis recommends to import and use Option when interacting with Java methods that may return null.
| Modifier and Type | Field and Description |
|---|---|
static java.lang.Object |
JAVA_NULL
Returns the Java null value.
|
| Modifier and Type | Method and Description |
|---|---|
static <E> Option<E> |
absent()
Returns an empty
Option instance. |
java.util.Set<E> |
asSet() |
static <E> Option<E> |
empty()
Returns an empty
Option instance. |
boolean |
equals(java.lang.Object obj) |
Option<E> |
filter(ExecutionContext ctx,
FunctionDefinition test)
Filter operation using Protelis functions.
|
Option<E> |
filter(java.util.function.Predicate<? super E> test) |
Option<E> |
filterNot(ExecutionContext ctx,
FunctionDefinition test)
Inverse filter operation using Protelis functions.
|
Option<E> |
filterNot(java.util.function.Predicate<? super E> test)
Inverse of
filter(Predicate). |
<X> Option<X> |
flatMap(ExecutionContext ctx,
FunctionDefinition fun)
Optional.flatMap(Function) function callable via Protelis. |
<X> Option<X> |
flatMap(java.util.function.Function<? super E,Option<X>> fun) |
static <E> Option<E> |
fromGuava(com.google.common.base.Optional<E> origin) |
static <E> Option<E> |
fromJavaUtil(java.util.Optional<E> origin) |
static <E> Option<E> |
fromNullable(E value)
Returns an
Option describing the specified value, if non-null,
otherwise returns an empty Option. |
E |
get() |
int |
hashCode() |
boolean |
isAbsent() |
boolean |
isEmpty() |
boolean |
isPresent() |
Option<java.lang.Object> |
map(ExecutionContext ctx,
FunctionDefinition mapper) |
<X> Option<X> |
map(java.util.function.Function<? super E,? extends X> mapper) |
Option<E> |
merge(Option<E> other,
java.util.function.BinaryOperator<E> combiner)
If both options are present, the combiner function is executed on the Option
contents.
|
static <E> Option<E> |
of(E value)
Returns an
Option describing the specified value, if non-null,
otherwise returns an empty Option. |
static <E> Option<E> |
ofNullable(E value)
Returns an
Option describing the specified value, if non-null,
otherwise returns an empty Option. |
Option<? extends E> |
or(Option<? extends E> other)
Return the value if present, otherwise return
other. |
Option<? extends E> |
or(java.util.Optional<? extends E> other)
Return the value if present, otherwise return
other. |
Option<? extends E> |
or(com.google.common.base.Optional<? extends E> other)
Return the value if present, otherwise return
other. |
E |
orElse(E other)
Return the value if present, otherwise return
other. |
E |
orElseGet(ExecutionContext ctx,
FunctionDefinition other)
Return the value if present, otherwise invoke
other and return the
result of that invocation. |
E |
orElseGet(java.util.function.Supplier<? extends E> other)
Return the value if present, otherwise invoke
other and return the
result of that invocation. |
<X extends java.lang.RuntimeException> |
orElseThrow(java.util.function.Supplier<? extends X> exceptionSupplier)
Return the contained value, if present, otherwise throw an exception
to be created by the provided supplier.
|
com.google.common.base.Optional<E> |
toGuava() |
java.util.Optional<E> |
toJavaUtil() |
java.lang.String |
toString() |
Option<java.lang.Object> |
transform(ExecutionContext ctx,
FunctionDefinition mapper) |
<X> Option<X> |
transform(java.util.function.Function<? super E,? extends X> mapper) |
public static final java.lang.Object JAVA_NULL
public java.util.Set<E> asSet()
Optionalpublic boolean equals(java.lang.Object obj)
equals in class java.lang.Objectpublic Option<E> filter(ExecutionContext ctx, FunctionDefinition test)
ctx - ExecutionContext the execution contexttest - the function used as predicate. Must return boolean.public Option<E> filter(java.util.function.Predicate<? super E> test)
test - predicate to testOption describing the value of this Option if a
value is present and the value matches the given predicate, otherwise
an empty OptionOptional.filter(Predicate)public Option<E> filterNot(ExecutionContext ctx, FunctionDefinition test)
ctx - ExecutionContext the execution contexttest - the function used as predicate. Must return boolean.public Option<E> filterNot(java.util.function.Predicate<? super E> test)
filter(Predicate).test - predicate to testOption describing the value of this Option if a
value is present and the value does not match the given predicate,
otherwise an empty OptionOptional.filter(Predicate)public <X> Option<X> flatMap(ExecutionContext ctx, FunctionDefinition fun)
Optional.flatMap(Function) function callable via Protelis.X - The type parameter to the Optional returnedctx - ExecutionContextfun - Function to run. Must accept a single parameter and return an
Option, Optional, Optional, or a Java
class with a get() method and one of the following methods
isPresent(), isNotPresent(), isEmpty(), isAbsent():Optional-bearing mapping function
to the value of this Optional, if a value is present,
otherwise an empty Optionalpublic <X> Option<X> flatMap(java.util.function.Function<? super E,Option<X>> fun)
X - The type parameter to the Optional returnedfun - a mapping function to apply to the value, if present the mapping
functionOptional-bearing mapping function
to the value of this Optional, if a value is present,
otherwise an empty OptionalOptional.flatMap(Function)public int hashCode()
hashCode in class java.lang.Objectpublic boolean isAbsent()
public boolean isEmpty()
public boolean isPresent()
public Option<java.lang.Object> map(ExecutionContext ctx, FunctionDefinition mapper)
ctx - ExecutionContextmapper - a mapping function to apply to the value, if presentOption describing the result of applying a mapping
function to the value of this Option, if a value is present,
otherwise an empty Option.public <X> Option<X> map(java.util.function.Function<? super E,? extends X> mapper)
X - The type of the result of the mapping functionmapper - a mapping function to apply to the value, if presentOption describing the result of applying a mapping
function to the value of this Option, if a value is present,
otherwise an empty OptionOptional.map(Function)public Option<E> merge(Option<E> other, java.util.function.BinaryOperator<E> combiner)
other - the other optioncombiner - the combining (reduction) functionpublic Option<? extends E> or(java.util.Optional<? extends E> other)
other.other - alternativeOptional if it has a value present; other
otherwise.public Option<? extends E> or(Option<? extends E> other)
other.other - alternativeOptional if it has a value present; other
otherwise.public Option<? extends E> or(com.google.common.base.Optional<? extends E> other)
other.other - alternativeOptional if it has a value present; other
otherwise.public E orElse(E other)
other.other - the value to be returned if there is no value presentotherpublic E orElseGet(ExecutionContext ctx, FunctionDefinition other)
other and return the
result of that invocation.ctx - ExecutionContextother - a 0-ary protelis functionother.get()public E orElseGet(java.util.function.Supplier<? extends E> other)
other and return the
result of that invocation.other - a Supplier whose result is returned if no value is
presentother.get()public <X extends java.lang.RuntimeException> E orElseThrow(java.util.function.Supplier<? extends X> exceptionSupplier)
X - Type of the exception to be thrownexceptionSupplier - The supplier which will return the exception to
be thrownpublic com.google.common.base.Optional<E> toGuava()
public java.util.Optional<E> toJavaUtil()
Optional view of this Optionpublic java.lang.String toString()
toString in class java.lang.Objectpublic Option<java.lang.Object> transform(ExecutionContext ctx, FunctionDefinition mapper)
ctx - ExecutionContextmapper - a mapping function to apply to the value, if presentOption describing the result of applying a mapping
function to the value of this Option, if a value is present,
otherwise an empty Option.public <X> Option<X> transform(java.util.function.Function<? super E,? extends X> mapper)
X - The type of the result of the mapping functionmapper - a mapping function to apply to the value, if presentOption describing the result of applying a mapping
function to the value of this Option, if a value is present,
otherwise an empty OptionOptional.map(Function)public static <E> Option<E> absent()
Option instance. No value is present for this
Optional.E - Type of the non-existent valueOptionpublic static <E> Option<E> empty()
Option instance. No value is present for this
Optional.E - Type of the non-existent valueOptionpublic static <E> Option<E> fromGuava(com.google.common.base.Optional<E> origin)
E - option typeorigin - the guava Optionalpublic static <E> Option<E> fromJavaUtil(java.util.Optional<E> origin)
E - option typeorigin - the Java Optionalpublic static <E> Option<E> fromNullable(E value)
Option describing the specified value, if non-null,
otherwise returns an empty Option.E - the class of the valuevalue - the possibly-null value to describeOption with a present value if the specified value is
non-null, otherwise an empty Optionpublic static <E> Option<E> of(E value)
Option describing the specified value, if non-null,
otherwise returns an empty Option.E - the class of the valuevalue - the possibly-null value to describeOption with a present value if the specified value is
non-null, otherwise an empty Optionpublic static <E> Option<E> ofNullable(E value)
Option describing the specified value, if non-null,
otherwise returns an empty Option.E - the class of the valuevalue - the possibly-null value to describeOption with a present value if the specified value is
non-null, otherwise an empty Option