Class ActionArgs

java.lang.Object
com.scivicslab.pojoactor.core.ActionArgs

public final class ActionArgs extends Object
Utility class for parsing arguments in 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 formatPassed 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 Classes
    Modifier and Type
    Class
    Description
    static class 
    Unified accessor for action arguments.
  • Method Summary

    Modifier and Type
    Method
    Description
    static org.json.JSONArray
    Parses the arguments as a JSONArray.
    static org.json.JSONObject
    Parses the arguments as a JSONObject.
    static boolean
    getBoolean(String args, int index)
    Gets a boolean at the specified index from array arguments.
    static boolean
    getBoolean(String args, int index, boolean defaultValue)
    Gets a boolean at the specified index from array arguments with default.
    static boolean
    getBoolean(String args, String key)
    Gets a boolean for the specified key from object arguments.
    static boolean
    getBoolean(String args, String key, boolean defaultValue)
    Gets a boolean for the specified key from object arguments with default.
    static double
    getDouble(String args, int index)
    Gets a double at the specified index from array arguments.
    static double
    getDouble(String args, int index, double defaultValue)
    Gets a double at the specified index from array arguments with default.
    static double
    getDouble(String args, String key)
    Gets a double for the specified key from object arguments.
    static double
    getDouble(String args, String key, double defaultValue)
    Gets a double for the specified key from object arguments with default.
    static String
    Gets the first string from array arguments.
    static int
    getInt(String args, int index)
    Gets an integer at the specified index from array arguments.
    static int
    getInt(String args, int index, int defaultValue)
    Gets an integer at the specified index from array arguments with default.
    static int
    getInt(String args, String key)
    Gets an integer for the specified key from object arguments.
    static int
    getInt(String args, String key, int defaultValue)
    Gets an integer for the specified key from object arguments with default.
    static long
    getLong(String args, int index)
    Gets a long at the specified index from array arguments.
    static long
    getLong(String args, int index, long defaultValue)
    Gets a long at the specified index from array arguments with default.
    static long
    getLong(String args, String key)
    Gets a long for the specified key from object arguments.
    static long
    getLong(String args, String key, long defaultValue)
    Gets a long for the specified key from object arguments with default.
    static String
    getString(String args, int index)
    Gets a string at the specified index from array arguments.
    static String
    getString(String args, int index, String defaultValue)
    Gets a string at the specified index from array arguments with default.
    static String
    getString(String args, String key)
    Gets a string for the specified key from object arguments.
    static String
    getString(String args, String key, String defaultValue)
    Gets a string for the specified key from object arguments with default.
    static boolean
    hasAtLeast(String args, int required)
    Checks if array arguments have at least the required number of elements.
    static boolean
    hasKey(String args, String key)
    Checks if the specified key exists in object arguments.
    static boolean
    Checks if arguments are in array format.
    static boolean
    Checks if arguments are empty.
    static boolean
    Checks if arguments are not empty.
    static boolean
    Checks if arguments are in object format.
    static int
    length(String args)
    Gets the length of array arguments.
    parse(String args)
    Parses arguments into a unified accessor.

    Methods inherited from class java.lang.Object

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

    • parse

      public static ActionArgs.ParsedArgs parse(String args)
      Parses arguments into a unified accessor.

      This is the recommended way to handle arguments in @Action methods. 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

      public static org.json.JSONArray asArray(String args)
      Parses the arguments as a JSONArray.
      Parameters:
      args - JSON string (array format)
      Returns:
      JSONArray, or empty JSONArray if args is null/empty
    • getString

      public static String getString(String args, int index)
      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

      public static String getString(String args, int index, String defaultValue)
      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

      public static String getFirst(String args)
      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

      public static int getInt(String args, int index)
      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

      public static int getInt(String args, int index, int defaultValue)
      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

      public static long getLong(String args, int index)
      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

      public static long getLong(String args, int index, long defaultValue)
      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

      public static double getDouble(String args, int index)
      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

      public static double getDouble(String args, int index, double defaultValue)
      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

      public static boolean getBoolean(String args, int index)
      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

      public static boolean getBoolean(String args, int index, boolean defaultValue)
      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

      public static int length(String args)
      Gets the length of array arguments.
      Parameters:
      args - JSON string (array format)
      Returns:
      array length
    • asObject

      public static org.json.JSONObject asObject(String args)
      Parses the arguments as a JSONObject.
      Parameters:
      args - JSON string (object format)
      Returns:
      JSONObject, or empty JSONObject if args is null/empty
    • getString

      public static String getString(String args, String key)
      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

      public static String getString(String args, String key, String defaultValue)
      Gets a string for the specified key from object arguments with default.
      Parameters:
      args - JSON string (object format)
      key - key name
      defaultValue - default value if not found
      Returns:
      string value
    • getInt

      public static int getInt(String args, String key)
      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

      public static int getInt(String args, String key, int defaultValue)
      Gets an integer for the specified key from object arguments with default.
      Parameters:
      args - JSON string (object format)
      key - key name
      defaultValue - default value if not found
      Returns:
      integer value
    • getLong

      public static long getLong(String args, String key)
      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

      public static long getLong(String args, String key, long defaultValue)
      Gets a long for the specified key from object arguments with default.
      Parameters:
      args - JSON string (object format)
      key - key name
      defaultValue - default value if not found
      Returns:
      long value
    • getDouble

      public static double getDouble(String args, String key)
      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

      public static double getDouble(String args, String key, double defaultValue)
      Gets a double for the specified key from object arguments with default.
      Parameters:
      args - JSON string (object format)
      key - key name
      defaultValue - default value if not found
      Returns:
      double value
    • getBoolean

      public static boolean getBoolean(String args, String key)
      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

      public static boolean getBoolean(String args, String key, boolean defaultValue)
      Gets a boolean for the specified key from object arguments with default.
      Parameters:
      args - JSON string (object format)
      key - key name
      defaultValue - default value if not found
      Returns:
      boolean value
    • hasKey

      public static boolean hasKey(String args, String key)
      Checks if the specified key exists in object arguments.
      Parameters:
      args - JSON string (object format)
      key - key name
      Returns:
      true if key exists
    • isArray

      public static boolean isArray(String args)
      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

      public static boolean isObject(String args)
      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

      public static boolean hasAtLeast(String args, int required)
      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 required elements
    • isEmpty

      public static boolean isEmpty(String args)
      Checks if arguments are empty.
      Parameters:
      args - JSON string
      Returns:
      true if args is null, empty, "[]", or "{}"
    • isNotEmpty

      public static boolean isNotEmpty(String args)
      Checks if arguments are not empty.
      Parameters:
      args - JSON string
      Returns:
      true if args is not empty