Package org.protelis.lang.datatype
Class Option<E>
- java.lang.Object
-
- org.protelis.lang.datatype.Option<E>
-
- Type Parameters:
E- the type of instance that can be contained.Optionis naturally covariant on this type, so it is safe to cast anOption<T>toOption<S>for any supertypeSofT.
- All Implemented Interfaces:
java.io.Serializable
public final class Option<E> extends java.lang.Object implements java.io.SerializableAn immutable object that may contain a non-null reference to another object. Each instance of this type either contains a non-null reference, or contains nothing (in which case we say that the reference is "absent"); it is never said to "containnull".A non-null
Option<E>reference can be used as a replacement for a nullableTreference. It allows you to represent "aTthat must be present" and a "aTthat 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.- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.ObjectJAVA_NULLReturns the Java null value.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static <E> Option<E>absent()Returns an emptyOptioninstance.java.util.Set<E>asSet()static <E> Option<E>empty()Returns an emptyOptioninstance.booleanequals(java.lang.Object obj)Option<E>filter(java.util.function.Predicate<? super E> test)Option<E>filter(ExecutionContext ctx, FunctionDefinition test)Filter operation using Protelis functions.Option<E>filterNot(java.util.function.Predicate<? super E> test)Inverse offilter(Predicate).Option<E>filterNot(ExecutionContext ctx, FunctionDefinition test)Inverse filter operation using Protelis functions.<X> Option<X>flatMap(java.util.function.Function<? super E,Option<X>> fun)<X> Option<X>flatMap(ExecutionContext ctx, FunctionDefinition fun)Optional.flatMap(Function)function callable via Protelis.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 anOptiondescribing the specified value, if non-null, otherwise returns an emptyOption.Eget()inthashCode()booleanisAbsent()booleanisEmpty()booleanisPresent()<X> Option<X>map(java.util.function.Function<? super E,? extends X> mapper)Option<java.lang.Object>map(ExecutionContext ctx, FunctionDefinition 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 anOptiondescribing the specified value, if non-null, otherwise returns an emptyOption.static <E> Option<E>ofNullable(E value)Returns anOptiondescribing the specified value, if non-null, otherwise returns an emptyOption.Option<? extends E>or(com.google.common.base.Optional<? extends E> other)Return the value if present, otherwise returnother.Option<? extends E>or(java.util.Optional<? extends E> other)Return the value if present, otherwise returnother.Option<? extends E>or(Option<? extends E> other)Return the value if present, otherwise returnother.EorElse(E other)Return the value if present, otherwise returnother.EorElseGet(java.util.function.Supplier<? extends E> other)Return the value if present, otherwise invokeotherand return the result of that invocation.EorElseGet(ExecutionContext ctx, FunctionDefinition other)Return the value if present, otherwise invokeotherand return the result of that invocation.<X extends java.lang.RuntimeException>
EorElseThrow(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.StringtoString()<X> Option<X>transform(java.util.function.Function<? super E,? extends X> mapper)Option<java.lang.Object>transform(ExecutionContext ctx, FunctionDefinition mapper)
-
-
-
Method Detail
-
asSet
public java.util.Set<E> asSet()
- Returns:
- a set representation of the Option, for compatibility with
Optional
-
equals
public boolean equals(java.lang.Object obj)
- Overrides:
equalsin classjava.lang.Object
-
filter
public Option<E> filter(ExecutionContext ctx, FunctionDefinition test)
Filter operation using Protelis functions.- Parameters:
ctx-ExecutionContextthe execution contexttest- the function used as predicate. Must return boolean.- Returns:
- If the test passes, the Option is unchanged. Otherwise, it's emptied.
-
filter
public Option<E> filter(java.util.function.Predicate<? super E> test)
- Parameters:
test- predicate to test- Returns:
- an
Optiondescribing the value of thisOptionif a value is present and the value matches the given predicate, otherwise an emptyOption - See Also:
Optional.filter(Predicate)
-
filterNot
public Option<E> filterNot(ExecutionContext ctx, FunctionDefinition test)
Inverse filter operation using Protelis functions.- Parameters:
ctx-ExecutionContextthe execution contexttest- the function used as predicate. Must return boolean.- Returns:
- If the test passes, the Option is emptied, otherwise, it's left unchanged.
-
filterNot
public Option<E> filterNot(java.util.function.Predicate<? super E> test)
Inverse offilter(Predicate).- Parameters:
test- predicate to test- Returns:
- an
Optiondescribing the value of thisOptionif a value is present and the value does not match the given predicate, otherwise an emptyOption - See Also:
Optional.filter(Predicate)
-
flatMap
public <X> Option<X> flatMap(ExecutionContext ctx, FunctionDefinition fun)
Optional.flatMap(Function)function callable via Protelis.- Type Parameters:
X- The type parameter to theOptionalreturned- Parameters:
ctx-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():- Returns:
- the result of applying an
Optional-bearing mapping function to the value of thisOptional, if a value is present, otherwise an emptyOptional
-
flatMap
public <X> Option<X> flatMap(java.util.function.Function<? super E,Option<X>> fun)
- Type Parameters:
X- The type parameter to theOptionalreturned- Parameters:
fun- a mapping function to apply to the value, if present the mapping function- Returns:
- the result of applying an
Optional-bearing mapping function to the value of thisOptional, if a value is present, otherwise an emptyOptional - See Also:
Optional.flatMap(Function)
-
hashCode
public int hashCode()
- Overrides:
hashCodein classjava.lang.Object
-
isAbsent
public boolean isAbsent()
- Returns:
- true if the Option has no value
-
isEmpty
public boolean isEmpty()
- Returns:
- true if the Option has no value
-
isPresent
public boolean isPresent()
- Returns:
- true if the Option is holding a value
-
map
public Option<java.lang.Object> map(ExecutionContext ctx, FunctionDefinition mapper)
- Parameters:
ctx-ExecutionContextmapper- a mapping function to apply to the value, if present- Returns:
- an
Optiondescribing the result of applying a mapping function to the value of thisOption, if a value is present, otherwise an emptyOption.
-
map
public <X> Option<X> map(java.util.function.Function<? super E,? extends X> mapper)
- Type Parameters:
X- The type of the result of the mapping function- Parameters:
mapper- a mapping function to apply to the value, if present- Returns:
- an
Optiondescribing the result of applying a mapping function to the value of thisOption, if a value is present, otherwise an emptyOption - See Also:
Optional.map(Function)
-
merge
public 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. Otherwise, if an Option is present, it is returned. Finally, if both are empty, an empty Option is returned.- Parameters:
other- the other optioncombiner- the combining (reduction) function- Returns:
- Option.of(combiner(this.get(), other.get()) if both are present, the only present option iff one is present, an empty option otherwise
-
or
public Option<? extends E> or(java.util.Optional<? extends E> other)
Return the value if present, otherwise returnother.- Parameters:
other- alternative- Returns:
- this
Optionalif it has a value present;otherotherwise.
-
or
public Option<? extends E> or(Option<? extends E> other)
Return the value if present, otherwise returnother.- Parameters:
other- alternative- Returns:
- this
Optionalif it has a value present;otherotherwise.
-
or
public Option<? extends E> or(com.google.common.base.Optional<? extends E> other)
Return the value if present, otherwise returnother.- Parameters:
other- alternative- Returns:
- this
Optionalif it has a value present;otherotherwise.
-
orElse
public E orElse(E other)
Return the value if present, otherwise returnother.- Parameters:
other- the value to be returned if there is no value present- Returns:
- the value, if present, otherwise
other
-
orElseGet
public E orElseGet(ExecutionContext ctx, FunctionDefinition other)
Return the value if present, otherwise invokeotherand return the result of that invocation.- Parameters:
ctx-ExecutionContextother- a 0-ary protelis function- Returns:
- the value if present otherwise the result of
other.get()
-
orElseGet
public E orElseGet(java.util.function.Supplier<? extends E> other)
Return the value if present, otherwise invokeotherand return the result of that invocation.- Parameters:
other- aSupplierwhose result is returned if no value is present- Returns:
- the value if present otherwise the result of
other.get()
-
orElseThrow
public <X extends java.lang.RuntimeException> E 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.- Type Parameters:
X- Type of the exception to be thrown- Parameters:
exceptionSupplier- The supplier which will return the exception to be thrown- Returns:
- the present value
-
toGuava
public com.google.common.base.Optional<E> toGuava()
- Returns:
- a Guava compatible view of this Option
-
toJavaUtil
public java.util.Optional<E> toJavaUtil()
- Returns:
- a
Optionalview of this Option
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
transform
public Option<java.lang.Object> transform(ExecutionContext ctx, FunctionDefinition mapper)
- Parameters:
ctx-ExecutionContextmapper- a mapping function to apply to the value, if present- Returns:
- an
Optiondescribing the result of applying a mapping function to the value of thisOption, if a value is present, otherwise an emptyOption.
-
transform
public <X> Option<X> transform(java.util.function.Function<? super E,? extends X> mapper)
- Type Parameters:
X- The type of the result of the mapping function- Parameters:
mapper- a mapping function to apply to the value, if present- Returns:
- an
Optiondescribing the result of applying a mapping function to the value of thisOption, if a value is present, otherwise an emptyOption - See Also:
Optional.map(Function)
-
absent
public static <E> Option<E> absent()
Returns an emptyOptioninstance. No value is present for this Optional.- Type Parameters:
E- Type of the non-existent value- Returns:
- an empty
Option
-
empty
public static <E> Option<E> empty()
Returns an emptyOptioninstance. No value is present for this Optional.- Type Parameters:
E- Type of the non-existent value- Returns:
- an empty
Option
-
fromGuava
public static <E> Option<E> fromGuava(com.google.common.base.Optional<E> origin)
- Type Parameters:
E- option type- Parameters:
origin- the guava Optional- Returns:
- a Protelis view of the Guava Option
-
fromJavaUtil
public static <E> Option<E> fromJavaUtil(java.util.Optional<E> origin)
- Type Parameters:
E- option type- Parameters:
origin- the Java Optional- Returns:
- a Protelis view of the Java Option
-
fromNullable
public static <E> Option<E> fromNullable(E value)
Returns anOptiondescribing the specified value, if non-null, otherwise returns an emptyOption.- Type Parameters:
E- the class of the value- Parameters:
value- the possibly-null value to describe- Returns:
- an
Optionwith a present value if the specified value is non-null, otherwise an emptyOption
-
of
public static <E> Option<E> of(E value)
Returns anOptiondescribing the specified value, if non-null, otherwise returns an emptyOption.- Type Parameters:
E- the class of the value- Parameters:
value- the possibly-null value to describe- Returns:
- an
Optionwith a present value if the specified value is non-null, otherwise an emptyOption
-
ofNullable
public static <E> Option<E> ofNullable(E value)
Returns anOptiondescribing the specified value, if non-null, otherwise returns an emptyOption.- Type Parameters:
E- the class of the value- Parameters:
value- the possibly-null value to describe- Returns:
- an
Optionwith a present value if the specified value is non-null, otherwise an emptyOption
-
-