StateUtil
StateUtil
provides utility methods for managing and manipulating state properties in your application.
Integer Operations
inc(statePropertyId: String): Integer
Increment the value of an integer state property by 1.
Parameters:
statePropertyId
(type: String) - The identifier of the state property.
Returns: An Integer representing the updated value of the state property.
// Increment an integer state property
var incrementedValue = StateUtil.inc("counter");
dec(statePropertyId: String): Integer
Decrement the value of an integer state property by 1.
Parameters:
statePropertyId
(type: String) - The identifier of the state property.
Returns: An Integer representing the updated value of the state property.
// Decrement an integer state property
var decrementedValue = StateUtil.dec("counter");
plus(statePropertyId: String, delta: Integer): Integer
Add a specified value (delta) to an integer state property.
Parameters:
statePropertyId
(type: String) - The identifier of the state property.delta
(type: Integer) - The value to add.
Returns: An Integer representing the updated value of the state property.
// Add a value to an integer state property
var delta = 10;
var newValue = StateUtil.plus("counter", delta);
counter(statePropertyId: String): Integer
Retrieve the current value of an integer state property.
Parameters:
statePropertyId
(type: String) - The identifier of the state property.
Returns:
An Integer representing the current value of the state property.
// Get the current value of an integer state property
var currentValue = StateUtil.counter("counter");
Integer and Long Property Access:
getInteger(statePropertyId: String): Integer
Retrieve the value of an integer state property.
Parameters:
statePropertyId
(type: String) - The identifier of the state property.
Returns:
An Integer representing the value of the state property.
// get integer state proeprty
var age = StateUtil.getInteger("age");
setInteger(statePropertyId: String, value: Integer): void
Set the value of an integer state property.
Parameters:
statePropertyId
(type: String) - The identifier of the state property.value
(type: Integer) - The new value to set.
// Set integer state properties
StateUtil.setInteger("age", 30);
setLong(statePropertyId: String, value: Long): void
Set the value of a long state property.
Parameters:
statePropertyId
(type: String) - The identifier of the state property.value
(type: Long) - The new value to set.
// Set long state property
StateUtil.setLong("timestamp", 1635180000000);
getLong(statePropertyId: String): Long
Retrieve the value of a long state property.
Parameters:
statePropertyId
(type: String) - The identifier of the state property.
Returns:
A Long representing the value of the state property.
// get long state property
var timestamp = StateUtil.getLong("timestamp");
String Property Access:
getString(statePropertyId: String): String
Retrieve the value of a string state property.
Parameters:
statePropertyId
(type: String) - The identifier of the state property.
Returns:
A String representing the value of the state property.
setString(statePropertyId: String, value: String): void
Set the value of a string state property.
Parameters:
statePropertyId
(type: String) - The identifier of the state property.value
(type: String) - The new value to set.
// Get and set string state properties
StateUtil.setString("name", "Alice");
var name = StateUtil.getString("name");
JSON Property Access:
setJson(statePropertyId: String, value: JsonNode): void
Set the value of a JSON state property.
Parameters:
statePropertyId
(type: String) - The identifier of the state property.value
(type: JsonNode) - The new JSON value to set.
// Get and set JSON state properties
var jsonData = JsonUtil.toJson({ "key": "value" });
StateUtil.setJson("data", jsonData);
getJson(statePropertyId: String): JsonNode
Retrieve the value of a JSON state property.
Parameters:
statePropertyId
(type: String) - The identifier of the state property.
Returns:
A
JsonNode
representing the JSON value of the state property.
var jsonValue = StateUtil.getJson("data"); // returns key: "value"
Examples
Example: Managing Integer State Property (e.g., Count):
// Initial state: Counter is set to 5
StateUtil.setInteger("counter", 5);
// Increment the counter by 1
var incrementedValue = StateUtil.inc("counter");
// Result: The counter is now 6
// Decrement the counter by 1
var decrementedValue = StateUtil.dec("counter");
// Result: The counter is back to 5
// Add 10 to the counter
var delta = 10;
var newValue = StateUtil.plus("counter", delta);
// Result: The counter is now 15
// Retrieve the current counter value
var currentValue = StateUtil.counter("counter");
// Result: The current value is 15
Example: Managing String State Property (e.g., User Name):
// Set the user's name
StateUtil.setString("userName", "Alice");
// Retrieve the user's name
var name = StateUtil.getString("userName");
// Result: The user's name is "Alice"
Example: Managing JSON State Property (e.g., User Data):
// Define a JSON object
var userData = JsonUtil.toJson({ "name": "Bob", "age": 28 });
// Set the user data in the state
StateUtil.setJson("userData", userData);
// Retrieve the user data from the state
var jsonValue = StateUtil.getJson("userData");
// Result: jsonValue contains the JSON data with name "Bob" and age 28
These examples demonstrate how the StateUtil
methods can be used to manage and manipulate different types of state properties in your application, making it easy to work with integers, strings, and JSON data in a business context.
Was this helpful?