T
- The type of the optional value.public interface Option<T> extends Value<T>
Optional
.
Option is a monadic container type which
represents an optional value. Instances of Option are either an instance of Some
or the
singleton None
.
Most of the API is taken from Optional
. A similar type can be found in Haskell and Scala.
Modifier and Type | Method and Description |
---|---|
default <U> boolean |
corresponds(Iterable<U> that,
java.util.function.BiPredicate<? super T,? super U> predicate)
Tests whether every element of this iterable relates to the corresponding element of another iterable by
satisfying a test predicate.
|
default boolean |
eq(Object o)
A smoothing replacement for
equals . |
boolean |
equals(Object o)
Clarifies that values have a proper equals() method implemented.
|
default boolean |
exists(java.util.function.Predicate<? super T> predicate)
Checks, if an element exists such that the predicate holds.
|
default Option<T> |
filter(java.util.function.Predicate<? super T> predicate)
Returns
Some(value) if this is a Some and the value satisfies the given predicate. |
default <U> Option<U> |
flatMap(java.util.function.Function<? super T,? extends Iterable<? extends U>> mapper)
Maps the value to a new
Option if this is a Some , otherwise returns None . |
default boolean |
forAll(java.util.function.Predicate<? super T> predicate)
Checks, if the given predicate holds for all elements.
|
default void |
forEach(java.util.function.Consumer<? super T> action)
Performs an action on each element.
|
T |
get()
Gets the underlying value or throws if no value is present.
|
default Option<T> |
getOption()
Gets the underlying as Option.
|
int |
hashCode()
Clarifies that values have a proper hashCode() method implemented.
|
default boolean |
isDefined()
Returns true, if this is
Some , otherwise false, if this is None . |
boolean |
isEmpty()
Returns true, if this is
None , otherwise false, if this is Some . |
default boolean |
isSingletonType()
An option is a singleton type.
|
default Iterator<T> |
iterator()
Returns a rich
javaslang.collection.Iterator . |
default <U> Option<U> |
map(java.util.function.Function<? super T,? extends U> mapper)
Maps the value and wraps it in a new
Some if this is a Some , returns None . |
static <T> Option<T> |
none()
Returns the single instance of
None |
static <T> Option<T> |
of(T value)
Creates a new
Option of a given value. |
static <T> Option<T> |
ofOptional(Optional<? extends T> optional)
Wraps a Java Optional to a new Option
|
default T |
orElse(T other)
Returns the value if this is a
Some or the other value if this is a None . |
default T |
orElseGet(java.util.function.Supplier<? extends T> supplier)
Returns the value if this is a
Some , otherwise the other value is returned,
if this is a None . |
default <X extends Throwable> |
orElseThrow(java.util.function.Supplier<X> exceptionSupplier)
Returns the value if this is a
Some , otherwise throws an exception. |
default Option<T> |
peek(java.util.function.Consumer<? super T> action)
Applies an action to this value, if this option is defined, otherwise does nothing.
|
static <T> Option<T> |
some(T value)
Creates a new
Some of a given value. |
default void |
stderr()
Sends the string representations of this value to the standard error stream System.err.
|
default void |
stdout()
Sends the string representations of this value to the standard output stream System.out.
|
default Object[] |
toJavaArray()
Converts this value to an untyped Java array.
|
String |
toString()
Clarifies that values have a proper toString() method implemented.
|
static <T> Option<T> |
when(boolean condition,
java.util.function.Supplier<? extends T> supplier)
Creates
Some of suppliers value if condition is true, or None in other case |
foldLeft, foldRight, get, ifDefined, ifDefined, ifEmpty, ifEmpty, orElseTry, out, out, toArray, toCharSeq, toJavaArray, toJavaList, toJavaMap, toJavaOptional, toJavaSet, toJavaStream, toLazy, toList, toMap, toOption, toQueue, toSet, toStack, toStream, toTree, toTry, toTry, toVector
fold, fold, foldLeft, foldMap, foldRight
lift, lift, lift, lift, lift, lift, lift, lift
spliterator
static <T> Option<T> of(T value)
Option
of a given value.T
- type of the valuevalue
- A valueSome(value)
if value is not null
, None
otherwisestatic <T> Option<T> some(T value)
Some
of a given value.
The only difference to of(Object)
is, when called with argument null
.
Option.of(null); // = None
Option.some(null); // = Some(null)
T
- type of the valuevalue
- A valueSome(value)
static <T> Option<T> none()
None
T
- component typeNone
static <T> Option<T> when(boolean condition, java.util.function.Supplier<? extends T> supplier)
Some
of suppliers value if condition is true, or None
in other caseT
- type of the optional valuecondition
- A boolean valuesupplier
- An optional value supplier, may supply null
Some
of supplier's value if condition is true, or None
in other caseNullPointerException
- if the given supplier
is nullstatic <T> Option<T> ofOptional(Optional<? extends T> optional)
T
- type of the valueoptional
- a given optional to wrap in Option
Some(optional.get())
if value is Java Optional
is present, None
otherwiseboolean isEmpty()
None
, otherwise false, if this is Some
.default boolean isDefined()
Some
, otherwise false, if this is None
.
Please note that it is possible to create new Some(null)
, which is defined.
default boolean isSingletonType()
isSingletonType
in interface Value<T>
true
T get()
Value
default Option<T> getOption()
Value
default T orElse(T other)
Some
or the other
value if this is a None
.
Please note, that the other value is eagerly evaluated.
default T orElseGet(java.util.function.Supplier<? extends T> supplier)
Some
, otherwise the other
value is returned,
if this is a None
.
Please note, that the other value is lazily evaluated.
default <X extends Throwable> T orElseThrow(java.util.function.Supplier<X> exceptionSupplier) throws X extends Throwable
Some
, otherwise throws an exception.orElseThrow
in interface Value<T>
X
- A throwableexceptionSupplier
- An exception supplierX
- a throwableX extends Throwable
default Option<T> filter(java.util.function.Predicate<? super T> predicate)
Some(value)
if this is a Some
and the value satisfies the given predicate.
Otherwise None
is returned.default <U> Option<U> flatMap(java.util.function.Function<? super T,? extends Iterable<? extends U>> mapper)
Option
if this is a Some
, otherwise returns None
.default <U> Option<U> map(java.util.function.Function<? super T,? extends U> mapper)
Some
if this is a Some
, returns None
.map
in interface Functor<T>
map
in interface Monad<T>
map
in interface Value<T>
U
- The new value typemapper
- A value mapperSome
containing the mapped value if this Option is defined, otherwise None
, if this is empty.default Option<T> peek(java.util.function.Consumer<? super T> action)
boolean equals(Object o)
Value
int hashCode()
Value
See Object.hashCode().
String toString()
Value
See Object.toString().
default <U> boolean corresponds(Iterable<U> that, java.util.function.BiPredicate<? super T,? super U> predicate)
U
- Component type of that iterablethat
- the other iterablepredicate
- the test predicate, which relates elements from both iterablestrue
if both iterables have the same length and predicate(x, y)
is true
for all corresponding elements x
of this iterable and y
of that
,
otherwise false
.default boolean eq(Object o)
equals
. It is similar to Scala's ==
but better in the way
that it is not limited to collection types, e.g. Some(1) eq List(1)
, None eq Failure(x)
etc.
In a nutshell: eq checks congruence of structures and equality of contained values.
Example:
// ((1, 2), ((3))) => structure: (()(())) values: 1, 2, 3
final Iterable<?> i1 = List.of(List.of(1, 2), Arrays.asList(List.of(3)));
final Iterable<?> i2 = Queue.of(Stream.of(1, 2), List.of(Lazy.of(() -> 3)));
assertThat(i1.eq(i2)).isTrue();
Semantics:
o == this : true
o instanceof javaslang.Iterable : iterable elements are eq, non-iterable elements equals, for all (o1, o2) in (this, o)
o instanceof java.lang.Iterable : this eq Iterator.of((java.lang.Iterable<?>) o);
otherwise : false
o
- An objectdefault boolean exists(java.util.function.Predicate<? super T> predicate)
predicate
- A PredicateNullPointerException
- if predicate
is nulldefault boolean forAll(java.util.function.Predicate<? super T> predicate)
predicate
- A PredicateNullPointerException
- if predicate
is nulldefault void forEach(java.util.function.Consumer<? super T> action)
forEach
in interface Iterable<T>
action
- A Consumer
NullPointerException
- if action
is nulldefault Object[] toJavaArray()
default void stderr()
IllegalStateException
- if PrintStream.checkError()
is true after writing to stderr.default void stdout()
IllegalStateException
- if PrintStream.checkError()
is true after writing to stdout.Copyright © 2015. All Rights Reserved.