Add method overload for fflib_ArrayUtils.mergeMaps (#71)

Adds compatibility with Boolean values
This commit is contained in:
William Velzeboer
2026-01-19 10:11:46 +00:00
committed by GitHub
parent 57a9c3f503
commit babc2081f6
3 changed files with 88 additions and 23 deletions
+3 -2
View File
@@ -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; Some of the changes that have major impact are listed here;
### Updates 2026 ### Updates 2026
- **fflib_ArrayUtils** - **fflib_ArrayUtils** <br/>
_replaceValue_, Adds two method overloads for merging a source map of idById and replacement map of idByString or idByDecimal _replaceValue_,<br/>Add three method overloads for merging a source map of idById and replacement map of idByString, idByDecimal or idByBoolean.</br>
_mergeMaps_,<br/>Add method overload to merge two maps of idByBoolean,
- **API Upgrades** - **API Upgrades**
Upgraded to API 65.0 Upgraded to API 65.0
@@ -230,6 +230,39 @@ public class fflib_ArrayUtils
return result; 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<Id, Boolean>
* @param mapB Map<Id, Boolean>
*
* @return Returns the merged map
*/
public static Map<Id, Boolean> mergeMaps(Map<Id, Boolean> mapA, Map<Id, Boolean> mapB)
{
Map<Id, Boolean> 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 * Sorts the list with Datetime values and return the oldest value in the list
* *
@@ -147,28 +147,28 @@ private class fflib_ArrayUtilsTest
static void testIsEmpty() static void testIsEmpty()
{ {
List<String> aNull = null; List<String> aNull = null;
System.Assert.istrue(fflib_ArrayUtils.isEmpty(aNull)); System.Assert.isTrue(fflib_ArrayUtils.isEmpty(aNull));
System.Assert.istrue(fflib_ArrayUtils.isEmpty(new List<String> {})); System.Assert.isTrue(fflib_ArrayUtils.isEmpty(new List<String> {}));
System.Assert.isfalse(fflib_ArrayUtils.isEmpty(new List<String> {'a'})); System.Assert.isFalse(fflib_ArrayUtils.isEmpty(new List<String> {'a'}));
System.Assert.isfalse(fflib_ArrayUtils.isEmpty(new List<String> {'a', 'b'})); System.Assert.isFalse(fflib_ArrayUtils.isEmpty(new List<String> {'a', 'b'}));
System.Assert.istrue(fflib_ArrayUtils.isEmpty(new List<Integer> {})); System.Assert.isTrue(fflib_ArrayUtils.isEmpty(new List<Integer> {}));
System.Assert.isfalse(fflib_ArrayUtils.isEmpty(new List<Integer> {1})); System.Assert.isFalse(fflib_ArrayUtils.isEmpty(new List<Integer> {1}));
System.Assert.isfalse(fflib_ArrayUtils.isEmpty(new List<Integer> {1, 2})); System.Assert.isFalse(fflib_ArrayUtils.isEmpty(new List<Integer> {1, 2}));
} }
@IsTest @IsTest
static void testIsNotEmpty() static void testIsNotEmpty()
{ {
List<String> aNull = null; List<String> aNull = null;
System.Assert.isfalse(fflib_ArrayUtils.isNotEmpty(aNull)); System.Assert.isFalse(fflib_ArrayUtils.isNotEmpty(aNull));
System.Assert.isfalse(fflib_ArrayUtils.isNotEmpty(new List<String> {})); System.Assert.isFalse(fflib_ArrayUtils.isNotEmpty(new List<String> {}));
System.Assert.istrue(fflib_ArrayUtils.isNotEmpty(new List<String> {'a'})); System.Assert.isTrue(fflib_ArrayUtils.isNotEmpty(new List<String> {'a'}));
System.Assert.istrue(fflib_ArrayUtils.isNotEmpty(new List<String> {'a', 'b'})); System.Assert.isTrue(fflib_ArrayUtils.isNotEmpty(new List<String> {'a', 'b'}));
System.Assert.isfalse(fflib_ArrayUtils.isNotEmpty(new List<Integer> {})); System.Assert.isFalse(fflib_ArrayUtils.isNotEmpty(new List<Integer> {}));
System.Assert.istrue(fflib_ArrayUtils.isNotEmpty(new List<Integer> {1})); System.Assert.isTrue(fflib_ArrayUtils.isNotEmpty(new List<Integer> {1}));
System.Assert.istrue(fflib_ArrayUtils.isNotEmpty(new List<Integer> {1, 2})); System.Assert.isTrue(fflib_ArrayUtils.isNotEmpty(new List<Integer> {1, 2}));
System.Assert.isfalse(fflib_ArrayUtils.isNotEmpty(new List<Account> {})); System.Assert.isFalse(fflib_ArrayUtils.isNotEmpty(new List<Account> {}));
System.Assert.istrue(fflib_ArrayUtils.isNotEmpty(new List<Account> {new Account(Name = 'test')})); System.Assert.isTrue(fflib_ArrayUtils.isNotEmpty(new List<Account> {new Account(Name = 'test')}));
} }
@IsTest @IsTest
@@ -368,7 +368,7 @@ private class fflib_ArrayUtilsTest
Set<String> result = fflib_ArrayUtils.getAllValues(stringsByIds, new Set<Id> {accountIdA, accountIdB}); Set<String> result = fflib_ArrayUtils.getAllValues(stringsByIds, new Set<Id> {accountIdA, accountIdB});
// THEN we get the values of the provide sub-set Ids // THEN we get the values of the provide sub-set Ids
System.Assert.istrue(result.containsAll(new Set<String> {'Account A', 'Account B'})); System.Assert.isTrue(result.containsAll(new Set<String> {'Account A', 'Account B'}));
} }
@IsTest @IsTest
@@ -392,15 +392,46 @@ private class fflib_ArrayUtilsTest
Map<Id, Set<String>> result = fflib_ArrayUtils.mergeMaps(stringsByIdsA, stringsByIdsB); Map<Id, Set<String>> result = fflib_ArrayUtils.mergeMaps(stringsByIdsA, stringsByIdsB);
System.Assert.istrue(result.keySet().containsAll(ids)); System.Assert.isTrue(result.keySet().containsAll(ids));
System.Assert.istrue(result.get(accountIdA).containsAll(new Set<String>{'A1', 'A2', 'A3'})); System.Assert.isTrue(result.get(accountIdA).containsAll(new Set<String>{'A1', 'A2', 'A3'}));
System.Assert.areEqual(3, result.get(accountIdA).size()); 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.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()); 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<Id> ids = new Set<Id> {accountIdA, accountIdB, accountIdC, accountIdD};
final Map<Id, Boolean> booleanByIdA = new Map<Id, Boolean>
{
accountIdA => true,
accountIdB => false,
accountIdD=> false
};
final Map<Id, Boolean> booleanByIdB = new Map<Id, Boolean>
{
accountIdA => false,
accountIdB => true,
accountIdC => true
};
Map<Id, Boolean> 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<Datetime> generateDateTimes() private static List<Datetime> generateDateTimes()
{ {
return new List<Datetime> return new List<Datetime>