Class Try.Failure<T>

java.lang.Object
io.github.suppierk.java.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 Object implements Try<T>
A container object which contains exception.
  • Constructor Details

    • Failure

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

    • get

      public T get()
      If this is Try.Success, returns the value, if this is Try.Failure, throws the exception stored as unchecked.
      Specified by:
      get in interface Try<T>
      Returns:
      the value held by this Try.Success
      See Also:
    • 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<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
    • ifSuccessOrElse

      public void ifSuccessOrElse(ThrowableConsumer<? super T> valueConsumer, ThrowableConsumer<Throwable> throwableConsumer)
      If a value is present, invoke the specified consumer with the value, otherwise invoke the specified consumer with the exception.
      Specified by:
      ifSuccessOrElse in interface Try<T>
      Parameters:
      valueConsumer - block to be executed if a value is present
      throwableConsumer - 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 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