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 |
---|---|
static OptionalInt |
empty()
Returns an empty
OptionalInt instance. |
boolean |
equals(java.lang.Object obj)
Indicates whether some other object is "equal to" this OptionalInt.
|
int |
get()
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 |
isPresent()
Return
true if there is a value present, otherwise false . |
static OptionalInt |
of(int value)
Return an
OptionalInt with the specified value present. |
int |
or(int other)
Returns the value if present, otherwise returns
other . |
int |
orGet(IntSupplier other)
Returns the value if present, otherwise invokes
other and returns
the result of that invocation. |
<X extends java.lang.Throwable> |
orThrow(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.
|
public static OptionalInt empty()
OptionalInt
instance. No value is present for this
OptionalInt.OptionalInt
public static OptionalInt of(int value)
OptionalInt
with the specified value present.value
- the value to be presentOptionalInt
with the value presentpublic int get()
OptionalInt
, returns the value,
otherwise throws NoSuchElementException
.OptionalInt
java.util.NoSuchElementException
- if there is no value presentisPresent()
public boolean isPresent()
true
if there is a value present, otherwise false
.true
if there is a value present, otherwise false
public void ifPresent(IntConsumer consumer)
consumer
- block to be executed if a value is presentjava.lang.NullPointerException
- if value is present and consumer
is
nullpublic void ifPresentOrElse(IntConsumer consumer, 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.public IntStream stream()
IntStream
if present, otherwise returns an empty IntStream
.IntStream
public int or(int other)
other
.other
- the value to be returned if there is no value presentother
public int orGet(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 <X extends java.lang.Throwable> int orThrow(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.Throwable
public boolean equals(java.lang.Object obj)
OptionalInt
and;
==
.
equals
in class java.lang.Object
obj
- an object to be tested for equalitytrue
if the other object is "equal to" this object
otherwise false
public int hashCode()
hashCode
in class java.lang.Object
public java.lang.String toString()
toString
in class java.lang.Object