Class Try.Failure<T>

  • Type Parameters:
    T - the class of the value
    All Implemented Interfaces:
    Try<T>
    Enclosing interface:
    Try<T>

    public static class Try.Failure<T>
    extends java.lang.Object
    implements Try<T>
    A container object which contains exception.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      protected Failure​(java.lang.Throwable exception)
      Constructs an instance with the exception present.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      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.
      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.
      Try<T> orElseTry​(ThrowableSupplier<T> supplier)
      Continue Try composition by providing alternative ways to compute desired value
      java.util.Optional<T> toOptional()
      Convert this Try to Optional without preserving exception information.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
      • Methods inherited from interface io.github.suppierk.java.util.Try

        orElse, orElse
    • Constructor Detail

      • Failure

        protected Failure​(java.lang.Throwable exception)
        Constructs an instance with the exception present.
        Parameters:
        exception - the non-null exception to be present
        Throws:
        java.lang.NullPointerException - if exception is null
    • Method Detail

      • isSuccess

        public boolean isSuccess()
        Specified by:
        isSuccess in interface Try<T>
        Returns:
        true if there is a value present, otherwise false
      • isFailure

        public boolean isFailure()
        Specified by:
        isFailure in interface Try<T>
        Returns:
        true if there is an exception present, otherwise false
      • ifSuccess

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

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

        public 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.
        Specified by:
        filter in interface Try<T>
        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

        public <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.
        Specified by:
        map in interface Try<T>
        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

        public <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 Try.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.
        Specified by:
        flatMap in interface Try<T>
        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

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

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