public final class OptionalInt
extends java.lang.Object
int value.
If a value is present, isPresent() will return true and
getAsInt() will return the value.| Modifier and Type | Method and Description |
|---|---|
<R> R |
custom(Function<OptionalInt,R> function)
Applies custom operator on
OptionalInt. |
static OptionalInt |
empty()
Returns an empty
OptionalInt instance. |
boolean |
equals(java.lang.Object obj)
Indicates whether some other object is "equal to" this OptionalInt.
|
OptionalInt |
executeIfAbsent(java.lang.Runnable action)
Invokes action function if value is absent.
|
OptionalInt |
executeIfPresent(IntConsumer consumer)
Invokes consumer function with value if present.
|
OptionalInt |
filter(IntPredicate predicate)
Performs filtering on inner value if it is present.
|
OptionalInt |
filterNot(IntPredicate predicate)
Performs negated filtering on inner value if it is present.
|
int |
getAsInt()
If a value is present in this
OptionalInt, returns the value,
otherwise throws NoSuchElementException. |
int |
hashCode()
Returns the hash code value of the present value, if any, or 0 (zero) if
no value is present.
|
void |
ifPresent(IntConsumer consumer)
Invokes consumer function with value if present, otherwise does nothing.
|
void |
ifPresentOrElse(IntConsumer consumer,
java.lang.Runnable emptyAction)
If a value is present, performs the given action with the value,
otherwise performs the empty-based action.
|
boolean |
isEmpty()
Checks the value is not present.
|
boolean |
isPresent()
Return
true if there is a value present, otherwise false. |
OptionalInt |
map(IntUnaryOperator mapper)
Invokes mapping function on inner value if present.
|
OptionalDouble |
mapToDouble(IntToDoubleFunction mapper)
Invokes mapping function on inner value if present.
|
OptionalLong |
mapToLong(IntToLongFunction mapper)
Invokes mapping function on inner value if present.
|
<U> Optional<U> |
mapToObj(IntFunction<U> mapper)
Invokes mapping function on inner value if present.
|
static OptionalInt |
of(int value)
Return an
OptionalInt with the specified value present. |
static OptionalInt |
ofNullable(java.lang.Integer value)
Returns an
OptionalInt with the specified value, or empty OptionalInt if value is null. |
OptionalInt |
or(Supplier<OptionalInt> supplier)
Returns current
OptionalInt if value is present, otherwise
returns an OptionalInt produced by supplier function. |
int |
orElse(int other)
Returns the value if present, otherwise returns
other. |
int |
orElseGet(IntSupplier other)
Returns the value if present, otherwise invokes
other and returns
the result of that invocation. |
int |
orElseThrow()
Returns inner value if present, otherwise throws
NoSuchElementException. |
<X extends java.lang.Throwable> |
orElseThrow(Supplier<X> exceptionSupplier)
Returns the value if present, otherwise throws an exception provided by supplier function.
|
IntStream |
stream()
Wraps a value into
IntStream if present, otherwise returns an empty IntStream. |
java.lang.String |
toString()
Returns a non-empty string representation of this object suitable for
debugging.
|
@NotNull public static OptionalInt empty()
OptionalInt instance. No value is present for this
OptionalInt.OptionalInt@NotNull public static OptionalInt of(int value)
OptionalInt with the specified value present.value - the value to be presentOptionalInt with the value present@NotNull public static OptionalInt ofNullable(@Nullable java.lang.Integer value)
OptionalInt with the specified value, or empty OptionalInt if value is null.value - the value which can be nullOptionalIntpublic int getAsInt()
OptionalInt, returns the value,
otherwise throws NoSuchElementException.
Since 1.2.0 prefer orElseThrow() method as it has readable name.OptionalIntjava.util.NoSuchElementException - if there is no value presentisPresent(),
orElseThrow()public boolean isPresent()
true if there is a value present, otherwise false.true if there is a value present, otherwise falsepublic boolean isEmpty()
true if a value is not present, false otherwisepublic void ifPresent(@NotNull
IntConsumer consumer)
consumer - block to be executed if a value is presentjava.lang.NullPointerException - if value is present and consumer is
nullpublic void ifPresentOrElse(@NotNull
IntConsumer consumer,
@NotNull
java.lang.Runnable emptyAction)
consumer - the consumer function to be executed, if a value is presentemptyAction - the empty-based action to be performed, if no value is presentjava.lang.NullPointerException - if a value is present and the given consumer function is null,
or no value is present and the given empty-based action is null.@NotNull public OptionalInt executeIfPresent(@NotNull IntConsumer consumer)
ifPresent, but does not break chainingconsumer - consumer functionOptionalIntifPresent(com.annimon.stream.function.IntConsumer)@NotNull public OptionalInt executeIfAbsent(@NotNull java.lang.Runnable action)
action - action that invokes if value absentOptionalInt@Nullable
public <R> R custom(@NotNull
Function<OptionalInt,R> function)
OptionalInt.R - the type of the resultfunction - a transforming functionjava.lang.NullPointerException - if function is null@NotNull public OptionalInt filter(@NotNull IntPredicate predicate)
predicate - a predicate functionOptionalInt if the value is present and matches predicate,
otherwise an empty OptionalInt@NotNull public OptionalInt filterNot(@NotNull IntPredicate predicate)
predicate - a predicate functionOptionalInt if the value is present and doesn't matches predicate,
otherwise an empty OptionalInt@NotNull public OptionalInt map(@NotNull IntUnaryOperator mapper)
mapper - mapping functionOptionalInt with transformed value if present,
otherwise an empty OptionalIntjava.lang.NullPointerException - if value is present and
mapper is null@NotNull public <U> Optional<U> mapToObj(@NotNull IntFunction<U> mapper)
U - the type of result valuemapper - mapping functionOptional with transformed value if present,
otherwise an empty Optionaljava.lang.NullPointerException - if value is present and
mapper is null@NotNull public OptionalLong mapToLong(@NotNull IntToLongFunction mapper)
mapper - mapping functionOptionalLong with transformed value if present,
otherwise an empty OptionalLongjava.lang.NullPointerException - if value is present and
mapper is null@NotNull public OptionalDouble mapToDouble(@NotNull IntToDoubleFunction mapper)
mapper - mapping functionOptionalDouble with transformed value if present,
otherwise an empty OptionalDoublejava.lang.NullPointerException - if value is present and
mapper is null@NotNull public IntStream stream()
IntStream if present, otherwise returns an empty IntStream.IntStream@NotNull public OptionalInt or(@NotNull Supplier<OptionalInt> supplier)
OptionalInt if value is present, otherwise
returns an OptionalInt produced by supplier function.supplier - supplier function that produces an OptionalInt to be returnedOptionalInt if value is present, otherwise
an OptionalInt produced by supplier functionjava.lang.NullPointerException - if value is not present and
supplier or value produced by it is nullpublic int orElse(int other)
other.other - the value to be returned if there is no value presentotherpublic int orElseGet(@NotNull
IntSupplier other)
other and returns
the result of that invocation.other - a IntSupplier whose result is returned if no value
is presentother.getAsInt()java.lang.NullPointerException - if value is not present and other is
nullpublic int orElseThrow()
NoSuchElementException.java.util.NoSuchElementException - if inner value is not presentpublic <X extends java.lang.Throwable> int orElseThrow(@NotNull
Supplier<X> exceptionSupplier)
throws X extends java.lang.Throwable
X - the type of exception to be thrownexceptionSupplier - supplier function that produces an exception to be thrownX - if inner value is not presentX extends java.lang.Throwablepublic boolean equals(java.lang.Object obj)
OptionalInt and;
==.
equals in class java.lang.Objectobj - an object to be tested for equalitytrue if the other object is "equal to" this object
otherwise falsepublic int hashCode()
hashCode in class java.lang.Object@NotNull public java.lang.String toString()
toString in class java.lang.Object