Class Success<T>

  • Type Parameters:
    T - The type of the value represented by this instance
    All Implemented Interfaces:
    java.io.Serializable, java.lang.Iterable<T>, Try<T>

    public final class Success<T>
    extends java.lang.Object
    implements Try<T>, java.io.Serializable
    Represents the successful implementation of Try.
    Acts as a carrier for the result of a successful computation.
    For examples on usage refer to the documentation for Try.
    Since:
    1.0
    See Also:
    Serialized Form
    • Constructor Summary

      Constructors 
      Constructor Description
      Success​(T value)
      Creates a 'successful' instance with the provided value.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      Try<java.lang.Throwable> failed()
      Returns a Failure with an UnsupportedOperationException.
      Try<T> filter​(java.util.function.Predicate<T> predicate)
      Applies the predicate to the value of this instance, if it matches this is returned else a Failure.
      <R> Try<R> flatMap​(ThrowableFunction1<T,​Try<R>> function)
      Applies the value to the function and returns the Try generated by the function.
      T get()
      Returns the value held by this instance.
      T getOrElse​(java.util.function.Supplier<T> supplier)
      Returns the value provided in the constructor.
      boolean isSuccess()
      Always returns true.
      java.util.Iterator<T> iterator()
      Returns an iterator containing the single value of this instance.
      <R> Try<R> map​(ThrowableFunction1<T,​R> function)
      Applies the value to the function and returns the Try representing the mapped value.
      Try<T> orElse​(java.util.function.Supplier<Try<T>> supplier)
      Always returns this.
      Try<T> recover​(ThrowableFunction1<java.lang.Throwable,​T> function)
      Always returns this .
      Try<T> recoverWith​(ThrowableFunction1<java.lang.Throwable,​Try<T>> function)
      Always returns this .
      java.util.stream.Stream<T> stream()
      Returns a stream of size one containing the value of this instance.
      java.lang.String toString()
      Returns a String representation of the instance.
      • Methods inherited from class java.lang.Object

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

        forEach, spliterator
    • Constructor Detail

      • Success

        public Success​(T value)
        Creates a 'successful' instance with the provided value.
        Null values are allowed.
        Parameters:
        value - The value represented by this instance
        Since:
        1.0
    • Method Detail

      • isSuccess

        public boolean isSuccess()
        Always returns true.
        Specified by:
        isSuccess in interface Try<T>
        Returns:
        If the Try is a Success
        Since:
        1.0
      • getOrElse

        public T getOrElse​(java.util.function.Supplier<T> supplier)
        Returns the value provided in the constructor.
        I.e. the provided supplier is never use.
        Specified by:
        getOrElse in interface Try<T>
        Parameters:
        supplier - The supplier to return the value in case of a Failure
        Returns:
        The value from the Try or the supplier
        Since:
        1.0
      • orElse

        public Try<T> orElse​(java.util.function.Supplier<Try<T>> supplier)
        Always returns this.
        I.e. the provided supplier is never use.
        Specified by:
        orElse in interface Try<T>
        Parameters:
        supplier - The supplier to return the value in case of a Failure
        Returns:
        This try or the value from the supplier
        Since:
        1.0
      • get

        public T get()
        Returns the value held by this instance.
        Never throws an exception as this is a success
        Specified by:
        get in interface Try<T>
        Returns:
        The value of the Success
        Since:
        1.0
      • failed

        public Try<java.lang.Throwable> failed()
        Returns a Failure with an UnsupportedOperationException.
        Since this is a success there is no failure to map to a success i.e. this is a illegal operation.
        Specified by:
        failed in interface Try<T>
        Returns:
        The value of the Failure in a Success
        Since:
        1.0
      • map

        public <R> Try<R> map​(ThrowableFunction1<T,​R> function)
        Applies the value to the function and returns the Try representing the mapped value.
        Specified by:
        map in interface Try<T>
        Type Parameters:
        R - The type for the return value from the function
        Parameters:
        function - The function to use
        Returns:
        The Option containing the mapped value
        Since:
        1.0
      • flatMap

        public <R> Try<R> flatMap​(ThrowableFunction1<T,​Try<R>> function)
        Applies the value to the function and returns the Try generated by the function.
        Specified by:
        flatMap in interface Try<T>
        Type Parameters:
        R - The type for the return value from the function
        Parameters:
        function - The function to use
        Returns:
        The Option containing the mapped value
        Since:
        1.2
      • filter

        public Try<T> filter​(java.util.function.Predicate<T> predicate)
        Applies the predicate to the value of this instance, if it matches this is returned else a Failure.
        Specified by:
        filter in interface Try<T>
        Parameters:
        predicate - The filter to apply
        Returns:
        The try matching the filter, either this if matching or a Failure in case no match
        Since:
        1.4
      • recover

        public Try<T> recover​(ThrowableFunction1<java.lang.Throwable,​T> function)
        Always returns this .
        As per definition this is a success and will not need to be recovered.
        Specified by:
        recover in interface Try<T>
        Parameters:
        function - The function to apply in case of a Failure
        Returns:
        The recovered Try
        Since:
        1.4
      • recoverWith

        public Try<T> recoverWith​(ThrowableFunction1<java.lang.Throwable,​Try<T>> function)
        Always returns this .
        As per definition this is a success and will not need to be recovered.
        Specified by:
        recoverWith in interface Try<T>
        Parameters:
        function - The function to apply in case of a Failure
        Returns:
        The recovered Try
        Since:
        1.4
      • toString

        public java.lang.String toString()
        Returns a String representation of the instance.
        Overrides:
        toString in class java.lang.Object
        Since:
        1.0
      • iterator

        public final java.util.Iterator<T> iterator()
        Returns an iterator containing the single value of this instance.
        Specified by:
        iterator in interface java.lang.Iterable<T>
        Returns:
        An iterator containing the single value
        Since:
        1.0
      • stream

        public final java.util.stream.Stream<T> stream()
        Returns a stream of size one containing the value of this instance.
        Returns:
        A stream containing the single value
        Since:
        1.0