public final class OptionalExtensions extends Object
Optional
.
import static extension de.fhg.fokus.xtenders.optional.OptionalExtensions.*
OptionalIntExtensions
,
OptionalLongExtensions
,
OptionalDoubleExtensions
Modifier and Type | Method and Description |
---|---|
static <T> @NonNull Iterable<T> |
asIterable(@NonNull Optional<T> self)
Returns an
Iterable<T> that either provides the one value present in
self , or an Iterable<T> providing no value if self is
empty. |
static <T,U> @NonNull Optional<U> |
filter(@NonNull Optional<T> self,
@NonNull Class<U> clazz)
If a value is present in
self , and the value is instance of the class
clazz , this method returns self casted to
Optional<T> , otherwise returns an empty Optional . |
static void |
forEachInt(@NonNull Optional<? extends IntegerRange> self,
@NonNull IntConsumer consumer)
When parameter
self contains a range, starts an iteration over all
int values in that range. |
static void |
forEachInt(@NonNull Optional<? extends IntegerRange> self,
@NonNull IntIntConsumer consumer)
When parameter
self contains a range, starts an iteration over all
int values in that range. |
static <T> void |
ifAllPresent(@NonNull List<Optional<T>> opts,
@NonNull Procedures.Procedure1<? super List<T>> consumer)
Will call the given consumer If
opts is empty, the consumer
will be called with an empty list. |
static <T,U> void |
ifBothPresent(@NonNull Optional<T> a,
@NonNull Optional<U> b,
@NonNull Procedures.Procedure2<? super T,? super U> consumer)
Calls
consumer with values from a and b , iff both
optionals have a value present. |
static <T> void |
ifNotPresent(@NonNull Optional<T> self,
@NonNull Procedures.Procedure0 then)
Calls the procedure
then if the optional self holds no value. |
static <T> void |
ifPresentOrElse(@NonNull Optional<T> opt,
Consumer<? super T> action,
Runnable emptyAction)
Will call
action with the value held by opt if it is not
empty. |
static <T> @NonNull Iterator<T> |
iterator(@NonNull Optional<T> self)
Returns an
Iterator that does not return a value if self is
empty and an Iterator providing just the value of self if
present |
static <T> @NonNull OptionalDouble |
mapDouble(@NonNull Optional<T> self,
@NonNull ToDoubleFunction<T> mapFunc)
Maps the value of
self to a double value wrapped into an
OptionalDouble , if self holds a value. |
static <T> @NonNull OptionalInt |
mapInt(@NonNull Optional<T> self,
@NonNull ToIntFunction<T> mapFunc)
Maps the value of
self to an int value wrapped into an
OptionalInt , if self holds a value. |
static <T> @NonNull OptionalLong |
mapLong(@NonNull Optional<T> self,
@NonNull ToLongFunction<T> mapFunc)
Maps the value of
self to a long value wrapped into an
OptionalLong , if self holds a value. |
static <T,U> U |
mapOrGet(Optional<T> self,
Functions.Function1<T,U> mapper,
Functions.Function0<U> fallback)
This method is like a shortcut for
self.map(mapper).orElseGet(fallback)
(ignoring the fact that we are using different functional interfaces). |
static <T> @NonNull Optional<T> |
maybe(T t)
Alias for
Optional.ofNullable(Object) . |
static <T> @NonNull Optional<T> |
none()
Alias for
Optional.empty() . |
static <T> T |
operator_elvis(@NonNull Optional<T> o,
Supplier<T> getter)
Alias for
Optional.orElseGet(Supplier) . |
static <T> T |
operator_elvis(@NonNull Optional<T> o,
T alternative)
Alias for
Optional.orElse(Object) . |
static <T> @NonNull Optional<T> |
operator_or(@NonNull Optional<T> self,
@NonNull Optional<? extends T> alternative)
Operator that will be de-sugared to call to
OptionalExtensions.or(self,alternative) . |
static <T> @NonNull Optional<T> |
operator_or(@NonNull Optional<T> self,
@NonNull Supplier<? extends Optional<? extends T>> alternativeSupplier)
Operator that will be de-sugared to call to
OptionalExtensions.or(self,alternativeSupplier) . |
static <T> @NonNull Optional<T> |
or(@NonNull Optional<T> self,
@NonNull Optional<? extends T> alternative)
This method will either return
self if it is not empty, or otherwise
alternative . |
static <T> @NonNull Optional<T> |
or(@NonNull Optional<T> self,
@NonNull Supplier<? extends Optional<? extends T>> alternativeSupplier)
This method will either return
self if it is not empty, or otherwise
the value supplied by alternativeSupplier . |
static <T> Iterable<T> |
orElseEmpty(@NonNull Optional<? extends Iterable<T>> optIter)
If the given optional contains an element, the element will be returned,
otherwise an empty iterable is returned.
|
static <U> @NonNull Optional<U> |
orSuper(@NonNull Optional<? extends U> self,
@NonNull Functions.Function0<Optional<U>> alternativeSupplier)
This method will either return
self if it is not empty, or otherwise
the value supplied by alternativeSupplier . |
static <U> @NonNull Optional<U> |
orSuper(@NonNull Optional<? extends U> self,
@NonNull Optional<U> alternative)
This method will either return
self if it is not empty, or otherwise
alternative . |
static <T> @NonNull Optional<T> |
some(T t)
Alias for
Optional.of(Object) . |
static <T> Stream<T> |
stream(@NonNull Optional<T> self)
This extension function returns a Stream of a single element if the given
Optional
self contains a value, or no element if the Optional is
empty.This is a forward compatibility extension method for the Java 9 feature on Optional. |
static <T> @NonNull List<T> |
toList(@NonNull Optional<T> self)
Returns an immutable list that either contains the value, held by the
optional, or an empty immutable list, if the optional is empty.
|
static <T> @NonNull Set<T> |
toSet(@NonNull Optional<T> self)
Returns an immutable set that either contains the value, held by the
optional, or an empty immutable set, if the optional is empty.
|
static @NonNull OptionalDouble |
unboxDouble(@NonNull Optional<Double> self)
If
self is empty returns an empty OptionalDouble , otherwise
reads the double value from self and returns an
OptionalDouble holding the value. |
static @NonNull OptionalInt |
unboxInt(@NonNull Optional<Integer> self)
If
self is empty returns an empty OptionalInt , otherwise
reads the int value from self and returns an OptionalInt
holding the value. |
static @NonNull OptionalLong |
unboxLong(@NonNull Optional<Long> self)
If
self is empty returns an empty OptionalLong , otherwise
reads the long value from self and returns an OptionalLong
holding the value. |
static <U,T extends U> |
upcast(Optional<T> opt)
Operation to cast
Optional<T> to an Optional<U> where
U is a supertype of T . |
static <T> Else |
whenPresent(@NonNull Optional<T> self,
@NonNull Procedures.Procedure1<? super T> onPresent)
This extension method will check if a value is present in
self and if
so will call onPresent with that value. |
public static void forEachInt(@NonNull Optional<? extends IntegerRange> self, @NonNull IntConsumer consumer)
self
contains a range, starts an iteration over all
int
values in that range. Every value will be passed to
consumer
one after the other. If the Optional is empty, the
consumer
will not be called.self
- Optional that may or may not contain an IntegerRange. If Optional
is not empty, consumer
will be called with each element in
range.consumer
- will be called with each element in given rangepublic static void forEachInt(@NonNull Optional<? extends IntegerRange> self, @NonNull IntIntConsumer consumer)
self
contains a range, starts an iteration over all
int
values in that range. Every value, and the number of the
iteration (starting with index 0
) will be passed to consumer
one after the other. If the Optional is empty, the consumer
will not
be called.self
- Optional that may or may not contain an IntegerRange. If Optional
is not empty, consumer
will be called with each element in
range.consumer
- will be called with each element and the number of iteration
(starting with 0
) in given rangepublic static <T> Iterable<T> orElseEmpty(@NonNull Optional<? extends Iterable<T>> optIter)
T
- type of the elements the iterable possibly wrapped in
optIter
holdsoptIter
- optional that may hold an Iterable.@Pure public static <U> @NonNull Optional<U> orSuper(@NonNull Optional<? extends U> self, @NonNull Optional<U> alternative)
self
if it is not empty, or otherwise
alternative
. If self
is returned, it is casted to
Optional<U>
, which is safe, since the value can only be read from
self
.U
- type of the element that might be wrapped in optional self
or alternative
self
- optional to be checked if empty. If not, this value will be
returned from this method.alternative
- will be returned if self
is empty.self
, if it is not empty, otherwise returns
alternative
.public static <U> @NonNull Optional<U> orSuper(@NonNull Optional<? extends U> self, @NonNull Functions.Function0<Optional<U>> alternativeSupplier)
self
if it is not empty, or otherwise
the value supplied by alternativeSupplier
. If self
is
returned, it is casted to Optional<U>
, which is safe, since the value
can only be read from self
.U
- type of the element that might be wrapped in optional self
or the optional provided by alternativeSupplier
self
- optional to be checked if empty. If not, this value will be
returned from this method.alternativeSupplier
- will be called to get return value if self
is empty.self
, if it is not empty, otherwise returns value supplied by
alternativeSupplier
.@Pure public static <T,U> @NonNull Optional<U> filter(@NonNull Optional<T> self, @NonNull Class<U> clazz)
self
, and the value is instance of the class
clazz
, this method returns self
casted to
Optional<T>
, otherwise returns an empty Optional
.T
- type of element that might be wrapped in optional self
U
- type the element in self
is checked to be instance ofself
- optional that's value is checked to be instance of clazz
clazz
- the type by which the value in self
is filteredself
is empty, or the value held by self
is not
instance of clazz
an empty optional, else self
casted
to Optional<T>
.public static <T> Else whenPresent(@NonNull Optional<T> self, @NonNull Procedures.Procedure1<? super T> onPresent)
self
and if
so will call onPresent
with that value. The returned Else
will allows to perform a block of code if the optional does not hold a value.
Example usage:
val Optional<String> o = Optional.of("some val")
o.whenPresent [
println(it)
].elseDo [
println("no val")
]
T
- type of element that could be wrapped in optional self
self
- if holds value, onPresent
will be executed with the value
held by self
.onPresent
- will be executed with value of self
, if present.Else
that either execute an else block if
self
has no value present, or ignore the else block if the
value is present.public static <T> void ifNotPresent(@NonNull Optional<T> self, @NonNull Procedures.Procedure0 then)
then
if the optional self
holds no value.
This method is equivalent to the following code:
if (!self.isPresent()) {
then.apply();
}
T
- type of element that could be wrapped in optional self
self
- if optional is empty, then
will be called.then
- procedure to be called if self
does not hold a value.public static <T> void ifAllPresent(@NonNull List<Optional<T>> opts, @NonNull Procedures.Procedure1<? super List<T>> consumer)
opts
is empty, the consumer
will be called with an empty list.T
- type of elements that might be held in optionals contained in
opts
opts
- list of optionals. If all optionals hold a value, they get
unpacked and passed to consumer
.consumer
- will be called with the list of values wrapped in the optionals
opts, if all optionals hold a value.public static <T,U> void ifBothPresent(@NonNull Optional<T> a, @NonNull Optional<U> b, @NonNull Procedures.Procedure2<? super T,? super U> consumer)
consumer
with values from a
and b
, iff both
optionals have a value present.T
- type of element that might be wrapped in optional a
U
- type of element that might be wrapped in optional b
a
- value from this optional and value from b
will be used to
call consumer
, if both presentb
- value from this optional and value from a
will be used to
call consumer
, if both presentconsumer
- will be called with values from a
and b
, iff both
present@Pure public static <T> @NonNull Optional<T> maybe(T t)
Optional.ofNullable(Object)
.T
- type of element t
to maybe wrap into a resulting
Optional
.t
- value the possibly-null value to be represented as
Optional
Optional
with value t
present if the specified
parameter t
is not null, otherwise an empty Optional
@Pure public static <T> @NonNull Optional<T> some(T t) throws NullPointerException
Optional.of(Object)
.T
- type of element t
to wrap into an optional.t
- non-null value to be represented as Optional
wrapping the
value.t
as present value.NullPointerException
- if t
is null
.@Pure public static <T> @NonNull Optional<T> none()
Optional.empty()
.T
- generic type of empty optional to be returned.@Pure public static <T> T operator_elvis(@NonNull Optional<T> o, T alternative)
Optional.orElse(Object)
.T
- type of element optional o
may hold and of the
alternative
.o
- optional to be queried for valuealternative
- will be returned if parameter o
is empty.o
has a value present, will return this value. Otherwise
returns alternative
.@Pure public static <T> T operator_elvis(@NonNull Optional<T> o, Supplier<T> getter)
Optional.orElseGet(Supplier)
.T
- type of element that might be wrapped in optional o
o
- optional to be queried for valuegetter
- will be called to get return value if parameter o
is
empty.o
has a value present, will return this value. Otherwise
returns getter
will be called to get return value.public static <T> @NonNull OptionalInt mapInt(@NonNull Optional<T> self, @NonNull ToIntFunction<T> mapFunc)
self
to an int
value wrapped into an
OptionalInt
, if self
holds a value. Returns an empty
OptionalInt
otherwise.T
- type of the value that might be wrapped in optional self
self
- optional, that's held value will be mapped with mapFunc
,
if presentmapFunc
- mapping function, to be applied to value of self
, if
presentself
, mapped to int using
mapFunc
if value present. Empty optional otherwise.public static <T> @NonNull OptionalLong mapLong(@NonNull Optional<T> self, @NonNull ToLongFunction<T> mapFunc)
self
to a long
value wrapped into an
OptionalLong
, if self
holds a value. Returns an empty
OptionalInt
otherwise.T
- type of the value that might be wrapped in optional self
self
- optional, that's held value will be mapped with mapFunc
,
if presentmapFunc
- mapping function, to be applied to value of self
, if
presentself
, mapped to long
using mapFunc
if value present. Empty optional otherwise.public static <T> @NonNull OptionalDouble mapDouble(@NonNull Optional<T> self, @NonNull ToDoubleFunction<T> mapFunc)
self
to a double
value wrapped into an
OptionalDouble
, if self
holds a value. Returns an empty
OptionalInt
otherwise.T
- type of the value that might be wrapped in optional self
self
- optional, that's held value will be mapped with mapFunc
,
if presentmapFunc
- mapping function, to be applied to value of self
, if
presentself
, mapped to long
using mapFunc
if value present. Empty optional otherwise.@Pure public static <T> @NonNull Iterable<T> asIterable(@NonNull Optional<T> self)
Iterable<T>
that either provides the one value present in
self
, or an Iterable<T>
providing no value if self
is
empty.T
- type of the value that might be wrapped in optional self
self
- optional that's value (if present) will be provided via the
returned iterable.Iterable<T>
providing one taken from self
or no
value, if self
is empty.public static <T> @NonNull Iterator<T> iterator(@NonNull Optional<T> self)
Iterator
that does not return a value if self
is
empty and an Iterator
providing just the value of self
if
presentT
- type of the value that might be wrapped in optional self
self
- optional, iterator is returned forself
has a value present or not.@Pure public static <T> @NonNull Set<T> toSet(@NonNull Optional<T> self)
T
- type of the value that might be wrapped in optional self
self
- Optional value is read from@Pure public static <T> @NonNull List<T> toList(@NonNull Optional<T> self)
T
- type of the value that might be wrapped in optional self
self
- Optional value is read from@Pure public static @NonNull OptionalLong unboxLong(@NonNull Optional<Long> self)
self
is empty returns an empty OptionalLong
, otherwise
reads the long value from self
and returns an OptionalLong
holding the value.self
- Optional to be converted to an OptionalLong
OptionalLong
if self
is empty, else an
OptionalLong
holding the long value wrapped in self
.@Pure public static @NonNull OptionalInt unboxInt(@NonNull Optional<Integer> self)
self
is empty returns an empty OptionalInt
, otherwise
reads the int value from self
and returns an OptionalInt
holding the value.self
- Optional to be converted to an OptionalInt
.OptionalInt
if self
is empty, else an
OptionalInt
holding the int value wrapped in self
.@Pure public static @NonNull OptionalDouble unboxDouble(@NonNull Optional<Double> self)
self
is empty returns an empty OptionalDouble
, otherwise
reads the double value from self
and returns an
OptionalDouble
holding the value.self
- Optional to be converted to an OptionalDouble
OptionalDouble
if self
is empty, else an
OptionalDouble
holding the double value wrapped in
self
.@Pure public static <T> @NonNull Optional<T> or(@NonNull Optional<T> self, @NonNull Optional<? extends T> alternative)
self
if it is not empty, or otherwise
alternative
.T
- type of the value that might be wrapped in optional self
self
- optional to be checked if empty. If not, this value will be
returned from this method.alternative
- will be returned if self
is empty.self
, if it is not empty, otherwise returns
alternative
.@Pure public static <T> @NonNull Optional<T> operator_or(@NonNull Optional<T> self, @NonNull Optional<? extends T> alternative)
OptionalExtensions.or(self,alternative)
.T
- type of the value that might be wrapped in optional self
self
- optional to be checked if empty. If not, this value will be
returned from this method.alternative
- will be returned if self
is empty.self
, if it is not empty, otherwise returns
alternative
.public static <T> @NonNull Optional<T> operator_or(@NonNull Optional<T> self, @NonNull Supplier<? extends Optional<? extends T>> alternativeSupplier)
OptionalExtensions.or(self,alternativeSupplier)
.T
- type of the value that might be wrapped in optional self
self
- optional to be checked if empty. If not, this value will be
returned from operator.alternativeSupplier
- will be called to get return value if self
is empty.self
, if it is not empty, otherwise returns value supplied by
alternativeSupplier
.public static <U,T extends U> Optional<U> upcast(Optional<T> opt)
Optional<T>
to an Optional<U>
where
U
is a supertype of T
. This cast is safe, since Optional
never actually uses elements of the generic type and only returns or passes
through elements of the generic type, so that no ClassCastException
will ever be thrown due to this cast.T
- type of the value that might be wrapped in optional opt
U
- super-type of U
; type of the value that might be wrapped the returned optionalopt
- optional to be upcasted to to Optional<U>
.opt
passed in as type Optional<U>
.public static <T,U> U mapOrGet(Optional<T> self, Functions.Function1<T,U> mapper, Functions.Function0<U> fallback)
self.map(mapper).orElseGet(fallback)
(ignoring the fact that we are using different functional interfaces). But the
map
function will produce a wrapper Optional
instance if the
original Optional
contains a value. So this method, additional to allowing
a shorter call chain, may save one object allocation. If either mapper
or
fallback
throws an exception when being executed in this method, the exception
will be thrown out of this method.T
- type of object held by self
U
- result typeself
- original optional that's value is supposed to be mapped to the result value.mapper
- the function mapping to either the result value or fallback
- will be called to get the result value when either self
is an
empty optional, or the mapper
returns a null
value.self
or the value provided by fallback
NullPointerException
- if self
, mapper
or fallback
is null
public static <T> @NonNull Optional<T> or(@NonNull Optional<T> self, @NonNull Supplier<? extends Optional<? extends T>> alternativeSupplier)
self
if it is not empty, or otherwise
the value supplied by alternativeSupplier
.T
- type of the value that might be wrapped in optional self
self
- optional to be checked if empty. If not, this value will be
returned from operator.alternativeSupplier
- will be called to get return value if self
is empty.self
, if it is not empty, otherwise returns value supplied by
alternativeSupplier
.public static <T> void ifPresentOrElse(@NonNull Optional<T> opt, Consumer<? super T> action, Runnable emptyAction)
action
with the value held by opt
if it is not
empty. Otherwise will call emptyAction
.T
- type of the value that might be wrapped in optional opt
opt
- optional to be checked for value.action
- to be called with value of opt
if it is not empty.emptyAction
- to be called if opt
is empty.@Pure public static <T> Stream<T> stream(@NonNull Optional<T> self)
self
contains a value, or no element if the Optional is
empty.T
- type of the value that might be wrapped in optional self
self
- the Optional providing the value for the Stream providedself
Copyright © 2019. All rights reserved.