Class Try.Failure<A>

  • All Implemented Interfaces:
    java.io.Serializable, java.lang.Iterable<A>
    Enclosing class:
    Try<A>

    private static final class Try.Failure<A>
    extends Try<A>
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.lang.Exception e  
      private static long serialVersionUID  
    • Constructor Summary

      Constructors 
      Constructor Description
      Failure​(java.lang.Exception e)  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean equals​(java.lang.Object o)  
      Try<A> filterOrElse​(java.util.function.Predicate<? super A> p, java.util.function.Supplier<java.lang.Exception> orElseSupplier)
      Return a Success if this is a Success and the contained values satisfies the given predicate.
      <B> Try<B> flatMap​(java.util.function.Function<? super A,​Try<B>> f)
      Binds the given function across the success value if it is one.
      <B> B fold​(java.util.function.Function<? super java.lang.Exception,​B> failureF, java.util.function.Function<A,​B> successF)
      Applies the function to the wrapped value, applying failureF it this is a Failure and successF if this is a Success.
      void forEach​(java.util.function.Consumer<? super A> action)
      Perform the given Consumer (side-effect) for the success if success value.
      A getOrElse​(java.util.function.Supplier<A> s)
      Returns the contained value if this is a success otherwise call the supplier and return its value.
      int hashCode()  
      boolean isFailure()
      Returns true if this failure, otherwise false
      boolean isSuccess()
      Returns true if this success, otherwise false
      <B> Try<B> map​(java.util.function.Function<? super A,​? extends B> f)
      Maps the given function to the value from this `Success` or returns this unchanged if a `Failure`.
      Try<A> orElse​(java.util.function.Supplier<? extends Try<? extends A>> orElse)
      If this is a success, return the same success.
      <X extends java.lang.Exception>
      Try<A>
      recover​(java.lang.Class<X> exceptionType, java.util.function.Function<? super X,​A> f)
      Applies the given function `f` if this is a `Failure` with certain exception type otherwise leaves this unchanged.
      Try<A> recover​(java.util.function.Function<? super java.lang.Exception,​A> f)
      Applies the given function `f` if this is a `Failure` otherwise this unchanged if a 'Success'.
      <X extends java.lang.Exception>
      Try<A>
      recoverWith​(java.lang.Class<X> exceptionType, java.util.function.Function<? super X,​Try<A>> f)
      Binds the given function across certain exception type if it is one, otherwise this unchanged.
      Try<A> recoverWith​(java.util.function.Function<? super java.lang.Exception,​Try<A>> f)
      Binds the given function across the failure value if it is one, otherwise this unchanged if a 'Success'.
      Either<java.lang.Exception,​A> toEither()
      Convert this Try to an Either, becoming a left if this is a failure and a right if this is a success.
      Option<A> toOption()
      Convert this Try to an Option.
      java.util.Optional<A> toOptional()
      Create a Optional from this try.
      java.util.stream.Stream<A> toStream()
      Create a Stream from this try.
      java.lang.String toString()  
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
      • Methods inherited from interface java.lang.Iterable

        spliterator
    • Field Detail

      • e

        private final java.lang.Exception e
    • Constructor Detail

      • Failure

        Failure​(java.lang.Exception e)
    • Method Detail

      • map

        public <B> Try<B> map​(java.util.function.Function<? super A,​? extends B> f)
        Description copied from class: Try
        Maps the given function to the value from this `Success` or returns this unchanged if a `Failure`.

        Note that for Try.delayed(Supplier) this is not an evaluating operation.

        Specified by:
        map in class Try<A>
        Type Parameters:
        B - result type
        Parameters:
        f - the function to apply
        Returns:
        `f` applied to the `Success`, otherwise returns this if this is a `Failure`.
      • isFailure

        public boolean isFailure()
        Description copied from class: Try
        Returns true if this failure, otherwise false

        Note that for Try.delayed(Supplier) this is an evaluating operation.

        Specified by:
        isFailure in class Try<A>
        Returns:
        true if this failure, otherwise false
      • isSuccess

        public boolean isSuccess()
        Description copied from class: Try
        Returns true if this success, otherwise false

        Note that for Try.delayed(Supplier) this is an evaluating operation.

        Specified by:
        isSuccess in class Try<A>
        Returns:
        true if this success, otherwise false
      • flatMap

        public <B> Try<B> flatMap​(java.util.function.Function<? super A,​Try<B>> f)
        Description copied from class: Try
        Binds the given function across the success value if it is one.

        Note that for Try.delayed(Supplier) this is not an evaluating operation.

        Specified by:
        flatMap in class Try<A>
        Type Parameters:
        B - result type
        Parameters:
        f - the function to bind.
        Returns:
        A new Try value after binding with the function applied if this is a Success, otherwise returns this if this is a `Failure`.
      • recover

        public Try<A> recover​(java.util.function.Function<? super java.lang.Exception,​A> f)
        Description copied from class: Try
        Applies the given function `f` if this is a `Failure` otherwise this unchanged if a 'Success'. This is like map for the failure.

        Note that for Try.delayed(Supplier) this is not an evaluating operation.

        Specified by:
        recover in class Try<A>
        Parameters:
        f - the function to apply
        Returns:
        `f` applied to the `Failure`, otherwise returns this if this is a `Success`.
      • recover

        public <X extends java.lang.Exception> Try<A> recover​(java.lang.Class<X> exceptionType,
                                                              java.util.function.Function<? super X,​A> f)
        Description copied from class: Try
        Applies the given function `f` if this is a `Failure` with certain exception type otherwise leaves this unchanged. This is like map for exceptions types.

        Note that for Try.delayed(Supplier) this is not an evaluating operation.

        Specified by:
        recover in class Try<A>
        Type Parameters:
        X - exception type
        Parameters:
        exceptionType - exception class
        f - the function to apply
        Returns:
        `f` applied to the `Failure`, otherwise returns this if this is a `Success` or the exception does not match the exception type.
      • recoverWith

        public Try<A> recoverWith​(java.util.function.Function<? super java.lang.Exception,​Try<A>> f)
        Description copied from class: Try
        Binds the given function across the failure value if it is one, otherwise this unchanged if a 'Success'. This is like flatmap for the failure.

        Note that for Try.delayed(Supplier) this is not an evaluating operation.

        Specified by:
        recoverWith in class Try<A>
        Parameters:
        f - the function to bind.
        Returns:
        A new Try value after binding with the function applied if this is a `Failure`, otherwise returns this if this is a `Success`.
      • recoverWith

        public <X extends java.lang.Exception> Try<A> recoverWith​(java.lang.Class<X> exceptionType,
                                                                  java.util.function.Function<? super X,​Try<A>> f)
        Description copied from class: Try
        Binds the given function across certain exception type if it is one, otherwise this unchanged. This is like flatmap for exceptions types.

        Note that for Try.delayed(Supplier) this is not an evaluating operation.

        Specified by:
        recoverWith in class Try<A>
        Type Parameters:
        X - exception type
        Parameters:
        exceptionType - exception class
        f - the function to apply
        Returns:
        A new Try value after binding with the function applied if this is a `Failure`, otherwise returns this if this is a `Success` or the exception does not match the exception type.
      • getOrElse

        public A getOrElse​(java.util.function.Supplier<A> s)
        Description copied from class: Try
        Returns the contained value if this is a success otherwise call the supplier and return its value.

        Note that for Try.delayed(Supplier) this is an evaluating operation.

        Specified by:
        getOrElse in class Try<A>
        Parameters:
        s - called if this is a failure
        Returns:
        the wrapped value or the value from the Supplier
      • orElse

        public Try<A> orElse​(java.util.function.Supplier<? extends Try<? extends A>> orElse)
        Description copied from class: Try
        If this is a success, return the same success. Otherwise, return value supplied by orElse.

        Note that for Try.delayed(Supplier) this is not an evaluating operation.

        Specified by:
        orElse in class Try<A>
        Parameters:
        orElse - try to return if this is failure
        Returns:
        this or orElse
      • filterOrElse

        public Try<A> filterOrElse​(java.util.function.Predicate<? super A> p,
                                   java.util.function.Supplier<java.lang.Exception> orElseSupplier)
        Description copied from class: Try
        Return a Success if this is a Success and the contained values satisfies the given predicate.

        If this is a Success but the predicate is not satisfied, return a Failure with the value provided by the orElseSupplier.

        Return a Failure if this a Failure with the contained value.

        Note that for Try.delayed(Supplier) this is not an evaluating operation.

        Specified by:
        filterOrElse in class Try<A>
        Parameters:
        p - The predicate function to test on the right contained value.
        orElseSupplier - The supplier to execute when is a success, and predicate is unsatisfied
        Returns:
        a new Try that will be either the existing success/failure or a failure with result of orElseSupplier
      • fold

        public <B> B fold​(java.util.function.Function<? super java.lang.Exception,​B> failureF,
                          java.util.function.Function<A,​B> successF)
        Description copied from class: Try
        Applies the function to the wrapped value, applying failureF it this is a Failure and successF if this is a Success.

        Note that for Try.delayed(Supplier) this is an evaluating operation.

        Specified by:
        fold in class Try<A>
        Type Parameters:
        B - the destination type
        Parameters:
        failureF - the function to apply if this is a Failure
        successF - the function to apply if this is a Success
        Returns:
        the result of the applied function
      • toEither

        public Either<java.lang.Exception,​A> toEither()
        Description copied from class: Try
        Convert this Try to an Either, becoming a left if this is a failure and a right if this is a success.

        Note that for Try.delayed(Supplier) this is an evaluating operation.

        Specified by:
        toEither in class Try<A>
        Returns:
        this value wrapped in right if a success, and the exception wrapped in a left if a failure.
      • toOption

        public Option<A> toOption()
        Description copied from class: Try
        Convert this Try to an Option. Returns Some with a value if it is a success, otherwise None.

        Note that for Try.delayed(Supplier) this is an evaluating operation.

        Specified by:
        toOption in class Try<A>
        Returns:
        The success's value in Some if it exists, otherwise None
      • toOptional

        public java.util.Optional<A> toOptional()
        Description copied from class: Try
        Create a Optional from this try.

        Note that for Try.delayed(Supplier) this is an evaluating operation.

        Specified by:
        toOptional in class Try<A>
        Returns:
        Optional.of(Object) with the value if success, Optional.empty() if failure.
      • toStream

        public java.util.stream.Stream<A> toStream()
        Description copied from class: Try
        Create a Stream from this try.

        Note that for Try.delayed(Supplier) this is an evaluating operation.

        Specified by:
        toStream in class Try<A>
        Returns:
        Stream.of(Object) with the value if success, Stream.empty() if failure.
      • forEach

        public void forEach​(java.util.function.Consumer<? super A> action)
        Description copied from class: Try
        Perform the given Consumer (side-effect) for the success if success value.

        Note that for Try.delayed(Supplier) this is an evaluating operation.

        Specified by:
        forEach in interface java.lang.Iterable<A>
        Specified by:
        forEach in class Try<A>
        Parameters:
        action - the Consumer to apply on the success value
      • equals

        public boolean equals​(java.lang.Object o)
        Overrides:
        equals in class java.lang.Object
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.Object
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object