Class ActionArgs
Action-annotated methods.
YAML workflow arguments are passed to @Action methods as JSON strings.
This class provides convenient static methods to parse these JSON strings into
Java types without boilerplate code.
Argument Formats
YAML arguments can be written in three formats:
| YAML format | Passed as JSON string |
|---|---|
arguments: "value" | ["value"] |
arguments: ["a", "b"] | ["a", "b"] |
arguments: {key: "value"} | {"key": "value"} |
Single string arguments are wrapped as arrays. This simplifies parsing to two patterns: array format and object format.
Usage Examples
Single argument (most common)
@Action("put")
public ActionResult put(String args) {
String value = ActionArgs.getFirst(args);
this.object.put(value);
return new ActionResult(true, "Put " + value);
}
Multiple arguments
@Action("add")
public ActionResult add(String args) {
int a = ActionArgs.getInt(args, 0);
int b = ActionArgs.getInt(args, 1);
return new ActionResult(true, String.valueOf(a + b));
}
Object arguments
@Action("configure")
public ActionResult configure(String args) {
String host = ActionArgs.getString(args, "hostname");
int port = ActionArgs.getInt(args, "port", 80);
boolean ssl = ActionArgs.getBoolean(args, "ssl", false);
// ...
}
With static import
import static com.scivicslab.pojoactor.core.ActionArgs.*;
@Action("move")
public ActionResult move(String args) {
String direction = getFirst(args);
// ...
}
- Since:
- 2.14.0
- Author:
- devteam@scivicslab.com
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classUnified accessor for action arguments. -
Method Summary
Modifier and TypeMethodDescriptionstatic org.json.JSONArrayParses the arguments as a JSONArray.static org.json.JSONObjectParses the arguments as a JSONObject.static booleangetBoolean(String args, int index) Gets a boolean at the specified index from array arguments.static booleangetBoolean(String args, int index, boolean defaultValue) Gets a boolean at the specified index from array arguments with default.static booleangetBoolean(String args, String key) Gets a boolean for the specified key from object arguments.static booleangetBoolean(String args, String key, boolean defaultValue) Gets a boolean for the specified key from object arguments with default.static doubleGets a double at the specified index from array arguments.static doubleGets a double at the specified index from array arguments with default.static doubleGets a double for the specified key from object arguments.static doubleGets a double for the specified key from object arguments with default.static StringGets the first string from array arguments.static intGets an integer at the specified index from array arguments.static intGets an integer at the specified index from array arguments with default.static intGets an integer for the specified key from object arguments.static intGets an integer for the specified key from object arguments with default.static longGets a long at the specified index from array arguments.static longGets a long at the specified index from array arguments with default.static longGets a long for the specified key from object arguments.static longGets a long for the specified key from object arguments with default.static StringGets a string at the specified index from array arguments.static StringGets a string at the specified index from array arguments with default.static StringGets a string for the specified key from object arguments.static StringGets a string for the specified key from object arguments with default.static booleanhasAtLeast(String args, int required) Checks if array arguments have at least the required number of elements.static booleanChecks if the specified key exists in object arguments.static booleanChecks if arguments are in array format.static booleanChecks if arguments are empty.static booleanisNotEmpty(String args) Checks if arguments are not empty.static booleanChecks if arguments are in object format.static intGets the length of array arguments.static ActionArgs.ParsedArgsParses arguments into a unified accessor.
-
Method Details
-
parse
Parses arguments into a unified accessor.This is the recommended way to handle arguments in
@Actionmethods. It provides a unified interface regardless of whether the YAML used array format or object format.@Action("example") public ActionResult example(String args) { ParsedArgs p = ActionArgs.parse(args); String value = p.get(0); // positional access String host = p.get("host"); // named access int port = p.getInt("port", 80); // with default }- Parameters:
args- JSON string from @Action method parameter- Returns:
- ParsedArgs for unified access
-
asArray
Parses the arguments as a JSONArray.- Parameters:
args- JSON string (array format)- Returns:
- JSONArray, or empty JSONArray if args is null/empty
-
getString
Gets a string at the specified index from array arguments.- Parameters:
args- JSON string (array format)index- index (0-based)- Returns:
- string value, or empty string if not found
-
getString
Gets a string at the specified index from array arguments with default.- Parameters:
args- JSON string (array format)index- index (0-based)defaultValue- default value if not found- Returns:
- string value
-
getFirst
Gets the first string from array arguments.This is a shortcut for the most common case: single argument.
- Parameters:
args- JSON string (array format)- Returns:
- first string, or empty string if not found
-
getInt
Gets an integer at the specified index from array arguments.- Parameters:
args- JSON string (array format)index- index (0-based)- Returns:
- integer value, or 0 if not found or parse fails
-
getInt
Gets an integer at the specified index from array arguments with default.- Parameters:
args- JSON string (array format)index- index (0-based)defaultValue- default value if not found- Returns:
- integer value
-
getLong
Gets a long at the specified index from array arguments.- Parameters:
args- JSON string (array format)index- index (0-based)- Returns:
- long value, or 0 if not found or parse fails
-
getLong
Gets a long at the specified index from array arguments with default.- Parameters:
args- JSON string (array format)index- index (0-based)defaultValue- default value if not found- Returns:
- long value
-
getDouble
Gets a double at the specified index from array arguments.- Parameters:
args- JSON string (array format)index- index (0-based)- Returns:
- double value, or 0.0 if not found or parse fails
-
getDouble
Gets a double at the specified index from array arguments with default.- Parameters:
args- JSON string (array format)index- index (0-based)defaultValue- default value if not found- Returns:
- double value
-
getBoolean
Gets a boolean at the specified index from array arguments.- Parameters:
args- JSON string (array format)index- index (0-based)- Returns:
- boolean value, or false if not found
-
getBoolean
Gets a boolean at the specified index from array arguments with default.- Parameters:
args- JSON string (array format)index- index (0-based)defaultValue- default value if not found- Returns:
- boolean value
-
length
Gets the length of array arguments.- Parameters:
args- JSON string (array format)- Returns:
- array length
-
asObject
Parses the arguments as a JSONObject.- Parameters:
args- JSON string (object format)- Returns:
- JSONObject, or empty JSONObject if args is null/empty
-
getString
Gets a string for the specified key from object arguments.- Parameters:
args- JSON string (object format)key- key name- Returns:
- string value, or empty string if not found
-
getString
Gets a string for the specified key from object arguments with default.- Parameters:
args- JSON string (object format)key- key namedefaultValue- default value if not found- Returns:
- string value
-
getInt
Gets an integer for the specified key from object arguments.- Parameters:
args- JSON string (object format)key- key name- Returns:
- integer value, or 0 if not found
-
getInt
Gets an integer for the specified key from object arguments with default.- Parameters:
args- JSON string (object format)key- key namedefaultValue- default value if not found- Returns:
- integer value
-
getLong
Gets a long for the specified key from object arguments.- Parameters:
args- JSON string (object format)key- key name- Returns:
- long value, or 0 if not found
-
getLong
Gets a long for the specified key from object arguments with default.- Parameters:
args- JSON string (object format)key- key namedefaultValue- default value if not found- Returns:
- long value
-
getDouble
Gets a double for the specified key from object arguments.- Parameters:
args- JSON string (object format)key- key name- Returns:
- double value, or 0.0 if not found
-
getDouble
Gets a double for the specified key from object arguments with default.- Parameters:
args- JSON string (object format)key- key namedefaultValue- default value if not found- Returns:
- double value
-
getBoolean
Gets a boolean for the specified key from object arguments.- Parameters:
args- JSON string (object format)key- key name- Returns:
- boolean value, or false if not found
-
getBoolean
Gets a boolean for the specified key from object arguments with default.- Parameters:
args- JSON string (object format)key- key namedefaultValue- default value if not found- Returns:
- boolean value
-
hasKey
Checks if the specified key exists in object arguments.- Parameters:
args- JSON string (object format)key- key name- Returns:
- true if key exists
-
isArray
Checks if arguments are in array format.Returns true if args starts with '['. This includes single string arguments which are wrapped as arrays (e.g., "value" becomes ["value"]).
- Parameters:
args- JSON string- Returns:
- true if args is array format
-
isObject
Checks if arguments are in object format.Returns true if args starts with '{'. This is used for named arguments (e.g., {host: "server1", port: 8080}).
- Parameters:
args- JSON string- Returns:
- true if args is object format
-
hasAtLeast
Checks if array arguments have at least the required number of elements.- Parameters:
args- JSON string (array format)required- required number of arguments- Returns:
- true if array has at least
requiredelements
-
isEmpty
Checks if arguments are empty.- Parameters:
args- JSON string- Returns:
- true if args is null, empty, "[]", or "{}"
-
isNotEmpty
Checks if arguments are not empty.- Parameters:
args- JSON string- Returns:
- true if args is not empty
-