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 90b05d7..ea2c8c2 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 @@ -73,6 +73,26 @@ public class fflib_ArrayUtils return objects.get(0); } + /** + * Gets all String values for the given Ids + * + * @param stringsByIds the data map to process + * @param idsToRetrieve The keys for which to retrieve their string values + * + * @return Returns all the values for the provided keys + */ + public static Set getAllValues(Map stringsByIds, Set idsToRetrieve) + { + Set result = new Set(); + for (Id id : idsToRetrieve) + { + if (stringsByIds.containsKey(id) == false) continue; + + result.add(stringsByIds.get(id)); + } + return result; + } + /** * Validates if a list is empty * @@ -185,6 +205,31 @@ public class fflib_ArrayUtils return lastValue(cloneAndSort(input)); } + /** + * Merge the given two maps, without modifying the source. + * + * @param mapA Map> + * @param mapB Map> + * + * @return Returns the merged map + */ + public static Map> mergeMaps(Map> mapA, Map> mapB) + { + Map> result = mapA.clone(); + for (Id idValue : mapB.keySet()) + { + if (result.containsKey(idValue)) + { + result.get(idValue).addAll(mapB.get(idValue)); + } + else + { + result.put(idValue, mapB.get(idValue)); + } + } + return result; + } + /** * Sorts the list with Datetime values and return the oldest value in the list * 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 c240706..cce7ced 100644 --- a/sfdx-source/apex-extensions/tests/classes/utils/fflib_ArrayUtilsTest.cls +++ b/sfdx-source/apex-extensions/tests/classes/utils/fflib_ArrayUtilsTest.cls @@ -238,6 +238,57 @@ private class fflib_ArrayUtilsTest System.assertEquals(8, result, 'Did not return the last value in the list of objects'); } + @IsTest + static void itShouldReturnAllValuesForTheGivenIds() + { + // GIVEN a map with String by Ids + final Id accountIdA = fflib_IDGenerator.generate(Schema.Account.SObjectType); + final Id accountIdB = fflib_IDGenerator.generate(Schema.Account.SObjectType); + final Id accountIdC = fflib_IDGenerator.generate(Schema.Account.SObjectType); + final Map stringsByIds = new Map + { + accountIdA => 'Account A', + accountIdB => 'Account B', + accountIdC => 'Account C' + }; + + // WHEN we retrieve a sub-set of Ids + Set result = fflib_ArrayUtils.getAllValues(stringsByIds, new Set {accountIdA, accountIdB}); + + // THEN we get the values of the provide sub-set Ids + System.assert(result.containsAll(new Set {'Account A', 'Account B'})); + } + + @IsTest + static void itShouldReturnMergedMap() + { + final Id accountIdA = fflib_IDGenerator.generate(Schema.Account.SObjectType); + final Id accountIdB = fflib_IDGenerator.generate(Schema.Account.SObjectType); + final Id accountIdC = fflib_IDGenerator.generate(Schema.Account.SObjectType); + final Set ids = new Set {accountIdA, accountIdB, accountIdC}; + + final Map> stringsByIdsA = new Map> + { + accountIdA => new Set {'A1', 'A2'}, + accountIdB => new Set {'B1'} + }; + final Map> stringsByIdsB = new Map> + { + accountIdA => new Set {'A3'}, + accountIdC => new Set {'C1'} + }; + + Map> result = fflib_ArrayUtils.mergeMaps(stringsByIdsA, stringsByIdsB); + + System.assert(result.keySet().containsAll(ids)); + System.assert(result.get(accountIdA).containsAll(new Set{'A1', 'A2', 'A3'})); + System.assert(result.get(accountIdA).size() == 3); + System.assert(result.get(accountIdB).contains('B1')); + System.assert(result.get(accountIdB).size() == 1); + System.assert(result.get(accountIdC).contains('C1')); + System.assert(result.get(accountIdC).size() == 1); + } + private static List generateDateTimes() { return new List