Interface Try<T>

  • Type Parameters:
    T - the class of the value
    All Known Implementing Classes:
    Try.Failure, Try.Success

    public interface Try<T>
    A container object that contains either a value or exception.

    If a value is present, isSuccess() will return true and get() will return the value. If exception occurred, isFailure() will return true and get() will return first occurred exception.

    Additional methods that depend on the presence or absence of a contained value are provided, such as orElse() (return a default value if value not present) and ifSuccess(ThrowableConsumer) (execute a block of code if the value is present) or ifFailure(ThrowableConsumer) (execute a block of code if the exception is present).

    This is a value-based class; use of identity-sensitive operations (including reference equality (==), identity hash code, or synchronization) on instances of Try may have unpredictable results and should be avoided.

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Interface Description
      static class  Try.Failure<T>
      A container object which contains exception.
      static class  Try.Success<T>
      A container object which contains value.
    • Method Summary

      All Methods Static Methods Instance Methods Abstract Methods Default Methods 
      Modifier and Type Method Description
      static <T> Try<T> failure​(java.lang.Throwable throwable)
      Returns a Try with the specified exception.
      Try<T> filter​(ThrowablePredicate<? super T> predicate)
      If a value is present, and the value matches the given predicate, return a Try describing the value, otherwise return a failed Try.
      <U> Try<U> flatMap​(ThrowableFunction<? super T,​Try<U>> mapper)
      If a value is present, apply the provided Try-bearing mapping function to it, return that result, otherwise return a failed Try.
      static <T> Try<T> fromOptional​(java.util.Optional<T> optional)
      Returns a Try created from specified Optional.
      T get()
      If this is Try.Success, returns the value, if this is Try.Failure, throws the exception stored as unchecked.
      void ifFailure​(ThrowableConsumer<java.lang.Throwable> consumer)
      If an exception is present, invoke the specified consumer with the value, otherwise do nothing.
      void ifSuccess​(ThrowableConsumer<? super T> consumer)
      If a value is present, invoke the specified consumer with the value, otherwise do nothing.
      boolean isFailure()  
      boolean isSuccess()  
      <U> Try<U> map​(ThrowableFunction<? super T,​? extends U> mapper)
      If a value is present, apply the provided mapping function to it, and if the result is non-null, return a Try describing the result.
      static <T> Try<T> of​(ThrowableSupplier<T> supplier)
      Returns a Try by invoking specified supplier.
      default T orElse​(java.util.function.Supplier<? extends T> other)
      Return the value if present, otherwise invoke other and return the result of that invocation.
      default T orElse​(T other)
      Return the value if present, otherwise return other.
      Try<T> orElseTry​(ThrowableSupplier<T> supplier)
      Continue Try composition by providing alternative ways to compute desired value
      static <T> Try<T> success​(T value)
      Returns a Try with the specified value.
      java.util.Optional<T> toOptional()
      Convert this Try to Optional without preserving exception information.
    • Method Detail

      • of

        static <T> Try<T> of​(ThrowableSupplier<T> supplier)
        Returns a Try by invoking specified supplier.
        Type Parameters:
        T - the class of the value
        Parameters:
        supplier - the supplier to retrieve the value
        Returns:
        a Try.Success with the value retrieved successfully or Try.Failure
      • fromOptional

        static <T> Try<T> fromOptional​(java.util.Optional<T> optional)
        Returns a Try created from specified Optional.
        Type Parameters:
        T - the class of the value
        Parameters:
        optional - the Optional to be present
        Returns:
        a Try.Success with the value if it was present otherwise a Try.Failure with NoSuchElementException
        Throws:
        java.lang.NullPointerException - if optional is null
      • success

        static <T> Try<T> success​(T value)
        Returns a Try with the specified value.
        Type Parameters:
        T - the class of the value
        Parameters:
        value - the value to be present
        Returns:
        a Try.Success with the value
        Throws:
        java.lang.NullPointerException - if value is null
      • failure

        static <T> Try<T> failure​(java.lang.Throwable throwable)
        Returns a Try with the specified exception.
        Type Parameters:
        T - the class of the value
        Parameters:
        throwable - the value to be present, which must be non-null
        Returns:
        a Try.Failure with the exception
        Throws:
        java.lang.NullPointerException - if throwable is null
      • isSuccess

        boolean isSuccess()
        Returns:
        true if there is a value present, otherwise false
      • isFailure

        boolean isFailure()
        Returns:
        true if there is an exception present, otherwise false
      • ifSuccess

        void ifSuccess​(ThrowableConsumer<? super T> consumer)
        If a value is present, invoke the specified consumer with the value, otherwise do nothing.
        Parameters:
        consumer - block to be executed if a value is present
      • ifFailure

        void ifFailure​(ThrowableConsumer<java.lang.Throwable> consumer)
        If an exception is present, invoke the specified consumer with the value, otherwise do nothing.
        Parameters:
        consumer - block to be executed if an exception is present
      • filter

        Try<T> filter​(ThrowablePredicate<? super T> predicate)
        If a value is present, and the value matches the given predicate, return a Try describing the value, otherwise return a failed Try.
        Parameters:
        predicate - a predicate to apply to the value, if present
        Returns:
        a Try.Success describing the value of this Try if a value is present and the value matches the given predicate, otherwise a Try.Failure
      • map

        <U> Try<U> map​(ThrowableFunction<? super T,​? extends U> mapper)
        If a value is present, apply the provided mapping function to it, and if the result is non-null, return a Try describing the result. Otherwise, return a failed Try.
        Type Parameters:
        U - The type of the result of the mapping function
        Parameters:
        mapper - a mapping function to apply to the value, if present
        Returns:
        a Try.Success describing the result of applying a mapping function to the value of this Try, if a value is present, otherwise a Try.Failure
      • flatMap

        <U> Try<U> flatMap​(ThrowableFunction<? super T,​Try<U>> mapper)
        If a value is present, apply the provided Try-bearing mapping function to it, return that result, otherwise return a failed Try. This method is similar to map(ThrowableFunction), but the provided mapper is one whose result is already a Try, and if invoked, flatMap does not wrap it with an additional Try.
        Type Parameters:
        U - The type parameter to the Try returned by
        Parameters:
        mapper - a mapping function to apply to the value, if present the mapping function
        Returns:
        the result of applying a Try-bearing mapping function to the value of this Try, if a value is present, otherwise a Try.Failure
      • toOptional

        java.util.Optional<T> toOptional()
        Convert this Try to Optional without preserving exception information.
        Returns:
        new Optional containing value or Optional.empty() if there was an exception
      • orElseTry

        Try<T> orElseTry​(ThrowableSupplier<T> supplier)
        Continue Try composition by providing alternative ways to compute desired value
        Parameters:
        supplier - to invoke in case of failure.
        Returns:
        new Try instance
      • orElse

        default T orElse​(T other)
        Return the value if present, otherwise return other.
        Parameters:
        other - the value to be returned if there is a failure, may be null
        Returns:
        the value, if present, otherwise other
      • orElse

        default T orElse​(java.util.function.Supplier<? extends T> other)
        Return the value if present, otherwise invoke other and return the result of that invocation.
        Parameters:
        other - a Supplier whose result is returned if no value is present
        Returns:
        the value if present otherwise the result of other.get()
        Throws:
        java.lang.NullPointerException - if value is not present and other is null