Interface Maybe<A>

  • Type Parameters:
    A - the contained type
    All Superinterfaces:
    Effect.Applicant<A>, java.lang.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 java.lang.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 Summary

      All Methods Instance Methods Abstract Methods Deprecated Methods 
      Modifier and Type Method Description
      boolean contains​(A elem)
      Tests whether the option contains a given value as an element.
      boolean exists​(java.util.function.Predicate<? super A> p)
      Whether this is is defined and applying the predicate to the contained value returns true.
      boolean forall​(java.util.function.Predicate<? super A> p)
      Returns true if empty or the result of the application of the given function to the value.
      A get()
      Get the value if defined.
      A getOr​(java.util.function.Supplier<? extends A> supplier)
      Get the value if defined or call the supplier and return its value if not.
      <B extends A>
      A
      getOrElse​(B other)
      Get the value if defined, otherwise returns other.
      A getOrElse​(java.util.function.Supplier<? extends A> supplier)
      Deprecated.
      since 3.0 getOrElse(Supplier) is being replaced with getOr(Supplier).
      A getOrError​(java.util.function.Supplier<java.lang.String> msg)
      Get the value or throws an error with the supplied message if not defined.
      A getOrNull()
      Get the value if defined or null if not.
      <X extends java.lang.Throwable>
      A
      getOrThrow​(java.util.function.Supplier<X> ifUndefined)
      Get the value or throws the supplied throwable if not defined.
      boolean isDefined()
      If the type contains a value return true.
      boolean isEmpty()
      If the type does not contain a value return true.
      java.util.Iterator<A> iterator()
      Return an iterator for this type.
      • Methods inherited from interface java.lang.Iterable

        forEach, spliterator
    • Method Detail

      • get

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

        <B extends AA 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​(java.util.function.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​(java.util.function.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​(java.util.function.Supplier<java.lang.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 java.lang.Throwable> A getOrThrow​(java.util.function.Supplier<X> ifUndefined)
                                              throws X extends java.lang.Throwable
        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.
        X extends java.lang.Throwable
        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​(java.util.function.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

        java.util.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 java.lang.Iterable<A>
        Returns:
        an iterator over the contained value if defined, or an empty one otherwise.
      • forall

        boolean forall​(java.util.function.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