From babc2081f60a3131101488def5158ca742b5f5e7 Mon Sep 17 00:00:00 2001 From: William Velzeboer <6429417+wimvelzeboer@users.noreply.github.com> Date: Mon, 19 Jan 2026 10:11:46 +0000 Subject: [PATCH] Add method overload for fflib_ArrayUtils.mergeMaps (#71) Adds compatibility with Boolean values --- README.md | 5 +- .../classes/utils/fflib_ArrayUtils.cls | 33 +++++++++ .../classes/utils/fflib_ArrayUtilsTest.cls | 73 +++++++++++++------ 3 files changed, 88 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index 1db83d6..584d00d 100644 --- a/README.md +++ b/README.md @@ -50,8 +50,9 @@ You can also have a look at the open items in the [project(s)](./projects), feel Some of the changes that have major impact are listed here; ### Updates 2026 -- **fflib_ArrayUtils** - _replaceValue_, Adds two method overloads for merging a source map of idById and replacement map of idByString or idByDecimal +- **fflib_ArrayUtils**
+ _replaceValue_,
Add three method overloads for merging a source map of idById and replacement map of idByString, idByDecimal or idByBoolean.
+ _mergeMaps_,
Add method overload to merge two maps of idByBoolean, - **API Upgrades** Upgraded to API 65.0 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 b6f6040..830d025 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 @@ -230,6 +230,39 @@ public class fflib_ArrayUtils return result; } + /** + * Merge the given two maps, without modifying the source. + * When duplicate values found, only TRUE values will be merged. + * e.g. + * mapA: 1 => true + * mapB: 1 => false + * result: 1 => true + * + * @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)) + { + if (mapB.get(idValue)) + { + result.put(idValue, true); + } + } + 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 4236bc9..8becf6f 100644 --- a/sfdx-source/apex-extensions/tests/classes/utils/fflib_ArrayUtilsTest.cls +++ b/sfdx-source/apex-extensions/tests/classes/utils/fflib_ArrayUtilsTest.cls @@ -147,28 +147,28 @@ private class fflib_ArrayUtilsTest static void testIsEmpty() { List aNull = null; - System.Assert.istrue(fflib_ArrayUtils.isEmpty(aNull)); - System.Assert.istrue(fflib_ArrayUtils.isEmpty(new List {})); - System.Assert.isfalse(fflib_ArrayUtils.isEmpty(new List {'a'})); - System.Assert.isfalse(fflib_ArrayUtils.isEmpty(new List {'a', 'b'})); - System.Assert.istrue(fflib_ArrayUtils.isEmpty(new List {})); - System.Assert.isfalse(fflib_ArrayUtils.isEmpty(new List {1})); - System.Assert.isfalse(fflib_ArrayUtils.isEmpty(new List {1, 2})); + System.Assert.isTrue(fflib_ArrayUtils.isEmpty(aNull)); + System.Assert.isTrue(fflib_ArrayUtils.isEmpty(new List {})); + System.Assert.isFalse(fflib_ArrayUtils.isEmpty(new List {'a'})); + System.Assert.isFalse(fflib_ArrayUtils.isEmpty(new List {'a', 'b'})); + System.Assert.isTrue(fflib_ArrayUtils.isEmpty(new List {})); + System.Assert.isFalse(fflib_ArrayUtils.isEmpty(new List {1})); + System.Assert.isFalse(fflib_ArrayUtils.isEmpty(new List {1, 2})); } @IsTest static void testIsNotEmpty() { List aNull = null; - System.Assert.isfalse(fflib_ArrayUtils.isNotEmpty(aNull)); - System.Assert.isfalse(fflib_ArrayUtils.isNotEmpty(new List {})); - System.Assert.istrue(fflib_ArrayUtils.isNotEmpty(new List {'a'})); - System.Assert.istrue(fflib_ArrayUtils.isNotEmpty(new List {'a', 'b'})); - System.Assert.isfalse(fflib_ArrayUtils.isNotEmpty(new List {})); - System.Assert.istrue(fflib_ArrayUtils.isNotEmpty(new List {1})); - System.Assert.istrue(fflib_ArrayUtils.isNotEmpty(new List {1, 2})); - System.Assert.isfalse(fflib_ArrayUtils.isNotEmpty(new List {})); - System.Assert.istrue(fflib_ArrayUtils.isNotEmpty(new List {new Account(Name = 'test')})); + System.Assert.isFalse(fflib_ArrayUtils.isNotEmpty(aNull)); + System.Assert.isFalse(fflib_ArrayUtils.isNotEmpty(new List {})); + System.Assert.isTrue(fflib_ArrayUtils.isNotEmpty(new List {'a'})); + System.Assert.isTrue(fflib_ArrayUtils.isNotEmpty(new List {'a', 'b'})); + System.Assert.isFalse(fflib_ArrayUtils.isNotEmpty(new List {})); + System.Assert.isTrue(fflib_ArrayUtils.isNotEmpty(new List {1})); + System.Assert.isTrue(fflib_ArrayUtils.isNotEmpty(new List {1, 2})); + System.Assert.isFalse(fflib_ArrayUtils.isNotEmpty(new List {})); + System.Assert.isTrue(fflib_ArrayUtils.isNotEmpty(new List {new Account(Name = 'test')})); } @IsTest @@ -368,7 +368,7 @@ private class fflib_ArrayUtilsTest Set result = fflib_ArrayUtils.getAllValues(stringsByIds, new Set {accountIdA, accountIdB}); // THEN we get the values of the provide sub-set Ids - System.Assert.istrue(result.containsAll(new Set {'Account A', 'Account B'})); + System.Assert.isTrue(result.containsAll(new Set {'Account A', 'Account B'})); } @IsTest @@ -392,15 +392,46 @@ private class fflib_ArrayUtilsTest Map> result = fflib_ArrayUtils.mergeMaps(stringsByIdsA, stringsByIdsB); - System.Assert.istrue(result.keySet().containsAll(ids)); - System.Assert.istrue(result.get(accountIdA).containsAll(new Set{'A1', 'A2', 'A3'})); + System.Assert.isTrue(result.keySet().containsAll(ids)); + System.Assert.isTrue(result.get(accountIdA).containsAll(new Set{'A1', 'A2', 'A3'})); System.Assert.areEqual(3, result.get(accountIdA).size()); - System.Assert.istrue(result.get(accountIdB).contains('B1')); + System.Assert.isTrue(result.get(accountIdB).contains('B1')); System.Assert.areEqual(1, result.get(accountIdB).size()); - System.Assert.istrue(result.get(accountIdC).contains('C1')); + System.Assert.isTrue(result.get(accountIdC).contains('C1')); System.Assert.areEqual(1, result.get(accountIdC).size()); } + @IsTest + static void itShouldReturnMergedMap_Boolean() + { + 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 Id accountIdD = fflib_IDGenerator.generate(Schema.Account.SObjectType); + final Set ids = new Set {accountIdA, accountIdB, accountIdC, accountIdD}; + + final Map booleanByIdA = new Map + { + accountIdA => true, + accountIdB => false, + accountIdD=> false + }; + final Map booleanByIdB = new Map + { + accountIdA => false, + accountIdB => true, + accountIdC => true + }; + + Map result = fflib_ArrayUtils.mergeMaps(booleanByIdA, booleanByIdB); + + System.Assert.isTrue(result.keySet().containsAll(ids)); + System.Assert.isTrue(result.get(accountIdA)); + System.Assert.isTrue(result.get(accountIdB)); + System.Assert.isTrue(result.get(accountIdC)); + System.Assert.isFalse(result.get(accountIdD)); + } + private static List generateDateTimes() { return new List