Class ExecutionResult<S extends Source>
- Type Parameters:
S- the type of command source that extendsSource, representing the entity that initiated the command execution (e.g., Player, Console)
This class follows a Result/Either pattern, encapsulating either a successful command execution with its associated context and search results, or a failure state with error information. This design allows for safe error handling without throwing exceptions during command processing.
Usage Examples:
// Creating a successful result
ExecutionResult<MySource> success = ExecutionResult.of(context, search);
// Creating a failure result with specific error
ExecutionResult<MySource> failure = ExecutionResult.failure(new IllegalArgumentException("Invalid command"));
// Creating a generic failure result
ExecutionResult<MySource> genericFailure = ExecutionResult.failure();
// Checking and handling results
if (result.hasFailed()) {
Throwable error = result.getError(); // May be null for generic failures
// Handle error case
} else {
ExecutionContext<MySource> context = result.getContext();
CommandPathSearch<MySource> search = result.getSearch();
// Process successful execution
}
Thread Safety: This class is immutable and thread-safe once constructed.
- Since:
- 1.0.0
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic <S extends Source>
ExecutionResult<S>Creates a failed execution result with the provided error information.static <S extends Source>
ExecutionResult<S>Creates a failed execution result without specific error information.@Nullable ThrowablegetError()Returns the error that caused the command execution to fail, if any.Returns the execution context containing command execution data and environment.Returns the command path search result containing parsing and matching information.booleanDetermines whether this execution result represents a failed command execution.static <S extends Source>
ExecutionResult<S>of(ExecutionContext<S> executionContext, CommandPathSearch<S> search, Context<S> context) Creates a successful execution result with the provided context and search results.
-
Method Details
-
of
public static <S extends Source> ExecutionResult<S> of(ExecutionContext<S> executionContext, CommandPathSearch<S> search, Context<S> context) Creates a successful execution result with the provided context and search results.This factory method should be used when a command has been successfully parsed and executed without errors. The resulting
ExecutionResultwill contain the execution context and command path search information.- Type Parameters:
S- the type of command source- Parameters:
context- the execution context containing command execution datasearch- the command path search result containing parsing and matching information- Returns:
- a new
ExecutionResultinstance representing successful execution - Throws:
IllegalArgumentException- if either context or search is null- See Also:
-
failure
public static <S extends Source> ExecutionResult<S> failure(@Nullable @Nullable Throwable error, Context<S> context) Creates a failed execution result with the provided error information.This factory method should be used when a command execution has failed due to parsing errors, validation failures, runtime exceptions, or other error conditions. The resulting
ExecutionResultwill contain only the error information, with context and search fields being null.- Type Parameters:
S- the type of command source- Parameters:
error- the exception or error that caused the execution failure, may be null to represent a generic failure without specific error details- Returns:
- a new
ExecutionResultinstance representing failed execution - See Also:
-
failure
Creates a failed execution result without specific error information.This is a convenience factory method that creates a generic failure result when no specific error details are available or needed. This is equivalent to calling
failure(null)but provides cleaner, more readable code for cases where the failure is expected or the specific error is not relevant.Use Cases:
- Command validation failures where the error message is handled elsewhere
- Permission checks that simply succeed or fail without detailed error info
- Generic failure states where the context provides sufficient information
Example:
// Instead of writing: return ExecutionResult.failure(null); // You can write: return ExecutionResult.failure(); // Usage in conditional logic: if (!hasPermission(source)) { return ExecutionResult.failure(); // Clean, readable failure }- Type Parameters:
S- the type of command source- Returns:
- a new
ExecutionResultinstance representing a generic failed execution - See Also:
-
hasFailed
public boolean hasFailed()Determines whether this execution result represents a failed command execution.An execution is considered failed if either the execution context or command path search is null, which occurs when the result was created using the
failure(Throwable, Context)factory method.Note: A failed result may or may not have associated error information accessible via
getError().- Returns:
trueif the command execution failed,falseif it succeeded- See Also:
-
getError
Returns the error that caused the command execution to fail, if any.This method returns the exception or error information associated with a failed execution. For successful executions (where
hasFailed()returnsfalse), this method will returnnull.Note: Even for failed executions, the error may be null if the failure was created without specific error details.
- Returns:
- the error that caused execution failure, or
nullif no specific error information is available or if the execution succeeded - See Also:
-
getContext
-
getSearch
Returns the command path search result containing parsing and matching information.This method provides access to the
CommandPathSearchinstance that contains information about how the command was parsed, which command path was matched, and any argument parsing results.Important: This method should only be called on successful execution results. For failed executions (where
hasFailed()returnstrue), this method will returnnull.- Returns:
- the command path search result for successful executions,
or
nullfor failed executions - Throws:
IllegalStateException- if called on a failed execution result (optional runtime check, implementation-dependent)- See Also:
-
getExecutionContext
Returns the execution context containing command execution data and environment.This method provides access to the
ExecutionContextinstance that contains the command source, parsed arguments, execution environment, and other contextual information needed for command processing.Important: This method should only be called on successful execution results. For failed executions (where
hasFailed()returnstrue), this method will returnnull.- Returns:
- the execution context for successful executions,
or
nullfor failed executions - Throws:
IllegalStateException- if called on a failed execution result (optional runtime check, implementation-dependent)- See Also:
-