Command

The Command class allows calling a method or a function.

Implements: ICommand

Description

The Command class allows you to call a method or a function.

Constructors

Creates a new command object and assigns it’s parameters.

public Command(String name, Schema schema, IExecutable function)

  • name: String - command name.
  • schema: Schema - schema to validate command arguments.
  • action: IExecutable - function to be executed by this command.

Instance methods

execute

Executes the command. Before execution it validates args using the defined schema.

Raise ApplicationException when execution fails for whatever reason.
See Parameters

public Object execute(IContext context, Parameters args) throws ApplicationException

  • context: IContext - (optional) a context to trace execution through a call chain.
  • args: Parameters - parameters (arguments) to pass to this command for execution.
  • returns: Object - execution result

getName

Gets the command name.

public String getName()

  • returns: String - name of this command.

validate

Validates the command args before execution using the defined schema.

public List<ValidationResult> validate(Parameters args)

  • args: Parameters - parameters (arguments) to validate using this command’s schema.
  • returns: List<ValidationResult> - array of ValidationResults or an empty array (if no schema is set).

Examples

{
  Command command = new Command("add", null, (args) -> {
      float param1 = args.getAsFloat("param1");
      float param2 = args.getAsFloat("param2");
      return param1 + param2;
  });
 
  Object result = command.execute(
    "123",
    Parameters.fromTuples(
      "param1", 2,
      "param2", 2
    )
  );
 
  System.out.println(result.toString());
 
  // Console output: 4
  }

See also