- java.lang.Object
-
- com.machinezoo.noexception.optional.OptionalBoolean
-
public final class OptionalBoolean extends Object
A container object which may or may not contain abooleanvalue. If value is present,isPresent()will returntrueandgetAsBoolean()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 OptionalBooleanempty()Returns an emptyOptionalBooleaninstance.booleanequals(Object obj)Indicates whether some other object is "equal to" thisOptionalBoolean.booleangetAsBoolean()If a value is present in thisOptionalBoolean, returns the value, otherwise throwsNoSuchElementException.inthashCode()Returns the hash code value of the present value, if any, or zero if no value is present.voidifPresent(IntConsumer action)If a value is present, passes the value to the provided consumer, otherwise does nothing.voidifPresentOrElse(IntConsumer action, Runnable emptyAction)If a value is present, passes the value to the provided consumer, otherwise executes the provided action.booleanisPresent()Returnstrueif there is a value present, otherwisefalse.static OptionalBooleanof(boolean value)Returns anOptionalBooleanwith the specified value present.booleanorElse(boolean other)Returns the value if present, otherwise returnsother.booleanorElseGet(BooleanSupplier other)Returns the value if present, otherwise invokesotherand returns the result of that invocation.booleanorElseThrow()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.IntStreamstream()If a value is present, returns the value in a stream, otherwise returns an empty stream.OptionalInttoInt()Widens the optional boolean value toOptionalInt.StringtoString()Returns string representation of this instance suitable for debugging.
-
-
-
Method Detail
-
empty
public static OptionalBoolean empty()
Returns an emptyOptionalBooleaninstance. 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 anOptionalBooleanwith the specified value present. There are only two global instances ofOptionalBooleanwith value present. This method just returns one of them.- Parameters:
-
value- - the value to be present - Returns:
-
an
OptionalBooleanwith the value present
-
isPresent
public boolean isPresent()
Returnstrueif there is a value present, otherwisefalse.- Returns:
-
trueif 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 invokesotherand returns the result of that invocation.- Parameters:
-
other- aBooleanSupplierwhose 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 andotherisnull
-
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 andexceptionSupplierisnull -
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 noBooleanStreamin JDK. The boolean value is therefore widened tointand returned in anIntStream. Value oftrueis widened to1,falseto0.- 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 noBooleanConsumerin JDK. The boolean value is therefore widened tointand passed toIntConsumer. Value oftrueis widened to1,falseto0.- Parameters:
-
action- code to be executed if a value is present - Throws:
-
NullPointerException- if value is present andactionis 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 noBooleanConsumerin JDK. The boolean value is therefore widened tointand passed toIntConsumer. Value oftrueis widened to1,falseto0.- 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 andactionis null
-
toInt
public OptionalInt toInt()
Widens the optional boolean value toOptionalInt. JDK consistently usesOptionalIntwhereOptionalBooleanwould be semantically correct. This class can consequently cause compatibility problems with other code that expectsOptionalInt. This method widens the boolean value tointas wrapped inOptionalIntto improve compatibility. Value oftrueis widened to1,falseto0.- 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 anOptionalBooleanand both instances have no value present or the present values are the same.- Overrides:
-
equalsin classObject - Parameters:
-
obj- an object to be tested for equality - Returns:
-
trueif 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:
-
hashCodein classObject - Returns:
- hash code value of the present value of 0 if no value is present
- See Also:
-
equals(Object),Boolean.hashCode()
-
-