|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectnet.sf.staccatocommons.lang.Option<A>
A - the type of optional value@Value
@Conditionally(value={net.sf.staccatocommons.restrictions.value.Immutable.class,java.io.Serializable.class})
public abstract class Option<A>
Option represent optional values, that can either be instances of
Some or None.
Option type should be used
Map.get(Object), returns null if there is no
object mapped for the given key. This kind of code can be tricky, as clients
may ignore this and continue using the returned value without checking it.
What is more, such client code can even consider null values valid and
usable, and then a return value of null has an ambiguous meaning. Using
Options removes ambiguity.Option instead, which traits nulls and not-set values not the
same wayNullPointerException.
Option subclasses redefine Object.equals(Object) and Object.hashCode(),
so that two instances are equal as long they values are equal, or as long
they are undefined.
| Method Summary | ||
|---|---|---|
Option<A> |
filter(net.sf.staccatocommons.defs.Evaluable<? super A> predicate)
Answers this option if defined and the given predicate
evaluates to true. |
|
void |
forEach(net.sf.staccatocommons.defs.Executable<? super A> block)
|
|
abstract void |
ifDefined(net.sf.staccatocommons.defs.Executable<? super A> block)
Executed the given block if this option is defined |
|
abstract boolean |
isDefined()
Returns if the value has been defined or not. |
|
boolean |
isUndefined()
|
|
|
map(net.sf.staccatocommons.defs.Applicable<? super A,? extends T2> function)
Applies the given function to the Option's value and wraps it
into an Option, if defined. |
|
static
|
none()
Factory method for creating an undefined value. |
|
static
|
nullToNone(T value)
Creates an option (defined or not), mapping null values to undefined options, and non nulls to defined option. |
|
static
|
some(T value)
Factory method for creating defined values.This method does not guarantee either to return the same or different instances for the same argument. |
|
static
|
someNull()
Factory method for creating defined, null values. |
|
abstract A |
value()
Gets the optional value, if defined, or throws an NoSuchElementException, otherwise. |
|
abstract A |
valueOrElse(A other)
Returns the value of this Option, or the provided object if
undefined |
|
abstract A |
valueOrElse(net.sf.staccatocommons.defs.Thunk<? extends A> other)
Returns the value of this Option, or the provided object if
undefined |
|
abstract A |
valueOrNull()
Returns the value of this Option, or null, if
undefined. |
|
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Methods inherited from interface java.lang.Iterable |
|---|
iterator |
| Methods inherited from interface net.sf.staccatocommons.defs.partial.SizeAware |
|---|
isEmpty, size |
| Methods inherited from interface net.sf.staccatocommons.defs.partial.ContainsAware |
|---|
contains |
| Methods inherited from interface net.sf.staccatocommons.defs.ProtoMonad |
|---|
skip |
| Method Detail |
|---|
public abstract A value()
throws NoSuchElementException
NoSuchElementException, otherwise.
value in interface net.sf.staccatocommons.defs.Thunk<A>NoSuchElementException - if this option is undefined, and thus there is no value.public abstract boolean isDefined()
public boolean isUndefined()
isDefined()public final <T2> Option<T2> map(net.sf.staccatocommons.defs.Applicable<? super A,? extends T2> function)
function to the Option's value and wraps it
into an Option, if defined. Returns none(), otherwise
map in interface net.sf.staccatocommons.defs.ProtoMonad<Option,A>T2 - function -
Optionpublic final Option<A> filter(net.sf.staccatocommons.defs.Evaluable<? super A> predicate)
predicate
evaluates to true. Answers none(), otherwise
filter in interface net.sf.staccatocommons.defs.ProtoMonad<Option,A>predicate -
@Constant public static <T> None<T> none()
T - the type of optional value
None instancepublic abstract A valueOrElse(A other)
Option, or the provided object if
undefined
other - the return value in case this Option is undefined
this.value() if defined, other otherwisepublic abstract A valueOrElse(net.sf.staccatocommons.defs.Thunk<? extends A> other)
Option, or the provided object if
undefined
other - the thunk of the return value in case this Option is
undefined
this.value() if defined, other.value()
otherwisepublic abstract A valueOrNull()
Option, or null, if
undefined.
this.value() if defined, or null,
otherwise
public abstract void ifDefined(@NonNull
net.sf.staccatocommons.defs.Executable<? super A> block)
block -
public void forEach(@NonNull
net.sf.staccatocommons.defs.Executable<? super A> block)
forEach in interface net.sf.staccatocommons.defs.ProtoMonad<Option,A>public static <T> Some<T> some(T value)
T - the type of optional valuevalue - May be null (although is discouraged). See value() for
details
public static <T> Some<T> someNull()
T -
public static <T> Option<T> nullToNone(T value)
T - value -
value != null ? Option.some(value) : Option.none()
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||