Class Command

java.lang.Object
org.pipservices3.commons.commands.Command
All Implemented Interfaces:
ICommand, IExecutable

public class Command extends Object implements ICommand
Concrete implementation of ICommand interface. Command allows to call a method or function using Command pattern.

### Example ###

 
 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:
  • Constructor Details

    • Command

      public Command(String name, Schema schema, IExecutable function)
      Creates a new command object and assigns it's parameters.
      Parameters:
      name - the name of the command
      schema - a validation schema for command arguments
      function - an execution function to be wrapped into this command.
  • Method Details

    • getName

      public String getName()
      Gets the command name.
      Specified by:
      getName in interface ICommand
      Returns:
      the command name
    • execute

      public Object execute(String correlationId, Parameters args) throws ApplicationException
      Executes the command. Before execution is validates Parameters args using the defined schema. The command execution intercepts ApplicationException raised by the called function and throws them.
      Specified by:
      execute in interface IExecutable
      Parameters:
      correlationId - optional transaction id to trace calls across components.
      args - the parameters (arguments) to pass to this command for execution.
      Returns:
      execution result.
      Throws:
      ApplicationException - when execution fails for whatever reason.
      See Also:
    • validate

      public List<ValidationResult> validate(Parameters args)
      Validates the command Parameters args before execution using the defined schema.
      Specified by:
      validate in interface ICommand
      Parameters:
      args - the parameters (arguments) to validate using this command's schema.
      Returns:
      a list ValidationResults or an empty list (if no schema is set).
      See Also: