Class Maybe<T>

Type Parameters:
T - the type of value

public class Maybe<T> extends Object
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). Maybe is primarily intended for use as a method return type where there is a clear need to represent "no result," and where using null is likely to cause errors. A variable whose type is Maybe should never itself be null; it should always point to an Maybe instance.

  • Method Summary

    Modifier and Type
    Method
    Description
    static <T> Maybe<T>
    Returns an empty Maybe instance.
    filter(Predicate<? super T> predicate)
    If a value is present, and the value matches the given predicate, returns an Maybe describing the value, otherwise returns an empty Maybe.
    <R> Maybe<R>
    flatMap(Function<? super T,? extends Maybe<? extends R>> mapper)
    If a value is present, returns the result of applying the given Maybe-bearing mapping function to the value, otherwise returns an empty Maybe.
    void
    ifPresent(Consumer<? super T> action)
    If a value is present, performs the given action with the value, otherwise does nothing.
    <R> Maybe<R>
    map(Function<? super T,? extends R> mapper)
    If a value is present, returns an Maybe describing the result of applying the given mapping function to the value, otherwise returns an empty Maybe.
    static <T> Maybe<T>
    of(T value)
    Returns an Maybe describing the given value, if non-null, otherwise returns an empty Maybe.
    or(Supplier<? extends Maybe<? extends T>> supplier)
    If a value is present, returns an Maybe describing the value, otherwise returns an Maybe produced by the supplying function.
    orElse(T other)
    If a value is present, returns the value, otherwise returns other.
    orElseGet(Supplier<? extends T> supplier)
    If a value is present, returns the value, otherwise returns the result produced by the supplying function.
    Returns a non-empty string representation of this Maybe suitable for debugging.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Method Details

    • of

      public static <T> Maybe<T> of(T value)
      Returns an Maybe describing the given value, if non-null, otherwise returns an empty Maybe.
      Type Parameters:
      T - the type of the value
      Parameters:
      value - the possibly-null value to describe
      Returns:
      an Maybe with a present value if the specified value is non-null, otherwise an empty Maybe
    • empty

      public static <T> Maybe<T> empty()
      Returns an empty Maybe instance. No value is present for this Maybe.
      Type Parameters:
      T - The type of the non-existent value
      Returns:
      an empty Maybe
    • filter

      public Maybe<T> filter(Predicate<? super T> predicate)
      If a value is present, and the value matches the given predicate, returns an Maybe describing the value, otherwise returns an empty Maybe.
      Parameters:
      predicate - the predicate to apply to a value, if present
      Returns:
      an Maybe describing the value of this Maybe, if a value is present and the value matches the given predicate, otherwise an empty Maybe
      Throws:
      NullPointerException - if the predicate is null
    • ifPresent

      public void ifPresent(Consumer<? super T> action)
      If a value is present, performs the given action with the value, otherwise does nothing.
      Parameters:
      action - the action to be performed, if a value is present
      Throws:
      NullPointerException - if value is present and the given action is null
    • map

      public <R> Maybe<R> map(Function<? super T,? extends R> mapper)
      If a value is present, returns an Maybe describing the result of applying the given mapping function to the value, otherwise returns an empty Maybe.

      If the mapping function returns a null result then this method returns an empty Maybe.

      Type Parameters:
      R - The type of the value returned from the mapping function
      Parameters:
      mapper - the mapping function to apply to a value, if present
      Returns:
      an Maybe describing the result of applying a mapping function to the value of this Maybe, if a value is present, otherwise an empty Maybe
      Throws:
      NullPointerException - if the mapping function is null
    • flatMap

      public <R> Maybe<R> flatMap(Function<? super T,? extends Maybe<? extends R>> mapper)
      If a value is present, returns the result of applying the given Maybe-bearing mapping function to the value, otherwise returns an empty Maybe.

      This method is similar to map(Function), but the mapping function is one whose result is already an Maybe, and if invoked, flatMap does not wrap it within an additional Maybe.

      Type Parameters:
      R - The type of value of the Maybe returned by the mapping function
      Parameters:
      mapper - the mapping function to apply to a value, if present
      Returns:
      the result of applying an Maybe-bearing mapping function to the value of this Maybe, if a value is present, otherwise an empty Maybe
      Throws:
      NullPointerException - if the mapping function is null or returns a null result
    • or

      public Maybe<T> or(Supplier<? extends Maybe<? extends T>> supplier)
      If a value is present, returns an Maybe describing the value, otherwise returns an Maybe produced by the supplying function.
      Parameters:
      supplier - the supplying function that produces an Maybe to be returned
      Returns:
      returns an Maybe describing the value of this Maybe, if a value is present, otherwise an Maybe produced by the supplying function.
      Throws:
      NullPointerException - if the supplying function is null or produces a null result
    • orElse

      public T orElse(T other)
      If a value is present, returns the value, otherwise returns other.
      Parameters:
      other - the value to be returned, if no value is present. May be null.
      Returns:
      the value, if present, otherwise other
    • orElseGet

      public T orElseGet(Supplier<? extends T> supplier)
      If a value is present, returns the value, otherwise returns the result produced by the supplying function.
      Parameters:
      supplier - the supplying function that produces a value to be returned
      Returns:
      the value, if present, otherwise the result produced by the supplying function
      Throws:
      NullPointerException - if no value is present and the supplying function is null
    • toString

      public String toString()
      Returns a non-empty string representation of this Maybe suitable for debugging. The exact presentation format is unspecified and may vary between implementations and versions.

      If a value is present the result must include its string representation in the result. Empty and present Maybes must be unambiguously differentiable.

      Overrides:
      toString in class Object
      Returns:
      the string representation of this instance