DTO_NameValues
Class · Group: Data Transfer Objects
@JsonAccess(serializable='always' deserializable='always') global inherited sharing class DTO_NameValues extends DTO_JsonBaseExtends: DTO_JsonBase
Class for managing and transferring key-value pairs, represented as names and values, between classes. Provides functionalities to store, retrieve, and manipulate these name-value pairs.
Example
DTO_NameValues params = new DTO_NameValues();
params.add('recipient', 'user@example.com');
params.add('subject', 'Hello');
String recipient = params.get('recipient');
Boolean hasAll = params.allExists(new Set<String>{'recipient', 'subject'}, true);Methods
| Method | Description |
|---|---|
| global void add(String name, String value) | Adds a new key-value pair to the map, or updates the value if the key already exists. |
| global Boolean allExists(Set<String> names) | Checks to see if all the parameter names provided exist in the list |
| global Boolean allExists(Set<String> namesToMatch, Boolean isNonBlank) | Determines if all keys in the provided set exist in the map, and optionally ensures that all have non-blank values. |
| global Boolean exists(String nameToMatch) | Returns true if the objects contains a value for the specified name |
| global Boolean exists(String nameToMatch, Boolean isNonBlank) | Determines if a key exists in the map and, optionally, if it is non-blank. |
| global String get(String name) | Retrieves the value associated with the specified key in the map. |
| global override Type getObjectType() | Returns the type of this DTO for deserialization purposes, useful for reconstructing the object from JSON. |
| global Boolean isEmpty() | Checks if the DTO contains no key-value pairs. |
| global override String serialize() | Serializes the map to a JSON string. |
| global String toParameterString() | Converts the name-value pairs in the map into a parameter string in the format "name1=value1,name2=value2". |
add
global void add(String name, String value)Adds a new key-value pair to the map, or updates the value if the key already exists.
Parameters
| Parameter | Type | Description |
|---|---|---|
name | String | The key to add or update. |
value | String | The value to associate with the key. |
Example
instance.add('myName', 'value');allExists
global Boolean allExists(Set<String> names)Checks to see if all the parameter names provided exist in the list
Parameters
| Parameter | Type | Description |
|---|---|---|
names | Set | A list of names on which to match (please note this is case sensitive) |
Returns Boolean — True if ALL the parameters exist
Example
Boolean result = instance.allExists(new Set<String>{'a', 'b'});global Boolean allExists(Set<String> namesToMatch, Boolean isNonBlank)Determines if all keys in the provided set exist in the map, and optionally ensures that all have non-blank values.
Parameters
| Parameter | Type | Description |
|---|---|---|
namesToMatch | Set | A set of keys to check for existence. Case-sensitive. |
isNonBlank | String | If true, requires that all specified keys have non-blank values. |
Returns Boolean — true if all keys exist (and have non-blank values if specified).
Example
Boolean result = instance.allExists(new Set<String>{'a', 'b'}, true);exists
global Boolean exists(String nameToMatch)Returns true if the objects contains a value for the specified name
Parameters
| Parameter | Type | Description |
|---|---|---|
nameToMatch | String | Name against which to find a key, note the compare is case-sensitive |
Returns Boolean — Null or the item found
Example
Boolean result = instance.exists('value');global Boolean exists(String nameToMatch, Boolean isNonBlank)Determines if a key exists in the map and, optionally, if it is non-blank. A non-blank value is neither null nor an empty string.
Parameters
| Parameter | Type | Description |
|---|---|---|
nameToMatch | String | Name against which to find a key, note the compare is case-sensitive |
isNonBlank | Boolean | Will return false if item exists and it's blank (either null or empty string) |
Returns Boolean — true if the key exists (and has a non-blank value if specified).
Example
Boolean result = instance.exists('value', true);get
global String get(String name)Retrieves the value associated with the specified key in the map. If the key does not exist, returns null.
Parameters
| Parameter | Type | Description |
|---|---|---|
name | String | The key for which to retrieve the value. Case-sensitive. |
Returns String — The value associated with the specified key, or null if the key is absent.
Example
String result = instance.get('myName');getObjectType
global override Type getObjectType()Returns the type of this DTO for deserialization purposes, useful for reconstructing the object from JSON.
Returns Type — The type class of DTO_NameValues.
Example
Type result = instance.getObjectType();isEmpty
global Boolean isEmpty()Checks if the DTO contains no key-value pairs. Returns true if the object has zero key-value pairs.
Returns Boolean — true if there are no entries, false otherwise.
Example
Boolean result = instance.isEmpty();serialize
global override String serialize()Serializes the map to a JSON string. If the map is empty, returns an empty JSON object {}.
Returns String — A JSON string representation of the map.
Example
String result = instance.serialize();toParameterString
global String toParameterString()Converts the name-value pairs in the map into a parameter string in the format "name1=value1,name2=value2".
Returns String — A string of comma-separated name-value pairs.
Example
String result = instance.toParameterString();Constructors
| Constructor | Description |
|---|---|
| global DTO_NameValues() | Constructor that initializes an empty name-value map to store key-value pairs. |
| global DTO_NameValues(Map<String, String> nameValues) | Constructor that populates name-value pairs from a provided map. |
| global DTO_NameValues(String separatedNameValues) | Constructor that accepts a delimited string of name-value pairs, converting them into key-value entries within the map. |
| global DTO_NameValues(String separatedNameValues, String separator) | Constructor that accepts a delimited string of name-value pairs, with a specified separator, and converts them into key-value entries within the map. |
DTO_NameValues()
global DTO_NameValues()Constructor that initializes an empty name-value map to store key-value pairs.
Example
DTO_NameValues instance = new DTO_NameValues();DTO_NameValues(Map<String, String> nameValues)
global DTO_NameValues(Map<String, String> nameValues)Constructor that populates name-value pairs from a provided map.
Parameters
| Parameter | Type | Description |
|---|---|---|
nameValues | Map | A map of key-value pairs where both keys and values are strings. |
Example
DTO_NameValues instance = new DTO_NameValues(new Map<String, String>{'key' => 'value'});DTO_NameValues(String separatedNameValues)
global DTO_NameValues(String separatedNameValues)Constructor that accepts a delimited string of name-value pairs, converting them into key-value entries within the map.
Parameters
| Parameter | Type | Description |
|---|---|---|
separatedNameValues | String | A string of name-value pairs separated by a comma, e.g., "paramName=paramValue,paramName2=paramValue2". |
Example
DTO_NameValues instance = new DTO_NameValues('value');DTO_NameValues(String separatedNameValues, String separator)
global DTO_NameValues(String separatedNameValues, String separator)Constructor that accepts a delimited string of name-value pairs, with a specified separator, and converts them into key-value entries within the map.
Parameters
| Parameter | Type | Description |
|---|---|---|
separatedNameValues | String | A string of name-value pairs, e.g., "paramName=paramValue|paramName2=paramValue2". |
separator | String | The delimiter to separate each name-value pair. If null, defaults to a comma. |
Example
DTO_NameValues instance = new DTO_NameValues('value', ',');Properties
| Property | Description |
|---|---|
| global transient Set<String> names | Returns all the names for registered name-value pairs |
| global transient Integer size | Returns the number of name-value pairs |
| global transient List<String> values | Returns all the values for registered name-value pairs |
names
global transient Set<String> namesType: Set
Returns all the names for registered name-value pairs
size
global transient Integer sizeType: Integer
Returns the number of name-value pairs
values
global transient List<String> valuesType: List
Returns all the values for registered name-value pairs