long
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
OptionalLong
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
OptionalLong
should never itself be null
; it should always point
to an OptionalLong
instance.
Modifier and Type | Field and Description |
---|---|
private static final OptionalLong | EMPTY
Common instance for |
private final boolean | isPresent
If true then the value is present, otherwise indicates no value is present |
private final long |
Access | Constructor and Description |
---|---|
private | |
private |
Modifier and Type | Method and Description |
---|---|
public static OptionalLong | |
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 long | Returns: the value described by thisOptionalLong 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(LongConsumer
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(LongConsumer
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 OptionalLong | Returns: anOptionalLong with the value presentthe value to describe value)Returns an |
public long | 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 long | 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 long | Returns: the value described by thisOptionalLong If a value is present, returns the value, otherwise throws
|
public < Type of the exception to be thrown X extends Throwable> long | 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 LongStream | Returns: the optional value as anLongStream If a value is present, returns a sequential |
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 OptionalLong 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 long value |
OptionalLong | back to summary |
---|---|
private OptionalLong() Construct an empty instance. Implementation Note generally only one empty instance, |
OptionalLong | back to summary |
---|---|
private OptionalLong(long value) Construct an instance with the described value.
|
empty | back to summary |
---|---|
public static OptionalLong 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
|
getAsLong | back to summary |
---|---|
public long getAsLong() 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(LongConsumer action) If a value is present, performs the given action with the value, otherwise does nothing.
|
ifPresentOrElse | back to summary |
---|---|
public void ifPresentOrElse(LongConsumer 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 OptionalLong of(long value) Returns an
|
orElse | back to summary |
---|---|
public long orElse(long other) If a value is present, returns the value, otherwise returns
|
orElseGet | back to summary |
---|---|
public long orElseGet(LongSupplier supplier) If a value is present, returns the value, otherwise returns the result produced by the supplying function.
|
orElseThrow | back to summary |
---|---|
public long orElseThrow() If a value is present, returns the value, otherwise throws
|
orElseThrow | back to summary |
---|---|
public <X extends Throwable> long 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 LongStream 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 |