optional

package
v1.2.116 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 7, 2024 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrorNoValuePresent = errors.New("no value present")
)

Functions

This section is empty.

Types

type EmptyConsumer

type EmptyConsumer interface {
	Run()
}

type Optional

type Optional struct {
	// contains filtered or unexported fields
}

Optional is a container object which may or may not contain a non-{@code null} value. If a value is present, {@code isPresent()} returns {@code true}. If no value is present, the object is considered <i>empty</i> and {@code isPresent()} returns {@code false}.

func Empty

func Empty() Optional

Empty returns an empty {@code Optional} instance. No value is present for this {@code Optional}.

func Of

func Of(value interface{}) Optional

Of Returns an {@code Optional} describing the given value.

func OfNillable

func OfNillable(value interface{}) Optional

OfNillable Returns an {@code Optional} describing the given value, if non-{@code null}, otherwise returns an empty {@code Optional}.

func (Optional) Filter

func (o Optional) Filter(predicate predicate.Predicater) Optional

*

  • If a value is present, and the value matches the given predicate,
  • returns an {@code Optional} describing the value, otherwise returns an
  • empty {@code Optional}. *
  • @param predicate the predicate to apply to a value, if present
  • @return an {@code Optional} describing the value of this
  • {@code Optional}, if a value is present and the value matches the
  • given predicate, otherwise an empty {@code Optional}
  • @throws NullPointerException if the predicate is {@code null}

func (*Optional) FlatMap

func (o *Optional) FlatMap(mapper func(interface{}) interface{}) Optional

*

  • If a value is present, returns the result of applying the given
  • {@code Optional}-bearing mapping function to the value, otherwise returns
  • an empty {@code Optional}. *
  • <p>This method is similar to {@link #map(Function)}, but the mapping
  • function is one whose result is already an {@code Optional}, and if
  • invoked, {@code flatMap} does not wrap it within an additional
  • {@code Optional}. *
  • @param <U> The type of value of the {@code Optional} returned by the
  • mapping function
  • @param mapper the mapping function to apply to a value, if present
  • @return the result of applying an {@code Optional}-bearing mapping
  • function to the value of this {@code Optional}, if a value is
  • present, otherwise an empty {@code Optional}
  • @throws NullPointerException if the mapping function is {@code null} or
  • returns a {@code null} result

func (Optional) Get

func (o Optional) Get() interface{}

Get returns the value if a value is present, otherwise throws {@code ErrorNoValuePresent}.

func (Optional) IfPresent

func (o Optional) IfPresent(action consumer.Consumer)

*

  • If a value is present, performs the given action with the value,
  • otherwise does nothing. *
  • @param action the action to be performed, if a value is present
  • @throws ErrorNilPointer if value is present and the given action is
  • {@code null}

func (Optional) IfPresentOrElse

func (o Optional) IfPresentOrElse(action consumer.Consumer, emptyAction EmptyConsumer)

*

  • If a value is present, performs the given action with the value,
  • otherwise performs the given empty-based action. *
  • @param action the action to be performed, if a value is present
  • @param emptyAction the empty-based action to be performed, if no value is
  • present
  • @throws NullPointerException if a value is present and the given action
  • is {@code null}, or no value is present and the given empty-based
  • action is {@code null}.
  • @since 9

func (Optional) IsPresent

func (o Optional) IsPresent() bool

IsPresent returns {@code true} if a value is present, otherwise {@code false}.

func (Optional) Map

func (o Optional) Map(mapper func(interface{}) interface{}) Optional

*

  • If a value is present, returns an {@code Optional} describing (as if by
  • {@link #ofNullable}) the result of applying the given mapping function to
  • the value, otherwise returns an empty {@code Optional}. *
  • <p>If the mapping function returns a {@code null} result then this method
  • returns an empty {@code Optional}. *
  • @apiNote
  • This method supports post-processing on {@code Optional} values, without
  • the need to explicitly check for a return status. For example, the
  • following code traverses a stream of URIs, selects one that has not
  • yet been processed, and creates a path from that URI, returning
  • an {@code Optional<Path>}: *
  • <pre>{@code
  • Optional<Path> p =
  • uris.stream().filter(uri -> !isProcessedYet(uri))
  • .findFirst()
  • .map(Paths::get);
  • }</pre> *
  • Here, {@code findFirst} returns an {@code Optional<URI>}, and then
  • {@code map} returns an {@code Optional<Path>} for the desired
  • URI if one exists. *
  • @param mapper the mapping function to apply to a value, if present
  • @param <U> The type of the value returned from the mapping function
  • @return an {@code Optional} describing the result of applying a mapping
  • function to the value of this {@code Optional}, if a value is
  • present, otherwise an empty {@code Optional}
  • @throws NullPointerException if the mapping function is {@code null}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL