- java.lang.Object
-
- com.machinezoo.noexception.optional.OptionalBoolean
-
public final class OptionalBoolean extends Object
A container object which may or may not contain aboolean
value. If value is present,isPresent()
will returntrue
andgetAsBoolean()
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) andifPresent(IntConsumer)
(execute a block of code if the value is present).
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static OptionalBoolean
empty()
Returns an emptyOptionalBoolean
instance.boolean
equals(Object obj)
Indicates whether some other object is "equal to" thisOptionalBoolean
.boolean
getAsBoolean()
If a value is present in thisOptionalBoolean
, returns the value, otherwise throwsNoSuchElementException
.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()
Returnstrue
if there is a value present, otherwisefalse
.static OptionalBoolean
of(boolean value)
Returns anOptionalBoolean
with the specified value present.boolean
orElse(boolean other)
Returns the value if present, otherwise returnsother
.boolean
orElseGet(BooleanSupplier other)
Returns the value if present, otherwise invokesother
and returns the result of that invocation.boolean
orElseThrow()
Returns the contained value if present, otherwise throwsNoSuchElementException
.<X extends Throwable>
booleanorElseThrow(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 toOptionalInt
.String
toString()
Returns string representation of this instance suitable for debugging.
-
-
-
Method Detail
-
empty
public static OptionalBoolean empty()
Returns an emptyOptionalBoolean
instance. No value is present for thisOptionalBoolean
. There is only one instance of emptyOptionalBoolean
.- Returns:
-
an empty
OptionalBoolean
-
of
public static OptionalBoolean of(boolean value)
Returns anOptionalBoolean
with the specified value present. There are only two global instances ofOptionalBoolean
with value present. This method just returns one of them.- Parameters:
-
value
- - the value to be present - Returns:
-
an
OptionalBoolean
with the value present
-
isPresent
public boolean isPresent()
Returnstrue
if there is a value present, otherwisefalse
.- Returns:
-
true
if there is a value present, otherwisefalse
-
getAsBoolean
public boolean getAsBoolean()
If a value is present in thisOptionalBoolean
, returns the value, otherwise throwsNoSuchElementException
.- Returns:
-
the value held by this
OptionalBoolean
- Throws:
-
NoSuchElementException
- if there is no value present - See Also:
-
isPresent()
-
orElse
public boolean orElse(boolean other)
Returns the value if present, otherwise returnsother
.- Parameters:
-
other
- the value to be returned if there is no value present - Returns:
-
the value, if present, otherwise
other
-
orElseGet
public boolean orElseGet(BooleanSupplier other)
Returns the value if present, otherwise invokesother
and returns the result of that invocation.- Parameters:
-
other
- aBooleanSupplier
whose result is returned if no value is present - Returns:
-
the value if present, otherwise the result of
other.getAsBoolean()
- Throws:
-
NullPointerException
- if value is not present andother
isnull
-
orElseThrow
public <X extends Throwable> boolean orElseThrow(Supplier<X> exceptionSupplier) throws X extends Throwable
Returns the contained value, if present, otherwise throws an exception created by the provided supplier. A method reference to the exception constructor with an empty argument list can be used as the supplier. For example,IllegalStateException::new
.- Type Parameters:
-
X
- type of the exception to be thrown - Parameters:
-
exceptionSupplier
- the supplier which will return the exception to be thrown - Returns:
- the present value
- Throws:
-
X
- if there is no value present -
NullPointerException
- if no value is present andexceptionSupplier
isnull
-
X extends Throwable
-
orElseThrow
public boolean orElseThrow()
Returns the contained value if present, otherwise throwsNoSuchElementException
.- Returns:
- the present value
- Throws:
-
NoSuchElementException
- if there is no value present
-
stream
public IntStream stream()
If a value is present, returns the value in a stream, otherwise returns an empty stream. There is noBooleanStream
in JDK. The boolean value is therefore widened toint
and returned in anIntStream
. Value oftrue
is widened to1
,false
to0
.- Returns:
-
the optional value as an
IntStream
-
ifPresent
public void ifPresent(IntConsumer action)
If a value is present, passes the value to the provided consumer, otherwise does nothing. There is noBooleanConsumer
in JDK. The boolean value is therefore widened toint
and passed toIntConsumer
. Value oftrue
is widened to1
,false
to0
.- Parameters:
-
action
- code to be executed if a value is present - Throws:
-
NullPointerException
- if value is present andaction
is null
-
ifPresentOrElse
public void ifPresentOrElse(IntConsumer action, Runnable emptyAction)
If a value is present, passes the value to the provided consumer, otherwise executes the provided action. There is noBooleanConsumer
in JDK. The boolean value is therefore widened toint
and passed toIntConsumer
. Value oftrue
is widened to1
,false
to0
.- Parameters:
-
action
- code to be executed if a value is present -
emptyAction
- code to be executed if no value is present - Throws:
-
NullPointerException
- if value is present andaction
is null
-
toInt
public OptionalInt toInt()
Widens the optional boolean value toOptionalInt
. JDK consistently usesOptionalInt
whereOptionalBoolean
would be semantically correct. This class can consequently cause compatibility problems with other code that expectsOptionalInt
. This method widens the boolean value toint
as wrapped inOptionalInt
to improve compatibility. Value oftrue
is widened to1
,false
to0
.- Returns:
-
the optional value converted to
OptionalInt
-
equals
public boolean equals(Object obj)
Indicates whether some other object is "equal to" thisOptionalBoolean
. The other object is considered equal if it is also anOptionalBoolean
and both instances have no value present or the present values are the same.- Overrides:
-
equals
in classObject
- Parameters:
-
obj
- an object to be tested for equality - Returns:
-
true
if the other object is "equal to" this object, otherwisefalse
- See Also:
-
hashCode()
-
hashCode
public int hashCode()
Returns the hash code value of the present value, if any, or zero if no value is present.- Overrides:
-
hashCode
in classObject
- Returns:
- hash code value of the present value of 0 if no value is present
- See Also:
-
equals(Object)
,Boolean.hashCode()
-
-