public final class OptionalIntExtensions extends Object
OptionalInt
.
import static extension de.fhg.fokus.xtenders.optional.OptionalIntExtensions.*
OptionalExtensions
,
OptionalLongExtensions
,
OptionalDoubleExtensions
Modifier and Type | Method and Description |
---|---|
static @NonNull OptionalDouble |
asDouble(@NonNull OptionalInt self)
Returns
OptionalDouble that holds the value from self casted to double , or
an empty OptionalDouble if self is empty. |
static @NonNull IntIterable |
asIterable(@NonNull OptionalInt self)
Returns an
IntIterable that either provides the one value present in self ,
or an IntIterable providing no value if self is empty. |
static @NonNull OptionalLong |
asLong(@NonNull OptionalInt self)
Returns
OptionalLong that holds the up-casted int value from self , or
an empty OptionalLong if self is empty. |
static @NonNull Optional<Integer> |
boxed(@NonNull OptionalInt self)
Returns an
Optional<Integer> holding the value of self , or returns
an empty optional, if the given OptionalInt is empty. |
static @NonNull OptionalInt |
filter(@NonNull OptionalInt self,
@NonNull IntPredicate predicate)
If the given
OptionalInt self holds a value and the value tests positive with the given
IntPredicate predicate , returns self , otherwise returns an empty OptionalInt . |
static @NonNull OptionalInt |
flatMapInt(@NonNull OptionalInt self,
@NonNull IntFunction<OptionalInt> mapFunc)
Maps the value of
self to an OptionalInt using mapFunc , if self has a present
value. |
static void |
ifNotPresent(@NonNull OptionalInt self,
@NonNull Procedures.Procedure0 then)
Calls the procedure
then if the optional self holds no
value. |
static void |
ifPresentOrElse(OptionalInt self,
IntConsumer action,
Runnable emptyAction)
Will call
action with the value held by self if it is not
empty. |
static @NonNull PrimitiveIterator.OfInt |
iterator(@NonNull OptionalInt self)
Returns an
PrimitiveIterable.OfInt that either provides the one value present in self ,
or an PrimitiveIterable.OfInt providing no value if self is empty. |
static <V> @NonNull Optional<V> |
map(@NonNull OptionalInt self,
@NonNull IntFunction<V> op)
Maps the value held by
self to an object wrapped in Optional if present,
returns an empty Optional otherwise. |
static @NonNull OptionalDouble |
mapDouble(@NonNull OptionalInt self,
@NonNull IntToDoubleFunction mapFunc)
Maps the value of
self to a double value wrapped into an OptionalDouble , if self holds a value. |
static @NonNull OptionalInt |
mapInt(@NonNull OptionalInt self,
@NonNull IntUnaryOperator mapFunc)
Maps the value of
self to an int value wrapped into an OptionalInt , if self holds a value. |
static @NonNull OptionalLong |
mapLong(@NonNull OptionalInt self,
@NonNull IntToLongFunction mapFunc)
Maps the value of
self to a long value wrapped into an OptionalLong , if self holds a value. |
static @NonNull OptionalInt |
maybe(@Nullable Integer i)
This method is a shortcut for the following expression:
|
static @NonNull OptionalInt |
noInt()
Alias for
OptionalInt.empty() . |
static int |
operator_elvis(@NonNull OptionalInt o,
int alternative)
This operator is an alias for:
|
static int |
operator_elvis(@NonNull OptionalInt o,
IntSupplier getter)
Alias for
OptionalInt.orElseGet(IntSupplier) . |
static @NonNull OptionalInt |
operator_or(@NonNull OptionalInt self,
@NonNull Supplier<? extends OptionalInt> alternativeSupplier)
Operator that will be de-sugared to call to
OptionalIntExtensions.or(self,alternativeSupplier) . |
static @NonNull OptionalInt |
or(@NonNull OptionalInt self,
@NonNull Supplier<? extends OptionalInt> alternativeSupplier)
This method will either return
self if it is not empty, or
otherwise the value supplied by alternativeSupplier . |
static @NonNull OptionalInt |
some(int i)
This method is an alias for
OptionalInt.of(int) . |
static OptionalInt |
someOf(int i)
This method returns an OptionalInt instance wrapping the given int
i . |
static @NonNull IntStream |
stream(@NonNull OptionalInt 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. |
static @NonNull Set<Integer> |
toSet(@NonNull OptionalInt self)
Returns an immutable set that either contains the value, held by the
optional, or an empty set, if the optional is empty.
|
static Else |
whenPresent(@NonNull OptionalInt self,
@NonNull IntConsumer onPresent)
This extension method will check if a value is present in
self and if so will call onPresent
with that value. |
public static Else whenPresent(@NonNull OptionalInt self, @NonNull IntConsumer 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 OptionalInt o = OptionalInt.of(42)
o.whenPresent [
println(it)
].elseDo [
println("no val")
]
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 void ifNotPresent(@NonNull OptionalInt 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();
}
self
- if optional is empty, then
will be called.then
- procedure to be called if self
does not hold a value.@Pure public static @NonNull OptionalInt some(int i)
OptionalInt.of(int)
.i
- integer to wrap in an OptionalInt
OptionalInt
, having present value i
.public static OptionalInt someOf(int i)
i
.
The returned value might be cached to achieve object re-use and therefore
less garbage collection.OptionalInt.of(int)
or a
new factory method adds caching of instances itself.i
- integer to be wrapped in OptionalInti
.@Pure public static @NonNull OptionalInt noInt()
OptionalInt.empty()
.OptionalInt
with no value present.@Pure public static @NonNull OptionalInt maybe(@Nullable Integer i)
i == null ? OptionalInt.empty() : OptionalInt.of(i)
i
- an Integer that is checked for null
. If i == null
, then
returns an empty OptionalInt
. If i != null
returns an OptionalInt
holding the integer value.OptionalInt
holding i
, if i != null
, an empty OptionalInt
otherwise.@Pure public static int operator_elvis(@NonNull OptionalInt o, int alternative)
o.orElse
(alternative)
o
- checked for a present valuealternative
- value to be returned by this extension function, oif o
is empty.o
, or alternative
if there is no
value present in o
.@Pure public static int operator_elvis(@NonNull OptionalInt o, IntSupplier getter)
OptionalInt.orElseGet(IntSupplier)
.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.@Pure public static @NonNull Optional<Integer> boxed(@NonNull OptionalInt self)
Optional<Integer>
holding the value of self
, or returns
an empty optional, if the given OptionalInt
is empty.self
- value will be extracted from this OptionalInt
, if value is present
and wrapped into an Integer
which will be returned in an Optional
from
this method.Optional<Integer>
holding the value of self
if present, empty
Optional
otherwisepublic static @NonNull OptionalInt filter(@NonNull OptionalInt self, @NonNull IntPredicate predicate)
OptionalInt self
holds a value and the value tests positive with the given
IntPredicate predicate
, returns self
, otherwise returns an empty OptionalInt
.self
- the optional value that will be filtered using predicate
predicate
- the test that will be used to filter self
.OptionalInt
, empty optional if self
is empty or it's content
tests negative with predicate
. Otherwise returns self
.@Pure public static @NonNull OptionalLong asLong(@NonNull OptionalInt self)
OptionalLong
that holds the up-casted int value from self
, or
an empty OptionalLong
if self
is empty.self
- the value to be converted to an OptionalLong
.self
wrapped in OptionalLong
if present,
an empty OptionalLong
otherwise.@Pure public static @NonNull OptionalDouble asDouble(@NonNull OptionalInt self)
OptionalDouble
that holds the value from self
casted to double
, or
an empty OptionalDouble
if self
is empty.self
- the value to be converted to an OptionalDouble
.self
wrapped in OptionalDouble
if present,
an empty OptionalDouble
otherwise.public static <V> @NonNull Optional<V> map(@NonNull OptionalInt self, @NonNull IntFunction<V> op)
self
to an object wrapped in Optional
if present,
returns an empty Optional
otherwise.V
- Type the given op
mapping function will map to from int
values.
The returned optional might hold a value of this type if self
actually holds a value.self
- the optional, that's value should be mapped if presentop
- mapping function mapping the value held by self
to an objectOptional
, if self
is empty, otherwise an Optional
holding the result of op
applied to the value held by self
.public static @NonNull OptionalInt mapInt(@NonNull OptionalInt self, @NonNull IntUnaryOperator mapFunc)
self
to an int
value wrapped into an OptionalInt
, if self
holds a value.
Returns an empty OptionalInt
otherwise.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 @NonNull OptionalInt flatMapInt(@NonNull OptionalInt self, @NonNull IntFunction<OptionalInt> mapFunc)
self
to an OptionalInt
using mapFunc
, if self
has a present
value. If self
is empty, this method returns an empty OptionalInt
self
- possibly holding value to be mapped with mapFunc
mapFunc
- mapping function, used on value of self
if a value is present.mapFunc
applied to value of self
if
value is present, otherwise returning empty optional.public static @NonNull OptionalLong mapLong(@NonNull OptionalInt self, @NonNull IntToLongFunction mapFunc)
self
to a long
value wrapped into an OptionalLong
, if self
holds a value.
Returns an empty OptionalInt
otherwise.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 @NonNull OptionalDouble mapDouble(@NonNull OptionalInt self, @NonNull IntToDoubleFunction mapFunc)
self
to a double
value wrapped into an OptionalDouble
, if self
holds a value.
Returns an empty OptionalDouble
otherwise.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 @NonNull IntIterable asIterable(@NonNull OptionalInt self)
IntIterable
that either provides the one value present in self
,
or an IntIterable
providing no value if self
is empty.self
- optional that's value (if present) will be provided via the returned iterable.IntIterable
providing one value taken from self
or no value, if self
is empty.public static @NonNull PrimitiveIterator.OfInt iterator(@NonNull OptionalInt self)
PrimitiveIterable.OfInt
that either provides the one value present in self
,
or an PrimitiveIterable.OfInt
providing no value if self
is empty.self
- optional that's value (if present) will be provided via the returned iterator.PrimitiveIterable.OfInt
providing one value taken from self
or no value, if self
is empty.@Pure public static @NonNull Set<Integer> toSet(@NonNull OptionalInt self)
self
- Optional value is read frompublic static @NonNull IntStream stream(@NonNull OptionalInt self)
self
contains a value, or no element if the optional is
empty.self
- the optional providing the value for the Stream providedself
public static void ifPresentOrElse(OptionalInt self, IntConsumer action, Runnable emptyAction)
action
with the value held by self
if it is not
empty. Otherwise will call emptyAction
.self
- 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.public static @NonNull OptionalInt operator_or(@NonNull OptionalInt self, @NonNull Supplier<? extends OptionalInt> alternativeSupplier)
OptionalIntExtensions.or(self,alternativeSupplier)
.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 @NonNull OptionalInt or(@NonNull OptionalInt self, @NonNull Supplier<? extends OptionalInt> alternativeSupplier)
self
if it is not empty, or
otherwise the value supplied by alternativeSupplier
.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
.Copyright © 2019. All rights reserved.