Optional
class Optional<T : Any!>
| kotlin.Any | |
| ↳ | java.util.Optional |
A container object which may or may not contain a non-null value. If a value is present, isPresent() returns true. If no value is present, the object is considered empty and isPresent() returns false.
Additional methods that depend on the presence or absence of a contained value are provided, such as orElse() (returns a default value if no value is present) and ifPresent() (performs an action if a value is present).
This is a value-based class; programmers should treat instances that are equal as interchangeable and should not use instances for synchronization, or unpredictable behavior may occur. For example, in a future release, synchronization may fail.
Summary
| Public methods | |
|---|---|
| static Optional<T> |
empty()Returns an empty |
| Boolean |
Indicates whether some other object is "equal to" this |
| Optional<T>! |
If a value is present, and the value matches the given predicate, returns an |
| Optional<U>! |
If a value is present, returns the result of applying the given |
| T |
get()If a value is present, returns the value, otherwise throws |
| Int |
hashCode()Returns the hash code of the value, if present, otherwise |
| Unit |
If a value is present, performs the given action with the value, otherwise does nothing. |
| Unit |
ifPresentOrElse(action: Consumer<in T>!, emptyAction: Runnable!)If a value is present, performs the given action with the value, otherwise performs the given empty-based action. |
| Boolean |
isEmpty()If a value is not present, returns |
| Boolean |
If a value is present, returns |
| Optional<U>! |
If a value is present, returns an |
| static Optional<T> |
of(value: T)Returns an |
| static Optional<T> |
ofNullable(value: T?)Returns an |
| Optional<T>! |
If a value is present, returns an |
| T |
orElse(other: T)If a value is present, returns the value, otherwise returns |
| T |
If a value is present, returns the value, otherwise returns the result produced by the supplying function. |
| T |
If a value is present, returns the value, otherwise throws |
| T |
orElseThrow(exceptionSupplier: Supplier<out X>!)If a value is present, returns the value, otherwise throws an exception produced by the exception supplying function. |
| Stream<T>! |
stream()If a value is present, returns a sequential |
| String |
toString()Returns a non-empty string representation of this |
Public methods
empty
static fun <T : Any!> empty(): Optional<T>
Returns an empty Optional instance. No value is present for this Optional.
| Parameters | |
|---|---|
<T> |
The type of the non-existent value |
| Return | |
|---|---|
Optional<T> |
an empty Optional |
equals
fun equals(other: Any?): Boolean
Indicates whether some other object is "equal to" this Optional. The other object is considered equal if:
- it is also an
Optionaland; - both instances have no value present or;
- the present values are "equal to" each other via
equals().
| Parameters | |
|---|---|
obj |
an object to be tested for equality |
| Return | |
|---|---|
Boolean |
true if the other object is "equal to" this object otherwise false |
filter
fun filter(predicate: Predicate<in T>!): Optional<T>!
If a value is present, and the value matches the given predicate, returns an Optional describing the value, otherwise returns an empty Optional.
| Parameters | |
|---|---|
predicate |
Predicate<in T>!: the predicate to apply to a value, if present |
| Return | |
|---|---|
Optional<T>! |
an Optional describing the value of this Optional, if a value is present and the value matches the given predicate, otherwise an empty Optional |
| Exceptions | |
|---|---|
java.lang.NullPointerException |
if the predicate is null |
flatMap
fun <U : Any!> flatMap(mapper: Function<in T, out Optional<out U>!>!): Optional<U>!
If a value is present, returns the result of applying the given Optional-bearing mapping function to the value, otherwise returns an empty Optional.
This method is similar to map(java.util.function.Function), but the mapping function is one whose result is already an Optional, and if invoked, flatMap does not wrap it within an additional Optional.
| Parameters | |
|---|---|
<U> |
The type of value of the Optional returned by the mapping function |
mapper |
Function<in T, out Optional<out U>!>!: the mapping function to apply to a value, if present |
| Return | |
|---|---|
Optional<U>! |
the result of applying an Optional-bearing mapping function to the value of this Optional, if a value is present, otherwise an empty Optional |
| Exceptions | |
|---|---|
java.lang.NullPointerException |
if the mapping function is null or returns a null result |
get
fun get(): T
If a value is present, returns the value, otherwise throws NoSuchElementException.
| Return | |
|---|---|
T |
the non-null value described by this Optional |
| Exceptions | |
|---|---|
java.util.NoSuchElementException |
if no value is present |
hashCode
fun hashCode(): Int
Returns the hash code of the value, if present, otherwise 0 (zero) if no value is present.
| Return | |
|---|---|
Int |
hash code value of the present value or 0 if no value is present |
ifPresent
fun ifPresent(action: Consumer<in T>): Unit
If a value is present, performs the given action with the value, otherwise does nothing.
| Parameters | |
|---|---|
action |
Consumer<in T>: the action to be performed, if a value is present |
| Exceptions | |
|---|---|
java.lang.NullPointerException |
if value is present and the given action is null |
ifPresentOrElse
fun ifPresentOrElse(
action: Consumer<in T>!,
emptyAction: Runnable!
): Unit
If a value is present, performs the given action with the value, otherwise performs the given empty-based action.
| Parameters | |
|---|---|
action |
Consumer<in T>!: the action to be performed, if a value is present |
emptyAction |
Runnable!: the empty-based action to be performed, if no value is present |
| Exceptions | |
|---|---|
java.lang.NullPointerException |
if a value is present and the given action is null, or no value is present and the given empty-based action is null. |
isEmpty
fun isEmpty(): Boolean
If a value is not present, returns true, otherwise false.
| Return | |
|---|---|
Boolean |
true if a value is not present, otherwise false |
isPresent
fun isPresent(): Boolean
If a value is present, returns true, otherwise false.
| Return | |
|---|---|
Boolean |
true if a value is present, otherwise false |
map
fun <U : Any!> map(mapper: Function<in T, out U>!): Optional<U>!
If a value is present, returns an Optional describing (as if by ofNullable) the result of applying the given mapping function to the value, otherwise returns an empty Optional.
If the mapping function returns a null result then this method returns an empty Optional.
| Parameters | |
|---|---|
mapper |
Function<in T, out U>!: the mapping function to apply to a value, if present |
<U> |
The type of the value returned from the mapping function |
| Return | |
|---|---|
Optional<U>! |
an Optional describing the result of applying a mapping function to the value of this Optional, if a value is present, otherwise an empty Optional |
| Exceptions | |
|---|---|
java.lang.NullPointerException |
if the mapping function is null |
of
static fun <T : Any!> of(value: T): Optional<T>
Returns an Optional describing the given non-null value.
| Parameters | |
|---|---|
value |
T: the value to describe, which must be non-null |
<T> |
the type of the value |
| Return | |
|---|---|
Optional<T> |
an Optional with the value present |
| Exceptions | |
|---|---|
java.lang.NullPointerException |
if value is null |
ofNullable
static fun <T : Any!> ofNullable(value: T?): Optional<T>
Returns an Optional describing the given value, if non-null, otherwise returns an empty Optional.
| Parameters | |
|---|---|
value |
T?: the possibly-null value to describe |
<T> |
the type of the value |
| Return | |
|---|---|
Optional<T> |
an Optional with a present value if the specified value is non-null, otherwise an empty Optional |
or
fun or(supplier: Supplier<out Optional<out T>!>!): Optional<T>!
If a value is present, returns an Optional describing the value, otherwise returns an Optional produced by the supplying function.
| Parameters | |
|---|---|
supplier |
Supplier<out Optional<out T>!>!: the supplying function that produces an Optional to be returned |
| Return | |
|---|---|
Optional<T>! |
returns an Optional describing the value of this Optional, if a value is present, otherwise an Optional produced by the supplying function. |
| Exceptions | |
|---|---|
java.lang.NullPointerException |
if the supplying function is null or produces a null result |
orElse
fun orElse(other: T): T
If a value is present, returns the value, otherwise returns other.
| Parameters | |
|---|---|
other |
T: the value to be returned, if no value is present. May be null. |
| Return | |
|---|---|
T |
the value, if present, otherwise other |
orElseGet
fun orElseGet(supplier: Supplier<out T>!): T
If a value is present, returns the value, otherwise returns the result produced by the supplying function.
| Parameters | |
|---|---|
supplier |
Supplier<out T>!: the supplying function that produces a value to be returned |
| Return | |
|---|---|
T |
the value, if present, otherwise the result produced by the supplying function |
| Exceptions | |
|---|---|
java.lang.NullPointerException |
if no value is present and the supplying function is null |
orElseThrow
fun orElseThrow(): T
If a value is present, returns the value, otherwise throws NoSuchElementException.
| Return | |
|---|---|
T |
the non-null value described by this Optional |
| Exceptions | |
|---|---|
java.util.NoSuchElementException |
if no value is present |
orElseThrow
fun <X : Throwable!> orElseThrow(exceptionSupplier: Supplier<out X>!): T
If a value is present, returns the value, otherwise throws an exception produced by the exception supplying function.
| Parameters | |
|---|---|
<X> |
Type of the exception to be thrown |
exceptionSupplier |
Supplier<out X>!: the supplying function that produces an exception to be thrown |
| Return | |
|---|---|
T |
the value, if present |
| Exceptions | |
|---|---|
X |
if no value is present |
java.lang.NullPointerException |
if no value is present and the exception supplying function is null |
stream
fun stream(): Stream<T>!
If a value is present, returns a sequential Stream containing only that value, otherwise returns an empty Stream.
| Return | |
|---|---|
Stream<T>! |
the optional value as a Stream |
toString
fun toString(): String
Returns a non-empty string representation of this Optional suitable for debugging. The exact presentation format is unspecified and may vary between implementations and versions.
| Return | |
|---|---|
String |
the string representation of this instance |