public final class OptionalChar
extends java.lang.Object
char
value.
If a value is present, isPresent()
will return true
and getAsChar()
will return the value.
Additional methods that depend on the presence or absence of a contained value are provided, such as orElse(char)
(return a default value if value not present) and ifPresent(CharConsumer)
(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 OptionalChar
may have unpredictable results
and should be avoided.
Modifier and Type | Method and Description |
---|---|
static OptionalChar |
empty()
Returns an empty
OptionalChar instance. |
boolean |
equals(java.lang.Object object)
Indicates whether some other
Object is "equal to" this OptionalChar . |
char |
getAsChar()
If a value is present in this
OptionalChar , 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(CharConsumer 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 OptionalChar |
of(char value)
Returns an
OptionalChar with the specified value present. |
char |
orElse(char other)
Returns the value if present, otherwise returns
other . |
char |
orElseGet(CharSupplier 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 OptionalChar
.
The other Object
is considered equal if:
OptionalChar
and;equals
in class java.lang.Object
object
- an Object
to be tested for equalitytrue
if the other Object
is "equal to" this OptionalChar
otherwise false
public boolean isPresent()
true
if there is a value present, otherwise false
.true
if there is a value present, otherwise false
public char getAsChar()
OptionalChar
, returns the value, otherwise throws NoSuchElementException
.OptionalChar
java.util.NoSuchElementException
- if there is no value presentpublic char orElse(char other)
other
.other
- the value to be returned if there is no value presentother
public char orElseGet(CharSupplier other)
other
and returns the result of that invocation.other
- a CharSupplier
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> char 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(CharConsumer 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 OptionalChar empty()
OptionalChar
instance.
No value is present for this OptionalChar
.
OptionalChar
instancepublic static OptionalChar of(char value)
OptionalChar
with the specified value present.value
- the value to be presentOptionalChar
with the specified value present