int
value.
If a value is present, isPresent()
returns true
. If no
value is present, the object is considered empty and
isPresent()
returns false
.
Additional methods that depend on the presence or absence of a contained
value are provided, such as orElse()
(returns a default value if no value is present) and
ifPresent()
(performs an
action if a value is present).
This is a value-based class; programmers should treat instances that are equal as interchangeable and should not use instances for synchronization, or unpredictable behavior may occur. For example, in a future release, synchronization may fail.
API Note
OptionalInt
is primarily intended for use as a method return type where
there is a clear need to represent "no result." A variable whose type is
OptionalInt
should never itself be null
; it should always point
to an OptionalInt
instance.
Modifier and Type | Field and Description |
---|---|
private static final OptionalInt | EMPTY
Common instance for |
private final boolean | isPresent
If true then the value is present, otherwise indicates no value is present |
private final int |
Access | Constructor and Description |
---|---|
private | |
private |
Modifier and Type | Method and Description |
---|---|
public static OptionalInt | |
public boolean | Returns: true if the other object is "equal to" this object
otherwise false an object to be tested for equality obj)Overrides java. Indicates whether some other object is "equal to" this
|
public int | Returns: the value described by thisOptionalInt If a value is present, returns the value, otherwise throws
|
public int | Returns: hash code value of the present value or0 if no value is
presentOverrides java. Returns the hash code of the value, if present, otherwise |
public void | ifPresent(IntConsumer
the action to be performed, if a value is present action)If a value is present, performs the given action with the value, otherwise does nothing. |
public void | ifPresentOrElse(IntConsumer
the action to be performed, if a value is present action, Runnable the empty-based action to be performed, if no value is
present emptyAction)If a value is present, performs the given action with the value, otherwise performs the given empty-based action. |
public boolean | Returns: true if a value is not present, otherwise false If a value is not present, returns |
public boolean | Returns: true if a value is present, otherwise false If a value is present, returns |
public static OptionalInt | Returns: anOptionalInt with the value presentthe value to describe value)Returns an |
public int | Returns: the value, if present, otherwiseother the value to be returned, if no value is present other)If a value is present, returns the value, otherwise returns
|
public int | Returns: the value, if present, otherwise the result produced by the supplying functionthe supplying function that produces a value to be returned supplier)If a value is present, returns the value, otherwise returns the result produced by the supplying function. |
public int | Returns: the value described by thisOptionalInt If a value is present, returns the value, otherwise throws
|
public < Type of the exception to be thrown X extends Throwable> int | Returns: the value, if presentthe supplying function that produces an
exception to be thrown exceptionSupplier)If a value is present, returns the value, otherwise throws an exception produced by the exception supplying function. |
public IntStream | |
public String | Returns: the string representation of this instanceOverrides java. Returns a non-empty string representation of this |
EMPTY | back to summary |
---|---|
private static final OptionalInt EMPTY Common instance for |
isPresent | back to summary |
---|---|
private final boolean isPresent If true then the value is present, otherwise indicates no value is present |
value | back to summary |
---|---|
private final int value |
OptionalInt | back to summary |
---|---|
private OptionalInt() Construct an empty instance. Implementation Note Generally only one empty instance, |
OptionalInt | back to summary |
---|---|
private OptionalInt(int value) Construct an instance with the described value.
|
empty | back to summary |
---|---|
public static OptionalInt empty() Returns an empty API Note Though it may be tempting to do so, avoid testing if an object is empty
by comparing with
|
equals | back to summary |
---|---|
public boolean equals(Object obj) Overrides java. Indicates whether some other object is "equal to" this
|
getAsInt | back to summary |
---|---|
public int getAsInt() If a value is present, returns the value, otherwise throws
API Note The preferred alternative to this method is
|
hashCode | back to summary |
---|---|
public int hashCode() Overrides java. Returns the hash code of the value, if present, otherwise
|
ifPresent | back to summary |
---|---|
public void ifPresent(IntConsumer action) If a value is present, performs the given action with the value, otherwise does nothing.
|
ifPresentOrElse | back to summary |
---|---|
public void ifPresentOrElse(IntConsumer action, Runnable emptyAction) If a value is present, performs the given action with the value, otherwise performs the given empty-based action.
|
isEmpty | back to summary |
---|---|
public boolean isEmpty() If a value is not present, returns
|
isPresent | back to summary |
---|---|
public boolean isPresent() If a value is present, returns
|
of | back to summary |
---|---|
public static OptionalInt of(int value) Returns an
|
orElse | back to summary |
---|---|
public int orElse(int other) If a value is present, returns the value, otherwise returns
|
orElseGet | back to summary |
---|---|
public int orElseGet(IntSupplier supplier) If a value is present, returns the value, otherwise returns the result produced by the supplying function.
|
orElseThrow | back to summary |
---|---|
public int orElseThrow() If a value is present, returns the value, otherwise throws
|
orElseThrow | back to summary |
---|---|
public <X extends Throwable> int orElseThrow(Supplier<? extends X> exceptionSupplier) throws Supplier-:X If a value is present, returns the value, otherwise throws an exception produced by the exception supplying function. API Note A method reference to the exception constructor with an empty argument
list can be used as the supplier. For example,
|
stream | back to summary |
---|---|
public IntStream stream() If a value is present, returns a sequential API Note This method can be used to transform a
|
toString | back to summary |
---|---|
public String toString() Overrides java. Returns a non-empty string representation of this Implementation Specification If a value is present the result must include its string representation
in the result. Empty and present |