Class Result<T>


  • public abstract class Result<T>
    extends Object
    Represents the result of an operation. Usually a conversion operation.
    See Also:
    Codec
    • Method Detail

      • success

        public static <T> Result<T> success​(T value)
        Creates a successful result
        Type Parameters:
        T - the type of the result
        Parameters:
        value - result's value
        Returns:
        a successful result
      • fail

        public static <T> Result<T> fail​(String error)
        Creates failure result
        Type Parameters:
        T - the type of the result
        Parameters:
        error - the error message
        Returns:
        a failure result
      • fail

        public static <T> Result<T> fail​(String error,
                                         Throwable cause)
        Creates failure result with an exception
        Type Parameters:
        T - the type of the result
        Parameters:
        error - the error message
        cause - the exception that caused this failure
        Returns:
        a failure result
      • isSuccess

        public abstract boolean isSuccess()
        Returns:
        true if the operation was successful
      • isFailure

        public abstract boolean isFailure()
        Returns:
        true if the operation has failed
      • get

        public abstract T get()
        Extracts the resulting value or throw an exception if the operation has failed.
        Returns:
        the result value
        Throws:
        IllegalStateException - if the operation has failed
      • getOptional

        public abstract Optional<T> getOptional()
        Gets an Optional type containing the result value if the operation was successful.
        Returns:
        an Optional with the result value, if success
      • getOrElse

        public abstract T getOrElse​(Supplier<T> defaultValue)
        Gets the result value or return the a default value if the operation has failed
        Parameters:
        defaultValue - default value to return case this represents a failure
        Returns:
        the result value of the default value
      • orElseGet

        public T orElseGet​(Supplier<T> defaultValue)
        Gets the result value or return the a default value if the operation has failed
        Parameters:
        defaultValue - default value to return case this represents a failure
        Returns:
        the result value of the default value
      • map

        public abstract <U> Result<U> map​(Function<T,​U> fn)
        Apply the function passed on the result value.
        Type Parameters:
        U - the type of the result
        Parameters:
        fn - the map function to be applied
        Returns:
        if this is a successful result, return a new successful result with the map function result. If this is a failure, returns a new failure with the same error message.
      • flatMap

        public abstract <U> Result<U> flatMap​(Function<T,​Result<U>> fn)
        Apply the function passed on the result value.
        Type Parameters:
        U - the type of the result
        Parameters:
        fn - the map function to be applied
        Returns:
        if this is a successful result, returns the map function result. If this is a failure, returns a new failure with the same error message.
      • orNull

        public T orNull()
        Extracts the resulting value or returns null if the operation has failed.
        Returns:
        the result value or null