public final class OptionalBoolean
extends java.lang.Object
boolean
value.
If a value is present, isPresent()
will return true
and getAsBoolean()
will return the value.
Additional methods that depend on the presence or absence of a contained value are provided, such as orElse(boolean)
(return a default value if value not present) and ifPresent(BooleanConsumer)
(execute
a block of code if the value is present).
This is a value-based class; use of identity-sensitive operations (including reference equality (==
), identity hash code, or synchronization) on instances of OptionalBoolean
may have unpredictable results
and should be avoided.
Modifier and Type | Method and Description |
---|---|
static OptionalBoolean |
empty()
Returns an empty
OptionalBoolean instance. |
boolean |
equals(java.lang.Object object)
Indicates whether some other
Object is "equal to" this OptionalBoolean . |
boolean |
getAsBoolean()
If a value is present in this
OptionalBoolean , 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(BooleanConsumer consumer)
Have the specified
consumer accept the value if a value is present, otherwise do nothing. |
boolean |
isPresent()
Returns
true if there is a value present, otherwise false . |
static OptionalBoolean |
of(boolean value)
Returns an
OptionalBoolean with the specified value present. |
boolean |
orElse(boolean other)
Returns the value if present, otherwise returns
other . |
boolean |
orElseGet(java.util.function.BooleanSupplier other)
Returns the value if present, otherwise invokes
other and returns the result of that invocation. |
<X extends java.lang.Throwable> |
orElseThrow(java.util.function.Supplier<X> exceptionSupplier)
Returns the contained value, if present, otherwise throws a
Throwable to be created by the provided Supplier . |
public boolean equals(java.lang.Object object)
Object
is "equal to" this OptionalBoolean
.
The other Object
is considered equal if:
OptionalBoolean
and;equals
in class java.lang.Object
object
- an Object
to be tested for equalitytrue
if the other Object
is "equal to" this OptionalBoolean
otherwise false
public boolean getAsBoolean()
OptionalBoolean
, returns the value, otherwise throws NoSuchElementException
.OptionalBoolean
java.util.NoSuchElementException
- if there is no value presentpublic boolean isPresent()
true
if there is a value present, otherwise false
.true
if there is a value present, otherwise false
public boolean orElse(boolean other)
other
.other
- the value to be returned if there is no value presentother
public boolean orElseGet(java.util.function.BooleanSupplier other)
other
and returns the result of that invocation.other
- a BooleanSupplier
whose result is returned if no value is presentother
and returns the result of that invocationjava.lang.NullPointerException
- if value is not present and other
is null
public <X extends java.lang.Throwable> boolean orElseThrow(java.util.function.Supplier<X> exceptionSupplier) throws X extends java.lang.Throwable
Throwable
to be created by the provided Supplier
.X
- the type of the Throwable
to be thrownexceptionSupplier
- the Supplier
which will return the Throwable
to be thrownjava.lang.NullPointerException
- if no value is present and exceptionSupplier
is null
X
- if there is no value presentX extends java.lang.Throwable
public int hashCode()
0
(zero) if no value is present.hashCode
in class java.lang.Object
0
(zero) if no value is presentpublic void ifPresent(BooleanConsumer consumer)
consumer
accept the value if a value is present, otherwise do nothing.consumer
- block to be executed if a value is presentjava.lang.NullPointerException
- if value is present and consumer
is null
public static OptionalBoolean empty()
OptionalBoolean
instance.
No value is present for this OptionalBoolean
.
OptionalBoolean
instancepublic static OptionalBoolean of(boolean value)
OptionalBoolean
with the specified value present.value
- the value to be presentOptionalBoolean
with the specified value present