Interface Maybe<T>

Type Parameters:
T - the type of the value
All Known Implementing Classes:
Maybe.None, Maybe.Some

public sealed interface Maybe<T> permits Maybe.Some<T>, Maybe.None<T>

A value that can be absent: Maybe.Some value or Maybe.None.

jsonlens uses this type, not null and not Optional, for values that can be absent. For example, JsonModel.member(Object, ca.marcusdunn.jsonlens.model.JsonString) gives Maybe.None for a member that is not in the object. Unlike Optional, Maybe is sealed, so a switch with record patterns can examine it:

Maybe<String> present = Maybe.some("value");
Maybe<String> absent = Maybe.none();

String text = switch (absent) {
    case Maybe.Some(String value) -> value;
    case Maybe.None() -> "default";
};
int length = present.map(String::length).orElse(0);                        // 5
Result<String, String> required = absent.toResult(() -> "value is missing"); // Err

A Maybe.Some value is never null.

  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Interface
    Description
    static final record 
    An absent value.
    static final record 
    A present value.
  • Method Summary

    Modifier and Type
    Method
    Description
    default <U> Maybe<U>
    flatMap(Function<? super T, Maybe<U>> mapper)
    Applies a function that can give an absent value to a present value.
    default boolean
    Returns true for an absent value.
    default boolean
    Returns true for a present value.
    default <U> Maybe<U>
    map(Function<? super T, ? extends U> mapper)
    Changes a present value.
    static <T> Maybe<T>
    Returns an absent value.
    default T
    orElse(T fallback)
    Returns the present value, or a fallback value.
    default T
    orElseGet(Supplier<? extends T> fallback)
    Returns the present value, or a value from a supplier.
    static <T> Maybe<T>
    some(T value)
    Makes a present value.
    default <E> Result<T,E>
    toResult(Supplier<? extends E> error)
    Makes a result: a success for a present value, or an error for an absent value.
  • Method Details

    • some

      static <T> Maybe<T> some(T value)
      Makes a present value.
      Type Parameters:
      T - the type of the value
      Parameters:
      value - the value
      Returns:
      a present value
    • none

      static <T> Maybe<T> none()
      Returns an absent value.
      Type Parameters:
      T - the type of the value
      Returns:
      an absent value
    • isSome

      default boolean isSome()
      Returns true for a present value.
      Returns:
      true if this is a Maybe.Some
    • isNone

      default boolean isNone()
      Returns true for an absent value.
      Returns:
      true if this is a Maybe.None
    • map

      default <U> Maybe<U> map(Function<? super T, ? extends U> mapper)
      Changes a present value. An absent value does not change.
      Type Parameters:
      U - the new type of the value
      Parameters:
      mapper - the function for the value
      Returns:
      the changed value, or an absent value
    • flatMap

      default <U> Maybe<U> flatMap(Function<? super T, Maybe<U>> mapper)
      Applies a function that can give an absent value to a present value.
      Type Parameters:
      U - the type of the value of the function
      Parameters:
      mapper - the function for the value
      Returns:
      the result of the function, or an absent value
    • orElse

      default T orElse(T fallback)
      Returns the present value, or a fallback value.
      Parameters:
      fallback - the value for an absent value
      Returns:
      the present value or the fallback value
    • orElseGet

      default T orElseGet(Supplier<? extends T> fallback)
      Returns the present value, or a value from a supplier.
      Parameters:
      fallback - the supplier for an absent value
      Returns:
      the present value or the supplied value
    • toResult

      default <E> Result<T,E> toResult(Supplier<? extends E> error)
      Makes a result: a success for a present value, or an error for an absent value.
      Type Parameters:
      E - the type of the error
      Parameters:
      error - the supplier of the error for an absent value
      Returns:
      the result