Class Command

java.lang.Object
org.pipservices4.rpc.commands.Command
All Implemented Interfaces:
org.pipservices4.components.exec.IExecutable, ICommand

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 Summary

    Constructors
    Constructor
    Description
    Command(String name, org.pipservices4.data.validate.Schema schema, org.pipservices4.components.exec.IExecutable function)
    Creates a new command object and assigns it's parameters.
  • Method Summary

    Modifier and Type
    Method
    Description
    execute(org.pipservices4.components.context.IContext context, org.pipservices4.components.exec.Parameters args)
    Executes the command.
    Gets the command name.
    List<org.pipservices4.data.validate.ValidationResult>
    validate(org.pipservices4.components.exec.Parameters args)
    Validates the command Parameters args before execution using the defined schema.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • Command

      public Command(String name, org.pipservices4.data.validate.Schema schema, org.pipservices4.components.exec.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(org.pipservices4.components.context.IContext context, org.pipservices4.components.exec.Parameters args) throws org.pipservices4.commons.errors.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 org.pipservices4.components.exec.IExecutable
      Parameters:
      context - (optional) a context to trace execution through call chain.
      args - the parameters (arguments) to pass to this command for execution.
      Returns:
      execution result.
      Throws:
      org.pipservices4.commons.errors.ApplicationException - when execution fails for whatever reason.
      See Also:
      • Parameters
    • validate

      public List<org.pipservices4.data.validate.ValidationResult> validate(org.pipservices4.components.exec.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:
      • Parameters
      • ValidationResult