Class Maybe<T>

  • Type Parameters:
    T - The type that this object might contain

    public final class Maybe<T>
    extends Object
    A functional “monad” that contains either a non-null value of the given type or an exception, but never both.

    The idea here is:

    objectList
     .stream()
     .map(obj -> Maybe.wrap(String.class, some::functionThatCanFail))
     .filter(Maybe.fnHasValue())
     .forEach(items);
    
    • Method Detail

      • fnHasError

        public static <F> Predicate<Maybe<F>> fnHasError()
        Creates a Predicate that returns true if the Maybe object has an error
        Type Parameters:
        F - The arbitrary input type
        Returns:
        The predicate
      • fnHasValue

        public static <F> Predicate<Maybe<F>> fnHasValue()
        Creates a Predicate that returns true if the Maybe object has a value
        Type Parameters:
        F - The arbitrary input type
        Returns:
        The predicate
      • of

        public static <A> Maybe<A> of​(A value)
        Returns a Maybe object containing the given value
        Type Parameters:
        A - The type of the value
        Parameters:
        value - The value
        Returns:
        A Maybe object containing the given value
      • of

        public static <A> Maybe<A> of​(Class<A> aClass,
                                      Functions.SupplierWithError<A> supplier)
        Returns a Maybe that is either a value or an error, depending on the outcome of the supplier in question.
        Type Parameters:
        A - The content type of the Maybe, if it has a value
        Parameters:
        aClass - The class expected, used solely to distinguish this method from the other of() implementations
        supplier - The supplier of a value to wrap in a Maybe
        Returns:
        A Maybe repesenting the outcome of the Supplier’s action
      • of

        public static <A> Maybe<A> of​(Throwable value,
                                      Class<A> otherwiseExpectedType)
        Returns a Maybe object containing an exception.

        The class parameter is only used for casting the output type.

        Type Parameters:
        A - The parameterized type that we would be expecting if this was not an exception
        Parameters:
        value - The exception
        otherwiseExpectedType - The parameterized type that we would be expecting if this was not an exception
        Returns:
        a Maybe object containing the exception
      • of

        public static <A> Maybe<A> of​(Maybe<?> value,
                                      Class<A> otherwiseExpectedType)
        Chains a Maybe object by passing along the Throwable from an existing Maybe into the next.

        This is for use with streams.

        Type Parameters:
        A - The parameterized type that we would be expecting if this was not an exception
        Parameters:
        value - An existing Maybe in error state
        otherwiseExpectedType - The parameterized type that we would be expecting if this was not an exception
        Returns:
        a Maybe object containing the exception
      • of

        public static <A> Maybe<A> of​(Maybe<?> value)
        Chains a Maybe object by passing along the Throwable from an existing Maybe into the next.

        This is for use with streams.

        Type Parameters:
        A - The parameterized type that we would be expecting if this was not an exception
        Parameters:
        value - An existing Maybe in error state
        Returns:
        a Maybe object containing the exception
      • of

        public static <R> Maybe<R> of​(Throwable e)
        Creates a new maybe from the given throwable with an inferred type by context
        Type Parameters:
        R - The inferred type
        Parameters:
        e - The throwable
        Returns:
        A chained Maybe
      • wrap

        public static <I,​O> Function<I,​Maybe<O>> wrap​(Class<O> aClass,
                                                                  Functions.FunctionWithError<I,​O> func)
        Returns a function wrapping the input function.

        The wrapper will resolve the output of the input function by invoking com.identityworksllc.iiq.common.Functions.FunctionWithError#applyWithError(I) and wrapping the output (whether a value or an exception) in a Maybe.

        Type Parameters:
        I - The input type to the function
        O - The content type of the Maybe, assuming it was to have a value
        Parameters:
        aClass - The output class expected
        func - The function that will be wrapped in a Maybe producer
        Returns:
        A Maybe repesenting the outcome of the Supplier’s action
      • hasError

        public boolean hasError()
        If this Maybe has an error and not a value
        Returns:
        True if this Maybe has an error
      • hasValue

        public boolean hasValue()
        If this Maybe has a value and not an error
        Returns:
        True if this Maybe does not have an error
      • map

        public <B> Maybe<B> map​(Functions.FunctionWithError<T,​B> downstream)
        Chains a Maybe object by invoking the given function on it.

        There are three possible outcomes:

        1. This object already has an error (hasError() returns true), in which case this method will return a new Maybe with that error.

        2. Applying the function to this Maybe’s value results in an exception, in which case this method will return a new Maybe with that exception.

        3. Applying the function is successful and produces an object of type ‘B’, in which case this method returns a new Maybe containing that object.

        Type Parameters:
        B - The output type
        Parameters:
        downstream - The mapping function to apply to this Maybe
        Returns:
        a Maybe object containing the exception