Interface Maybe<A>

Type Parameters:
A - the contained type
All Superinterfaces:
Effect.Applicant<A>, Iterable<A>
All Known Subinterfaces:
Either.Projection<A,B,L,R>
All Known Implementing Classes:
Either.AbstractProjection, Either.LeftProjection, Either.RightProjection, Option, Option.None, Option.Some

public interface Maybe<A> extends Iterable<A>, Effect.Applicant<A>
Implemented by things that may or may not contain a value.

Note there are some methods suggested by this interface that cannot be expressed here as the return type cannot be expressed in Java's type system (due to the lack of higher-kinded types). These are for instance (where M is the implementing Maybe sub-type):

  • <B> Maybe<B> map(Function<? super A, B>)
  • <B> Maybe<B> flatMap(Function<? super A, Maybe<B>>
  • Maybe<A> filter(Predicate<? super A>)
Since:
1.0
  • Method Details

    • get

      A get()
      Get the value if defined. Throw an exception otherwise.
      Returns:
      the wrapped value
      Throws:
      NoSuchElementException - if this is a none
    • getOrElse

      <B extends A> A getOrElse(B other)
      Get the value if defined, otherwise returns other.
      Type Parameters:
      B - default value type
      Parameters:
      other - value to return if this is empty
      Returns:
      wrapped value if this is defined, otherwise returns other
    • getOr

      A getOr(Supplier<? extends A> supplier)
      Get the value if defined or call the supplier and return its value if not. Replaces getOrElse(Supplier). Get the value if defined or call the supplier and return its value if not.
      Parameters:
      supplier - called if this is empty
      Returns:
      the wrapped value or the value from the Supplier
    • getOrElse

      @Deprecated A getOrElse(Supplier<? extends A> supplier)
      Deprecated.
      since 3.0 getOrElse(Supplier) is being replaced with getOr(Supplier). In Java 8 type inference cannot disambiguate between an overloaded method taking a generic A and the same method taking a Supplier<A>.
      Get the value if defined or call the supplier and return its value if not.
      Parameters:
      supplier - called if this is empty
      Returns:
      the wrapped value or the value from the Supplier
    • getOrNull

      A getOrNull()
      Get the value if defined or null if not.

      Although the use of null is discouraged, code written to use these must often interface with code that expects and returns nulls.

      Returns:
      the value or null if not defined
    • getOrError

      A getOrError(Supplier<String> msg)
      Get the value or throws an error with the supplied message if not defined.

      Used when absolutely sure this is defined.

      Parameters:
      msg - the message for the error.
      Returns:
      the contained value.
    • getOrThrow

      <X extends Throwable> A getOrThrow(Supplier<X> ifUndefined) throws X
      Get the value or throws the supplied throwable if not defined.

      Used when absolutely sure this is defined.

      Type Parameters:
      X - exception type
      Parameters:
      ifUndefined - the supplier of the throwable.
      Returns:
      the contained value.
      Throws:
      X - the throwable the supplier creates if there is no value.
      Since:
      2.0
    • isDefined

      boolean isDefined()
      If the type contains a value return true.
      Returns:
      true if this holds a value, false otherwise.
    • isEmpty

      boolean isEmpty()
      If the type does not contain a value return true.
      Returns:
      true if this does not hold a value, false otherwise.
    • exists

      boolean exists(Predicate<? super A> p)
      Whether this is is defined and applying the predicate to the contained value returns true.
      Parameters:
      p - the predicate to test, must not be null
      Returns:
      true if defined and the predicate returns true for the contained value, false otherwise.
    • iterator

      Iterator<A> iterator()
      Return an iterator for this type. In most cases this takes the form of an iterator returning zero or one values.
      Specified by:
      iterator in interface Iterable<A>
      Returns:
      an iterator over the contained value if defined, or an empty one otherwise.
    • forall

      boolean forall(Predicate<? super A> p)
      Returns true if empty or the result of the application of the given function to the value.
      Parameters:
      p - The predicate function to test on the contained value, must not be null
      Returns:
      true if no value or returns the result of the application of the given function to the value.
    • contains

      boolean contains(A elem)
      Tests whether the option contains a given value as an element.
      Parameters:
      elem - the element to test
      Returns:
      true if the option has an element that is equal to elem, false otherwise
      Since:
      6.1