Class Result<Type>

java.lang.Object
de.gustavblass.commons.Result<Type>
Type Parameters:
Type - The class of the operation's result.
All Implemented Interfaces:
Copyable<Result<Type>>

public class Result<Type> extends Object implements Copyable<Result<Type>>
Represents the outcome of an operation that is either successful or unsuccessful and may return an elaborate result. If the operation was unsuccessful, the cause of failure may be specified.
  • Field Details

    • success

      private final boolean success
      Whether the operation was successful. Success does not imply that an elaborate result was returned, and failure does not imply that no elaborate result was returned.
    • result

      @Nullable private final Type result
      The detailed outcome of the operation. Its presence does not imply that the operation was successful, and its absence does not imply that the operation was unsuccessful.
    • cause

      @Nullable private final @Nullable Throwable cause
      The reason why the operation failed, if any. If it is not present, this does not imply that the operation was successful (use getSuccess() for that).
  • Constructor Details

    • Result

      public Result(boolean success)
      Creates a new result object with no elaborate return value. Use Result(boolean, Type) if there is an elaborate return value that should be included.
      Parameters:
      success - True if the operation was successful, false otherwise.
      See Also:
    • Result

      public Result(boolean success, @NonNull Type result)
      Creates a new result object.
      Parameters:
      success - True if the operation was successful, false otherwise.
      result - The elaborate return value of the operation. Must not be null. Use Result(boolean) if there is no elaborate return value.
      See Also:
    • Result

      public Result(boolean success, @Nullable @Nullable Throwable cause)
      Creates a new result object with a cause of failure.
      Parameters:
      success - True if the operation was successful, false otherwise.
      cause - The reason why the operation failed. Will be used as the cause.
    • Result

      public Result(boolean success, @Nullable Type result, @Nullable @Nullable Throwable cause)
      Creates a new result object with a cause of failure.
      Parameters:
      success - True if the operation was successful, false otherwise.
      result - The elaborate return value of the operation.
      cause - The reason why the operation failed.
  • Method Details

    • getSuccess

      @Contract(pure=true) public boolean getSuccess()
      Returns:
      Whether the operation was successful. If true, this does not imply that getResult() will return a non-empty value. If false, this does not imply that getResult() will return an empty value.
    • getResult

      @Contract(pure=true) @NonNull public @NonNull Optional<Type> getResult()
      Returns:
      The elaborate return value of the operation, if any. If it is present, this does not imply that the operation was successful. If it is absent, this does not imply that the operation was unsuccessful.
    • getCause

      @Contract(pure=true) @NonNull public @NonNull Optional<Throwable> getCause()
      Returns:
      The reason why the operation failed, if any. If it is not present, this does not imply that the operation was successful (use getSuccess() for that).
    • copy

      @Contract(pure=true) @NonNull public @NonNull Result<Type> copy()
      Specified by:
      copy in interface Copyable<Type>
      Returns:
      A deep copy of the object, i.e. a clone that has the exact same values as the original object, but is a different instance (new reference).