A container object that wraps possibly undefined values.
| Name |
|---|
T |
- filter
- flatMap
- get
- guard
- ifPresent
- ifPresentOrElse
- isEmpty
- isPresent
- map
- or
- orElse
- orElseGet
- orElseThrow
- orThrow
- empty
- of
- ofNullable
- wrapInArray
• Private new Optional<T>(value?)
| Name |
|---|
T |
| Name | Type |
|---|---|
value? |
null | T |
▸ filter(predicate): Optional<T>
Checks the value in the optional for a certain condition. Will be skipped if the optional is empty.
| Name | Type | Description |
|---|---|---|
predicate |
(value: T) => boolean |
The predicate that checks if the optional value should be passed |
Optional<T>
The optional itself if the predicate is true. Returns an empty optional if the predicate is false or the optional is empty.
▸ flatMap<U>(mapper): Optional<U>
Maps the optional value to another optional. Will be skipped if the optional is empty.
| Name |
|---|
U |
| Name | Type | Description |
|---|---|---|
mapper |
(value: T) => Optional<U> |
The mapper function that transforms the value into another optional |
Optional<U>
An optional that is produced by the mapper function or an empty optional if no value is present.
▸ get(): T
Returns the value that is wrapped in the optional.
throws Error if the optional is empty
T
The wrapped value
▸ guard(predicate, exceptionSupplier): Optional<T>
Checks for the given condition and throws an exception if the condition fails.
throws Error if the condition isn't met.
| Name | Type | Description |
|---|---|---|
predicate |
(value: T) => boolean |
The condition that needs to be fulfilled to go on |
exceptionSupplier |
() => Error |
A supplier that generates an {@link Error} if the condition fails |
Optional<T>
The current optional if the condition is fulfilled
▸ ifPresent(consumer): void
Executes a consumer callback if the optional contains a value.
| Name | Type | Description |
|---|---|---|
consumer |
(value: T) => void |
The callback to execute |
void
▸ ifPresentOrElse(presentAction, emptyAction): void
Executes one of the given callbacks.
| Name | Type | Description |
|---|---|---|
presentAction |
(value: T) => void |
The callback to execute if the optional has a value |
emptyAction |
() => void |
The callback to execute if the optional is empty |
void
▸ isEmpty(): boolean
Defines if the optional is empty. Opposite of isPresent
boolean
▸ isPresent(): boolean
Checks if the optional contains a value. Opposite of isPresent
boolean
▸ map<U>(mapper): Optional<U>
Maps the optional value to something else. Will be skipped if the optional is empty.
| Name |
|---|
U |
| Name | Type | Description |
|---|---|---|
mapper |
(value: T) => undefined | null | U |
The mapper function that transforms the optional value |
Optional<U>
An optional that contains the mapped value or is empty if the map returned a nullish value
▸ or<U>(optionalSupplier): Optional<T | U>
If the current optional is empty then another optional is generated and returned.
| Name |
|---|
U |
| Name | Type | Description |
|---|---|---|
optionalSupplier |
() => Optional<U> |
A supplier callback that generates an alternative optional |
Optional<T | U>
The current optional or the generated optional if the current one is empty
▸ orElse<U>(other): T | U
Returns the value of the optional or the given value if the optional is empty.
| Name |
|---|
U |
| Name | Type | Description |
|---|---|---|
other |
U |
The value to return if the optional is empty |
T | U
The value from the optional or the alternative value
▸ orElseGet<U>(supplier): T | U
Returns the value of the optional or the value of the given supplier if the optional is empty.
| Name |
|---|
U |
| Name | Type | Description |
|---|---|---|
supplier |
() => U |
The supplier that generates an alternative value if the optional is empty |
T | U
The value from the optional or the alternative value
▸ orElseThrow(exceptionSupplier): T
Returns the value of the optional or throws an error if the optional is empty.
| Name | Type | Description |
|---|---|---|
exceptionSupplier |
() => Error |
The supplier that generates an error if the optional is empty |
T
The value from the optional
▸ orThrow(exceptionSupplier): Optional<T>
Throws an exception if the current value of the optional is empty.
throws Error if the optional is empty
| Name | Type | Description |
|---|---|---|
exceptionSupplier |
() => Error |
The supplier that generates an error if the optional is empty |
Optional<T>
The current optional if the value is not empty
▸ wrapInArray(): [T] | []
Returns the value of the optional wrapped in an array. If the optional is empty then the array will be empty too.
| Name |
|---|
T |
[T] | []
An array that is either empty or contains the value
▸ Static empty<T>(): Optional<T>
Creates an empty optional.
| Name |
|---|
T |
Optional<T>
▸ Static of<T>(value): Optional<T>
Creates an Optional from a non-null value.
| Name |
|---|
T |
| Name | Type | Description |
|---|---|---|
value |
T |
The value to wrap |
Optional<T>
the created optional with the given value
▸ Static ofNullable<T>(value): Optional<T>
Creates an Optional from a value that might be null.
| Name |
|---|
T |
| Name | Type | Description |
|---|---|---|
value |
undefined | null | T |
The value to wrap |
Optional<T>
the created optional with the given value