Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Optional<T>

A container object which may or may not contain a non-null value. If a value is present, isPresent will return true and get will return the value.

Additional methods that depend on the presence or absence of a contained value are provided, such as orElse (return a default value if value not present) and ifPresent (execute a block of code if the value is present).

This is a value-based class.

Type parameters

  • T

Hierarchy

  • Optional

Index

Constructors

Private constructor

  • Constructs an instance with the value present.

    Otherwise, constructs an empty instance.

    Parameters

    • Optional value: T

      to be present

    Returns Optional

Properties

Private value

value: T | null | undefined

If non-null, the value; if null, indicates no value is present

Static Private EMPTY

EMPTY: Optional<any> = new Optional<any>()

Common instance for empty.

Accessors

Private nonNullValue

  • get nonNullValue(): T

Methods

equals

filter

  • If a value is present, and the value matches the given predicate, return an Optional describing the value, otherwise return an empty Optional.

    throws

    Error if the predicate is null

    Parameters

    • predicate: Predicate<T>

      a predicate to apply to the value, if present

    Returns 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

flatMap

  • If a value is present, apply the provided Optional-bearing mapping function to it, return that result, otherwise return an empty Optional. This method is similar to map, but the provided mapper is one whose result is already an Optional, and if invoked, flatMap does not wrap it with an additional Optional.

    throws

    Error if the mapping function is null or returns a null result

    Type parameters

    • U

      The type parameter to the Optional returned by

    Parameters

    • mapper: Function<T, Optional<U>>

      a mapping function to apply to the value, if present the mapping function

    Returns 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

get

  • get(): T
  • If a value is present in this Optional, returns the value, otherwise throws {@linkcode Error}.

    throws

    {@linkcode Error} if there is no value present

    see

    isPresent

    Returns T

    the non-null value held by this Optional

ifPresent

  • ifPresent(consumer: Consumer<T>): void
  • If a value is present, invoke the specified consumer with the value, otherwise do nothing.

    throws

    Error if value is present and consumer is null

    Parameters

    • consumer: Consumer<T>

      block to be executed if a value is present

    Returns void

ifPresentOrElse

  • If a value is present, performs the given action with the value, otherwise performs the given empty-based action.

    throws

    Error if a value is present and the given action is null, or no value is present and the given empty-based action is null.

    Parameters

    • action: Consumer<T>

      the action to be performed, if a value is present

    • emptyAction: Callback

      the empty-based action to be performed, if no value is present

    Returns void

isEmpty

  • isEmpty(): boolean
  • If a value is not present, returns true, otherwise false.

    Returns boolean

    value true if a value is not present, otherwise false

isPresent

  • isPresent(): boolean

map

  • If a value is present, apply the provided mapping function to it, and if the result is non-null, return an Optional describing the result. Otherwise return an empty Optional.
    This method supports post-processing on optional values, without the need to explicitly check for a return status.
    For example, the following code creates Optional around a response, and then checks is the received request successful.

       const responseIsSuccessful = Optional.ofNullable(response)
                                            .map(resp => resp.isSuccessful)
                                            .orElse(false);
    

    Here, Optional.ofNullable(response) returns an Optional<any>, and then map returns an Optional<boolean>.

    throws

    Error if the mapping function is null

    Type parameters

    • U

      The type of the result of the mapping function

    Parameters

    • mapper: Function<T, U>

      a mapping function to apply to the value, if present

    Returns 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

orElse

  • orElse(other: T): T
  • Return the value if present, otherwise return other.

    Parameters

    • other: T

      the value to be returned if there is no value present, may be null

    Returns T

    the value, if present, otherwise other

orElseGet

  • orElseGet<U>(other: Producer<U>): T | U
  • Return the value if present, otherwise invoke other and return the result of that invocation.

    throws

    Error if value is not present and other is null

    Type parameters

    • U

    Parameters

    • other: Producer<U>

      a {@linkcode Supplier} whose result is returned if no value is present

    Returns T | U

    the value if present otherwise the result of other()

orElseThrow

  • orElseThrow(errorSupplier: Producer<Error>): T
  • Return the contained value, if present, otherwise throw an error to be created by the provided supplier.

    throws

    Error if there is no value present

    throws

    Error if no value is present and errorSupplier is null

    Parameters

    • errorSupplier: Producer<Error>

      The supplier which will return the error to be thrown

    Returns T

    the present value

Static empty

  • Returns an empty Optional instance. No value is present for this Optional.
    Though it may be tempting to do so, avoid testing if an object is empty by comparing with == against instances returned by empty. There is no guarantee that it is a singleton. Instead, use isPresent.

    Type parameters

    • T

      Type of the non-existent value

    Returns Optional<T>

    an empty Optional

Static of

  • Returns an Optional with the specified present non-null value.

    throws

    Error if value is null

    Type parameters

    • T

      the class of the value

    Parameters

    • value: T

      the value to be present, which must be non-null

    Returns Optional<T>

    an Optional with the value present

Static ofNullable

  • ofNullable<T>(value: T | null | undefined): Optional<T>
  • Returns an Optional describing the specified value, if non-null, otherwise returns an empty Optional.

    Type parameters

    • T

      the class of the value

    Parameters

    • value: T | null | undefined

      the possibly-null value to describe

    Returns Optional<T>

    an Optional with a present value if the specified value is non-null, otherwise an empty Optional

Generated using TypeDoc