Class Try<T>

  • Type Parameters:
    T - the contained object.
    Direct Known Subclasses:
    Failure, Success

    public abstract class Try<T>
    extends java.lang.Object
    Utility class for handling checked exceptions in map functions. Check tests for examples
    • Field Detail

      • NULL_ACTION_MESSAGE

        public static final java.lang.String NULL_ACTION_MESSAGE
        See Also:
        Constant Field Values
    • Constructor Detail

      • Try

        public Try()
    • Method Detail

      • of

        public static <T> Try<T> of​(T input)
      • attempt

        public static <S> Try<S> attempt​(java.util.concurrent.Callable<S> action)
        A wrapper for actions that throw checked Exceptions. See: "https://www.oreilly.com/content/handling-checked-exceptions-in-java-streams" Try to perform the action. Any exception will be enclosed in a Failure.
        Type Parameters:
        S - the resulting object
        Parameters:
        action - a Callable action that throws or does not throw a checked Exception
        Returns:
        a new Try instance
      • attempt

        public static <T,​R,​E extends java.lang.Exception> java.util.function.Function<T,​Try<R>> attempt​(FunctionWithException<T,​R,​E> fe)
        A wrapper for functions that throw checked Exceptions. See: "https://www.oreilly.com/content/handling-checked-exceptions-in-java-streams" Try to perform the action. Any exception will be enclosed in a Failure.
        Type Parameters:
        T - the type of the argument of the function.
        R - the type of the result of the function
        E - the type of the thrown Exception
        Parameters:
        fe - a FunctionWithException function that throws or does not throw a checked Exception
        Returns:
        a new Try instance
      • stream

        public abstract java.util.stream.Stream<T> stream()
      • isSuccess

        public abstract boolean isSuccess()
      • get

        public abstract T get()
      • getException

        public abstract java.lang.Exception getException()
      • isFailure

        public final boolean isFailure()
      • forEach

        public abstract <E extends java.lang.Exception> Try<java.lang.Void> forEach​(ConsumerWithException<T,​E> consumer)
      • orElseThrow

        public abstract <E extends java.lang.Exception> T orElseThrow​(java.util.function.Function<Failure<T>,​E> action)
                                                               throws E extends java.lang.Exception
        Throws:
        E extends java.lang.Exception
      • orElseThrow

        public abstract T orElseThrow()
      • orElse

        public abstract <E extends java.lang.Exception> T orElse​(FunctionWithException<Failure<T>,​T,​E> action)
                                                          throws E extends java.lang.Exception
        Throws:
        E extends java.lang.Exception
      • toOptional

        public abstract <E extends java.lang.Exception> java.util.Optional<T> toOptional​(ConsumerWithException<Failure<T>,​E> action)
                                                                                  throws E extends java.lang.Exception
        Throws:
        E extends java.lang.Exception
      • toOptional

        public abstract java.util.Optional<T> toOptional()