Skip to content

DTO_NameValues

Class · Group: Data Transfer Objects

apex
@JsonAccess(serializable='always' deserializable='always') global inherited sharing class DTO_NameValues extends DTO_JsonBase

Extends: 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

apex
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

MethodDescription
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

apex
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

ParameterTypeDescription
nameStringThe key to add or update.
valueStringThe value to associate with the key.

Example

apex
instance.add('myName', 'value');

allExists

apex
global Boolean allExists(Set<String> names)

Checks to see if all the parameter names provided exist in the list

Parameters

ParameterTypeDescription
namesSetA list of names on which to match (please note this is case sensitive)

Returns Boolean — True if ALL the parameters exist

Example

apex
Boolean result = instance.allExists(new Set<String>{'a', 'b'});
apex
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

ParameterTypeDescription
namesToMatchSetA set of keys to check for existence. Case-sensitive.
isNonBlankStringIf 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

apex
Boolean result = instance.allExists(new Set<String>{'a', 'b'}, true);

exists

apex
global Boolean exists(String nameToMatch)

Returns true if the objects contains a value for the specified name

Parameters

ParameterTypeDescription
nameToMatchStringName against which to find a key, note the compare is case-sensitive

Returns Boolean — Null or the item found

Example

apex
Boolean result = instance.exists('value');
apex
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

ParameterTypeDescription
nameToMatchStringName against which to find a key, note the compare is case-sensitive
isNonBlankBooleanWill 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

apex
Boolean result = instance.exists('value', true);

get

apex
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

ParameterTypeDescription
nameStringThe 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

apex
String result = instance.get('myName');

getObjectType

apex
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

apex
Type result = instance.getObjectType();

isEmpty

apex
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

apex
Boolean result = instance.isEmpty();

serialize

apex
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

apex
String result = instance.serialize();

toParameterString

apex
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

apex
String result = instance.toParameterString();

Constructors

ConstructorDescription
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()

apex
global DTO_NameValues()

Constructor that initializes an empty name-value map to store key-value pairs.

Example

apex
DTO_NameValues instance = new DTO_NameValues();

DTO_NameValues(Map<String, String> nameValues)

apex
global DTO_NameValues(Map<String, String> nameValues)

Constructor that populates name-value pairs from a provided map.

Parameters

ParameterTypeDescription
nameValuesMapA map of key-value pairs where both keys and values are strings.

Example

apex
DTO_NameValues instance = new DTO_NameValues(new Map<String, String>{'key' => 'value'});

DTO_NameValues(String separatedNameValues)

apex
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

ParameterTypeDescription
separatedNameValuesStringA string of name-value pairs separated by a comma, e.g., "paramName=paramValue,paramName2=paramValue2".

Example

apex
DTO_NameValues instance = new DTO_NameValues('value');

DTO_NameValues(String separatedNameValues, String separator)

apex
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

ParameterTypeDescription
separatedNameValuesStringA string of name-value pairs, e.g., "paramName=paramValue|paramName2=paramValue2".
separatorStringThe delimiter to separate each name-value pair. If null, defaults to a comma.

Example

apex
DTO_NameValues instance = new DTO_NameValues('value', ',');

Properties

PropertyDescription
global transient Set<String> namesReturns all the names for registered name-value pairs
global transient Integer sizeReturns the number of name-value pairs
global transient List<String> valuesReturns all the values for registered name-value pairs

names

apex
global transient Set<String> names

Type: Set

Returns all the names for registered name-value pairs

size

apex
global transient Integer size

Type: Integer

Returns the number of name-value pairs

values

apex
global transient List<String> values

Type: List

Returns all the values for registered name-value pairs