public final class OptionalBoolean extends Object
boolean
value. If 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 is not present) and ifPresent(IntConsumer)
(execute a block of code if the value is present).
Modifier and Type | Method and Description |
---|---|
static OptionalBoolean |
empty()
Returns an empty OptionalBoolean instance.
|
boolean |
equals(Object obj)
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 zero if no value is present.
|
void |
ifPresent(IntConsumer action)
If a value is present, passes the value to the provided consumer, otherwise does nothing.
|
void |
ifPresentOrElse(IntConsumer action, Runnable emptyAction)
If a value is present, passes the value to the provided consumer, otherwise executes the provided action.
|
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(BooleanSupplier other)
Returns the value if present, otherwise invokes other and returns the result of that invocation.
|
boolean |
orElseThrow()
Returns the contained value if present, otherwise throws NoSuchElementException .
|
<X extends Throwable> |
orElseThrow(Supplier<X> exceptionSupplier)
Returns the contained value, if present, otherwise throws an exception created by the provided supplier.
|
IntStream |
stream()
If a value is present, returns the value in a stream, otherwise returns an empty stream.
|
OptionalInt |
toInt()
Widens the optional boolean value to OptionalInt .
|
String |
toString()
Returns string representation of this instance suitable for debugging.
|
public static OptionalBoolean empty()
OptionalBoolean
instance. No value is present for this OptionalBoolean
. There is only one instance of empty OptionalBoolean
.
OptionalBoolean
public static OptionalBoolean of(boolean value)
OptionalBoolean
with the specified value present. There are only two global instances of OptionalBoolean
with value present. This method just returns one of them.
value
- - the value to be present
OptionalBoolean
with the value present
public boolean isPresent()
true
if there is a value present, otherwise false
.
true
if there is a value present, otherwise false
public boolean getAsBoolean()
OptionalBoolean
, returns the value, otherwise throws NoSuchElementException
.
OptionalBoolean
NoSuchElementException
- if there is no value present
isPresent()
public boolean orElse(boolean other)
other
.
other
- the value to be returned if there is no value present
other
public boolean orElseGet(BooleanSupplier other)
other
and returns the result of that invocation.
other
- a BooleanSupplier
whose result is returned if no value is present
other.getAsBoolean()
NullPointerException
- if value is not present and other
is null
public <X extends Throwable> boolean orElseThrow(Supplier<X> exceptionSupplier) throws X extends Throwable
IllegalStateException::new
.
X
- type of the exception to be thrown
exceptionSupplier
- the supplier which will return the exception to be thrown
X
- if there is no value present
NullPointerException
- if no value is present and exceptionSupplier
is null
X extends Throwable
public boolean orElseThrow()
NoSuchElementException
.
NoSuchElementException
- if there is no value present
public IntStream stream()
BooleanStream
in JDK. The boolean value is therefore widened to int
and returned in an IntStream
. Value of true
is widened to 1
, false
to 0
.
IntStream
public void ifPresent(IntConsumer action)
BooleanConsumer
in JDK. The boolean value is therefore widened to int
and passed to IntConsumer
. Value of true
is widened to 1
, false
to 0
.
action
- code to be executed if a value is present
NullPointerException
- if value is present and action
is null
public void ifPresentOrElse(IntConsumer action, Runnable emptyAction)
BooleanConsumer
in JDK. The boolean value is therefore widened to int
and passed to IntConsumer
. Value of true
is widened to 1
, false
to 0
.
action
- code to be executed if a value is present
emptyAction
- code to be executed if no value is present
NullPointerException
- if value is present and action
is null
public OptionalInt toInt()
OptionalInt
. JDK consistently uses OptionalInt
where OptionalBoolean
would be semantically correct. This class can consequently cause compatibility problems with other code that expects OptionalInt
. This method widens the boolean value to int
as wrapped in OptionalInt
to improve compatibility. Value of true
is widened to 1
, false
to 0
.
OptionalInt
public boolean equals(Object obj)
OptionalBoolean
. The other object is considered equal if it is also an OptionalBoolean
and both instances have no value present or the present values are the same.
equals
in class Object
obj
- an object to be tested for equality
true
if the other object is "equal to" this object, otherwise false
hashCode()
public int hashCode()
hashCode
in class Object
equals(Object)
, Boolean.hashCode()
Copyright © 2017–2020 Robert Važan. All rights reserved.