public final class OptionalInt extends 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(Object obj)
Indicates whether some other object is "equal to" this OptionalInt.
|
OptionalInt |
executeIfAbsent(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,
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(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 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. |
String |
toString()
Returns a non-empty string representation of this object suitable for
debugging.
|
public static OptionalInt empty()
OptionalInt instance. No value is present for this
OptionalInt.OptionalIntpublic static OptionalInt of(int value)
OptionalInt with the specified value present.value - the value to be presentOptionalInt with the value presentpublic static OptionalInt ofNullable(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.OptionalIntNoSuchElementException - 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(IntConsumer consumer)
consumer - block to be executed if a value is presentNullPointerException - if value is present and consumer is
nullpublic void ifPresentOrElse(IntConsumer consumer, 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 presentNullPointerException - 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.public OptionalInt executeIfPresent(IntConsumer consumer)
ifPresent, but does not break chainingconsumer - consumer functionOptionalIntifPresent(com.annimon.stream.function.IntConsumer)public OptionalInt executeIfAbsent(Runnable action)
action - action that invokes if value absentOptionalIntpublic <R> R custom(Function<OptionalInt,R> function)
OptionalInt.R - the type of the resultfunction - a transforming functionNullPointerException - if function is nullpublic OptionalInt filter(IntPredicate predicate)
predicate - a predicate functionOptionalInt if the value is present and matches predicate,
otherwise an empty OptionalIntpublic OptionalInt filterNot(IntPredicate predicate)
predicate - a predicate functionOptionalInt if the value is present and doesn't matches predicate,
otherwise an empty OptionalIntpublic OptionalInt map(IntUnaryOperator mapper)
mapper - mapping functionOptionalInt with transformed value if present,
otherwise an empty OptionalIntNullPointerException - if value is present and
mapper is nullpublic <U> Optional<U> mapToObj(IntFunction<U> mapper)
U - the type of result valuemapper - mapping functionOptional with transformed value if present,
otherwise an empty OptionalNullPointerException - if value is present and
mapper is nullpublic OptionalLong mapToLong(IntToLongFunction mapper)
mapper - mapping functionOptionalLong with transformed value if present,
otherwise an empty OptionalLongNullPointerException - if value is present and
mapper is nullpublic OptionalDouble mapToDouble(IntToDoubleFunction mapper)
mapper - mapping functionOptionalDouble with transformed value if present,
otherwise an empty OptionalDoubleNullPointerException - if value is present and
mapper is nullpublic IntStream stream()
IntStream if present, otherwise returns an empty IntStream.IntStreampublic OptionalInt or(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 functionNullPointerException - 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(IntSupplier other)
other and returns
the result of that invocation.other - a IntSupplier whose result is returned if no value
is presentother.getAsInt()NullPointerException - if value is not present and other is
nullpublic int orElseThrow()
NoSuchElementException.NoSuchElementException - if inner value is not presentpublic <X extends Throwable> int orElseThrow(Supplier<X> exceptionSupplier) throws X extends 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 Throwablepublic boolean equals(Object obj)
OptionalInt and;
==.
public int hashCode()
Copyright © 2018. All rights reserved.