Add two method overloads for fflib_ArrayUtils.replaceValues to add co… (#68)

* Add two method overloads for fflib_ArrayUtils.replaceValues to add compatibility with Decimal and String values

* Fix failing test
This commit is contained in:
William Velzeboer
2026-01-16 16:12:41 +00:00
committed by GitHub
parent 414145791d
commit 85d35c9abb
2 changed files with 116 additions and 0 deletions
@@ -340,6 +340,66 @@ public class fflib_ArrayUtils
return result;
}
/**
* Takes two maps and replaces the value of the source with the value of the replacement,
* where the source value matches the replacement key.
* The value will become null if the source value does not exists as key in replacement map
*
* @param source The map to replace its value
* @param replacement The map to use its value for replacement
*
* @return Returns a Map<{Source-key} Id, {Replacement-value} Id>
*/
public static Map<Id, Decimal> replaceValue(Map<Id, Id> source, Map<Id, Decimal> replacement)
{
Map<Id, Decimal> result = new Map<Id, Decimal>();
for (Id keyId : source.keySet())
{
Id sourceValue = source.get(keyId);
if (replacement.containsKey(sourceValue))
{
result.put(keyId, replacement.get(sourceValue));
}
else
{
// There is no replacement value for this keyId
result.put(keyId, null);
}
}
return result;
}
/**
* Takes two maps and replaces the value of the source with the value of the replacement,
* where the source value matches the replacement key.
* The value will become null if the source value does not exists as key in replacement map
*
* @param source The map to replace its value
* @param replacement The map to use its value for replacement
*
* @return Returns a Map<{Source-key} Id, {Replacement-value} Id>
*/
public static Map<Id, String> replaceValue(Map<Id, Id> source, Map<Id, String> replacement)
{
Map<Id, String> result = new Map<Id, String>();
for (Id keyId : source.keySet())
{
Id sourceValue = source.get(keyId);
if (replacement.containsKey(sourceValue))
{
result.put(keyId, replacement.get(sourceValue));
}
else
{
// There is no replacement value for this keyId
result.put(keyId, null);
}
}
return result;
}
/**
* Takes two maps and replaces the value of the source with the values of the replacement,
* where the source value matches the replacement key.
@@ -266,6 +266,62 @@ private class fflib_ArrayUtilsTest
System.Assert.isNull(result.get(accountIdC));
}
@IsTest
static void itShouldReplaceValueInTheMap_decimal()
{
Id accountIdA = fflib_IDGenerator.generate(Schema.Account.SObjectType);
Id accountIdB = fflib_IDGenerator.generate(Schema.Account.SObjectType);
Id accountIdC = fflib_IDGenerator.generate(Schema.Account.SObjectType);
Id contactIdA = fflib_IDGenerator.generate(Schema.Contact.SObjectType);
Id contactIdB = fflib_IDGenerator.generate(Schema.Contact.SObjectType);
Map<Id, Id> source = new Map<Id, Id>
{
accountIdA => contactIdA,
accountIdB => contactIdB,
accountIdC => fflib_IDGenerator.generate(Schema.Contact.SObjectType)
};
Map<Id, Decimal> replacement = new Map<Id, Decimal>
{
contactIdA => 10.0,
contactIdB => 20.0
};
Map<Id, Decimal> result = fflib_ArrayUtils.replaceValue(source, replacement);
System.Assert.areEqual(3, result.size());
System.Assert.areEqual(10.0, result.get(accountIdA));
System.Assert.areEqual(20.0, result.get(accountIdB));
System.Assert.isNull(result.get(accountIdC));
}
@IsTest
static void itShouldReplaceValueInTheMap_string()
{
Id accountIdA = fflib_IDGenerator.generate(Schema.Account.SObjectType);
Id accountIdB = fflib_IDGenerator.generate(Schema.Account.SObjectType);
Id accountIdC = fflib_IDGenerator.generate(Schema.Account.SObjectType);
Id contactIdA = fflib_IDGenerator.generate(Schema.Contact.SObjectType);
Id contactIdB = fflib_IDGenerator.generate(Schema.Contact.SObjectType);
Map<Id, Id> source = new Map<Id, Id>
{
accountIdA => contactIdA,
accountIdB => contactIdB,
accountIdC => fflib_IDGenerator.generate(Schema.Contact.SObjectType)
};
Map<Id, String> replacement = new Map<Id, String>
{
contactIdA => 'First',
contactIdB => 'Second'
};
Map<Id, String> result = fflib_ArrayUtils.replaceValue(source, replacement);
System.Assert.areEqual(3, result.size());
System.Assert.areEqual('First', result.get(accountIdA));
System.Assert.areEqual('Second', result.get(accountIdB));
System.Assert.isNull(result.get(accountIdC));
}
@IsTest
static void itShouldReturnAllValuesForTheGivenIds()
{