Class Command
java.lang.Object
org.pipservices3.commons.commands.Command
- All Implemented Interfaces:
ICommand,IExecutable
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
ConstructorsConstructorDescriptionCommand(String name, Schema schema, IExecutable function) Creates a new command object and assigns it's parameters. -
Method Summary
Modifier and TypeMethodDescriptionexecute(String correlationId, Parameters args) Executes the command.getName()Gets the command name.validate(Parameters args) Validates the command Parameters args before execution using the defined schema.
-
Constructor Details
-
Command
Creates a new command object and assigns it's parameters.- Parameters:
name- the name of the commandschema- a validation schema for command argumentsfunction- an execution function to be wrapped into this command.
-
-
Method Details
-
getName
Gets the command name. -
execute
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:
executein interfaceIExecutable- 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
Validates the command Parameters args before execution using the defined schema.
-