diff --git a/sfdx-source/apex-extensions/main/default/classes/utils/fflib_ArrayUtils.cls b/sfdx-source/apex-extensions/main/default/classes/utils/fflib_ArrayUtils.cls index f70a49d..559d658 100644 --- a/sfdx-source/apex-extensions/main/default/classes/utils/fflib_ArrayUtils.cls +++ b/sfdx-source/apex-extensions/main/default/classes/utils/fflib_ArrayUtils.cls @@ -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 replaceValue(Map source, Map replacement) + { + Map result = new Map(); + 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 replaceValue(Map source, Map replacement) + { + Map result = new Map(); + 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. diff --git a/sfdx-source/apex-extensions/tests/classes/utils/fflib_ArrayUtilsTest.cls b/sfdx-source/apex-extensions/tests/classes/utils/fflib_ArrayUtilsTest.cls index 39c7bea..17d1f33 100644 --- a/sfdx-source/apex-extensions/tests/classes/utils/fflib_ArrayUtilsTest.cls +++ b/sfdx-source/apex-extensions/tests/classes/utils/fflib_ArrayUtilsTest.cls @@ -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 source = new Map + { + accountIdA => contactIdA, + accountIdB => contactIdB, + accountIdC => fflib_IDGenerator.generate(Schema.Contact.SObjectType) + }; + Map replacement = new Map + { + contactIdA => 10.0, + contactIdB => 20.0 + }; + + Map 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 source = new Map + { + accountIdA => contactIdA, + accountIdB => contactIdB, + accountIdC => fflib_IDGenerator.generate(Schema.Contact.SObjectType) + }; + Map replacement = new Map + { + contactIdA => 'First', + contactIdB => 'Second' + }; + + Map 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() {