Add documentation for fflib_Criteria

This commit is contained in:
Wim Velzeboer
2021-07-06 16:32:38 +01:00
parent 014c145d9a
commit d5bfc3c48c
8 changed files with 561 additions and 85 deletions
@@ -64,7 +64,7 @@ public virtual with sharing class fflib_Criteria
* Evaluates:
* Name = 'Example' OR AccountNumber = '1234567'
*/
public fflib_Criteria orCriteria()
public virtual fflib_Criteria orCriteria()
{
this.type = 'OR';
return this;
@@ -75,10 +75,10 @@ public virtual with sharing class fflib_Criteria
* By default the comparator is set to AND,
* so this method should only be used in custom implementations extended from fflib_Criteria
*
* @return Instance of fflib_Criteria
* @return An instance of itself to enable method chaining
*
* @example
* new MyCustomCriteria()
* new fflib_Criteria()
* .andCriteria()
* .equalTo(Account.Name, 'Example')
* .equalTo(Account.AccountNumber, '1234567')
@@ -86,7 +86,7 @@ public virtual with sharing class fflib_Criteria
* Evaluates:
* Name = 'Example' AND AccountNumber = '1234567'
*/
public fflib_Criteria andCriteria()
public virtual fflib_Criteria andCriteria()
{
this.type = 'AND';
return this;
@@ -97,7 +97,7 @@ public virtual with sharing class fflib_Criteria
*
* @param subCriteria The condition of the sub criteria
*
* @return Instance of fflib_Criteria
* @return An instance of itself to enable method chaining
*
* @example
* new fflib_Criteria()
@@ -110,7 +110,7 @@ public virtual with sharing class fflib_Criteria
* Evaluates:
* Account.Name = 'Example' AND (Account.AccountNumber = '0001' OR Account.AccountNumber = '0002')
*/
public fflib_Criteria addOrCriteria(fflib_Criteria subCriteria)
public virtual fflib_Criteria addOrCriteria(fflib_Criteria subCriteria)
{
subCriteria.orCriteria();
subCriteria.setEmbraced(true);
@@ -123,7 +123,7 @@ public virtual with sharing class fflib_Criteria
*
* @param subCriteria The condition of the sub criteria
*
* @return Instance of fflib_Criteria
* @return An instance of itself to enable method chaining
*
* @example
* new fflib_Criteria()
@@ -137,7 +137,7 @@ public virtual with sharing class fflib_Criteria
* Evaluates:
* Name = 'Example' OR (AccountNumber = '0001' AND ShippingCountry = 'USA')
*/
public fflib_Criteria addAndCriteria(fflib_Criteria subCriteria)
public virtual fflib_Criteria addAndCriteria(fflib_Criteria subCriteria)
{
subCriteria.andCriteria();
subCriteria.setEmbraced(true);
@@ -146,12 +146,12 @@ public virtual with sharing class fflib_Criteria
}
/**
* add an equal to criteria comparing a fields value to a given value
* Add an equal to criteria comparing a fields value to a given value
*
* @param field The sObjectField to evaluate
* @param value The value to be compared to the fields value
*
* @return Instance of fflib_Criteria
* @return An instance of itself to enable method chaining
*
* @example
* new fflib_Criteria()
@@ -160,19 +160,19 @@ public virtual with sharing class fflib_Criteria
* Evaluates:
* Name = 'Example'
*/
public fflib_Criteria equalTo(Schema.SObjectField field, Object value)
public virtual fflib_Criteria equalTo(Schema.SObjectField field, Object value)
{
evaluators.add(new FieldEvaluator(field, fflib_Operator.EQUALS, value));
return this;
}
/**
* add a not equal to criteria while comparing a fields value to a given value
* Add a not equal to criteria while comparing a fields value to a given value
*
* @param field The sObjectField to evaluate
* @param value The value to be compared to the fields value
*
* @return Instance of fflib_Criteria
* @return An instance of itself to enable method chaining
*
* @example
* new fflib_Criteria()
@@ -181,7 +181,7 @@ public virtual with sharing class fflib_Criteria
* Evaluates:
* Name != 'Example'
*/
public fflib_Criteria notEqualTo(Schema.SObjectField field, Object value)
public virtual fflib_Criteria notEqualTo(Schema.SObjectField field, Object value)
{
evaluators.add(new FieldEvaluator(field, fflib_Operator.NOT_EQUALS, value));
return this;
@@ -194,7 +194,7 @@ public virtual with sharing class fflib_Criteria
*
* @return The Boolean result of the evaluated criteria
*/
public Boolean evaluate(Object record)
public virtual Boolean evaluate(Object record)
{
if (this.evaluators.isEmpty()) return true;
@@ -215,13 +215,13 @@ public virtual with sharing class fflib_Criteria
}
/**
* compares the fields values to be greater or equal to the given value
* Compares the fields values to be greater or equal to the given value
* Evaluates to false if the field value is null
*
* @param field The field to use its value
* @param field The Schema.sObjectField to use its value
* @param value The value to be compared to the fields value
*
* @return Instance of fflib_Criteria
* @return An instance of itself to enable method chaining
*
* @example
* new fflib_Criteria()
@@ -230,20 +230,20 @@ public virtual with sharing class fflib_Criteria
* Evaluates:
* AnnualRevenue >= 54321
*/
public fflib_Criteria greaterOrEqualTo(Schema.SObjectField field, Object value)
public virtual fflib_Criteria greaterOrEqualTo(Schema.SObjectField field, Object value)
{
evaluators.add(new FieldEvaluator(field, fflib_Operator.GREATER_THAN_OR_EQUAL_TO, value));
return this;
}
/**
* compares the fields values to be greater or equal to the given value
* Compares the fields values to be greater or equal to the given value
* Evaluates to false if the field value is null
*
* @param field The field to use its value
* @param field The Schema.SObjectField to use its value
* @param value The value to be compared to the fields value
*
* @return Instance of fflib_Criteria
* @return An instance of itself to enable method chaining
*
* @example
* new fflib_Criteria()
@@ -252,20 +252,20 @@ public virtual with sharing class fflib_Criteria
* Evaluates:
* AnnualRevenue > 54321
*/
public fflib_Criteria greaterThan(Schema.SObjectField field, Object value)
public virtual fflib_Criteria greaterThan(Schema.SObjectField field, Object value)
{
evaluators.add(new FieldEvaluator(field, fflib_Operator.GREATER_THAN, value));
return this;
}
/**
* compares the fields values to be less or equal to the given value
* Compares the fields values to be less or equal to the given value
* Evaluates to true if the field value is null
*
* @param field The field to use its value
* @param field The Schema.SObjectField to use its value
* @param value The value to be compared to the fields value
*
* @return Instance of fflib_Criteria
* @return An instance of itself to enable method chaining
*
* @example
* new fflib_Criteria()
@@ -274,7 +274,7 @@ public virtual with sharing class fflib_Criteria
* Evaluates:
* AnnualRevenue <= 54321
*/
public fflib_Criteria lessOrEqualTo(Schema.SObjectField field, Object value)
public virtual fflib_Criteria lessOrEqualTo(Schema.SObjectField field, Object value)
{
evaluators.add(new FieldEvaluator(field, fflib_Operator.LESS_THAN_OR_EQUAL_TO, value));
return this;
@@ -282,13 +282,13 @@ public virtual with sharing class fflib_Criteria
/**
* compares the fields values to be greater or equal to the given value
* Compares the fields values to be greater or equal to the given value
* Evaluates to true if the field value is null
*
* @param field The field to use its value
* @param field The Schema.sObjectField to use its value
* @param value The value to be compared to the fields value
*
* @return Instance of fflib_Criteria
* @return An instance of itself to enable method chaining
*
* @example
* new fflib_Criteria()
@@ -297,19 +297,19 @@ public virtual with sharing class fflib_Criteria
* Evaluates:
* AnnualRevenue < 54321
*/
public fflib_Criteria lessThan(Schema.SObjectField field, Object value)
public virtual fflib_Criteria lessThan(Schema.SObjectField field, Object value)
{
evaluators.add(new FieldEvaluator(field, fflib_Operator.LESS_THAN, value));
return this;
}
/**
* checks if the given sets contains the fields values
* Checks if the given sets contains the fields values
*
* @param field The field to use its value
* @param values The value to be compared to the fields value
* @param field The Schema.sObjectField to use its value
* @param values The values to be compared to the fields value
*
* @return Instance of fflib_Criteria
* @return An instance of itself to enable method chaining
*
* @example
* new fflib_Criteria()
@@ -318,7 +318,7 @@ public virtual with sharing class fflib_Criteria
* Evaluates:
* Account.Type IN ('Customer','Competitor','Partner')
*/
public fflib_Criteria inSet(Schema.SObjectField field, Set<Object> values)
public virtual fflib_Criteria inSet(Schema.SObjectField field, Set<Object> values)
{
return inSet(field, new fflib_Objects(new List<Object>(values)));
}
@@ -361,7 +361,7 @@ public virtual with sharing class fflib_Criteria
* @param field The field to use its value
* @param values The value to be compared to the fields value
*
* @return Instance of fflib_Criteria
* @return An instance of itself to enable method chaining
*
* @example
* new fflib_Criteria()
@@ -377,12 +377,12 @@ public virtual with sharing class fflib_Criteria
}
/**
* checks if the given sets contains the fields values
* Checks if the given sets does not contain the fields values
*
* @param field The field to use its value
* @param field The Schema.sObjectField to use its value
* @param values The value to be compared to the fields value
*
* @return Instance of fflib_Criteria
* @return An instance of itself to enable method chaining
*
* @example
* new fflib_Criteria()
@@ -434,7 +434,7 @@ public virtual with sharing class fflib_Criteria
* @param field The field to use its value
* @param values The value to be compared to the fields value
*
* @return Instance of fflib_Criteria
* @return An instance of itself to enable method chaining
*
* @example
* new fflib_Criteria()
@@ -454,7 +454,7 @@ public virtual with sharing class fflib_Criteria
*
* @return The "where" part in the SOQL statement
*/
public String toSOQL()
public virtual String toSOQL()
{
if (this.evaluators.isEmpty()) return '';
@@ -1,252 +0,0 @@
/**
* File Name: ArrayTest
* @description
*
* @author architect ir. Wilhelmus G.J. Velzeboer | wvelzebo@its.jnj.com
*/
@IsTest(IsParallel=true)
private class ArrayUtilsTest
{
@IsTest
static void testObjectToString()
{
System.assertEquals(
new List<String> {'123', '456', '789'},
ArrayUtils.objectToString(
new List<Object> {123, 456, 789}
)
);
}
@IsTest
static void testObjectStringToString()
{
System.assertEquals(
new List<String> {'123', '456', '789'},
ArrayUtils.objectToString(
new List<Object> {'123', '456', '789'}
)
);
}
@IsTest
static void itShouldReturnEmptyListOnEmptyObjectToString()
{
System.assertEquals(
new List<String>(),
ArrayUtils.objectToString(
new List<Object>()
)
);
}
@IsTest
static void itShouldConvertSetToListOfString()
{
System.assertEquals(
new List<String> {'1', '2', '3'},
ArrayUtils.objectToString(
new Set<Object> {1, 2, 3}
)
);
}
@IsTest
static void itShouldConvertSetObjectToSetOfIds()
{
Id idA = fflib_IDGenerator.generate(Account.SObjectType);
Id idB = fflib_IDGenerator.generate(Account.SObjectType);
System.assertEquals(
new Set<Id> {idA, idB},
ArrayUtils.objectToIds(
new Set<Object>
{
(Object) idA,
(Object) idB
}
)
);
}
@IsTest
static void itShouldReturnReversedObjectList()
{
System.assertEquals(
new List<Object>
{
(Object) 'C',
(Object) 'B',
(Object) 'A'
},
ArrayUtils.reverse(
new List<Object>
{
(Object) 'A',
(Object) 'B',
(Object) 'C'
}
)
);
}
@IsTest
static void itShouldReturnReversedSObjectList()
{
System.assertEquals(
new List<SObject>
{
(SObject) new Account(Name = 'C'),
(SObject) new Account(Name = 'B'),
(SObject) new Account(Name = 'A')
},
ArrayUtils.reverse(
new List<SObject>
{
(SObject) new Account(Name = 'A'),
(SObject) new Account(Name = 'B'),
(SObject) new Account(Name = 'C')
}
)
);
}
@IsTest
static void itShouldConvertListOfStringsIntoUpperCase()
{
System.assertEquals(
new List<String>
{
'TEST A',
'TEST B',
'TEST C'
},
ArrayUtils.upperCase(
new List<String>
{
'test a',
'test b',
'test c'
}
)
);
}
@IsTest
static void testLowerCase()
{
System.assertEquals(
new List<String> {'abc', 'def'},
ArrayUtils.lowerCase(
new List<String> {'ABC', 'DEF'}
)
);
}
@IsTest
static void testIsEmpty()
{
List<String> aNull = null;
System.assertEquals(true, ArrayUtils.isEmpty(aNull));
System.assertEquals(true, ArrayUtils.isEmpty(new List<String> {}));
System.assertEquals(false, ArrayUtils.isEmpty(new List<String> {'a'}));
System.assertEquals(false, ArrayUtils.isEmpty(new List<String> {'a', 'b'}));
System.assertEquals(true, ArrayUtils.isEmpty(new List<Integer> {}));
System.assertEquals(false, ArrayUtils.isEmpty(new List<Integer> {1}));
System.assertEquals(false, ArrayUtils.isEmpty(new List<Integer> {1, 2}));
}
@IsTest
static void testIsNotEmpty()
{
List<String> aNull = null;
System.assertEquals(false, ArrayUtils.isNotEmpty(aNull));
System.assertEquals(false, ArrayUtils.isNotEmpty(new List<String> {}));
System.assertEquals(true, ArrayUtils.isNotEmpty(new List<String> {'a'}));
System.assertEquals(true, ArrayUtils.isNotEmpty(new List<String> {'a', 'b'}));
System.assertEquals(false, ArrayUtils.isNotEmpty(new List<Integer> {}));
System.assertEquals(true, ArrayUtils.isNotEmpty(new List<Integer> {1}));
System.assertEquals(true, ArrayUtils.isNotEmpty(new List<Integer> {1, 2}));
System.assertEquals(false, ArrayUtils.isNotEmpty(new List<Account> {}));
System.assertEquals(true, ArrayUtils.isNotEmpty(new List<Account> {new Account(Name = 'test')}));
}
@IsTest
static void itShouldReturnMostRecentDatetime()
{
// GIVEN - a list of unsorted Datetime values
List<Datetime> datetimes = generateDateTimes();
// WHEN - we request the maxDatetime
Datetime maxDatetime = ArrayUtils.maxDatetime(datetimes);
// THEN - the highest Datetime should be returned
System.assertEquals(datetimes.get(1), maxDatetime);
}
@IsTest
static void itShouldReturnOldestDatetime()
{
// GIVEN - a list of unsorted Datetime values
List<Datetime> datetimes = generateDateTimes();
// WHEN - we request the maxDatetime
Datetime maxDatetime = ArrayUtils.minDatetime(datetimes);
// THEN - the highest Datetime should be returned
System.assertEquals(datetimes.get(4), maxDatetime);
}
@IsTest
static void itShouldHandleNullInputsForMaxDatatime()
{
System.assertEquals(
null, ArrayUtils.maxDatetime(new List<Datetime>()),
'maxDatetime method was unable to handle an empty list as input'
);
System.assertEquals(
null, ArrayUtils.maxDatetime(null),
'maxDatetime method was unable to handle null as input'
);
}
@IsTest
static void itShouldCloneAndSortTheList()
{
// GIVEN - a list of unsorted numbers
List<Integer> unsorted = new List<Integer>
{
1, 30, 2, 76, -7
};
// WHEN - we request the a cloned and sorted list
List<Integer> sorted = ArrayUtils.cloneAndSort(unsorted);
System.assertNotEquals(unsorted, sorted, 'The original object was modified, something went wrong with the cloning');
System.assertEquals(new List<Integer> {-7, 1, 2, 30, 76}, sorted, 'Incorrect sorted list returned');
}
@IsTest
static void itShouldReturnTheLastValueInTheList()
{
// GIVEN - a list of unsorted values
List<Object> objects = new List<Object> {1, 2, 3, 9, 4, 7, 8};
// WHEN - we request the last value
Object result = ArrayUtils.lastValue(objects);
// THEN - the highest Datetime should be returned
System.assertEquals(8, result, 'Did not return the last value in the list of objects');
}
private static List<Datetime> generateDateTimes()
{
return new List<Datetime>
{
Datetime.newInstance(2020, 01, 01, 07, 59, 7),
Datetime.newInstance(2020, 02, 01, 07, 40, 7),
Datetime.newInstance(2020, 02, 01, 07, 00, 7),
Datetime.newInstance(2019, 02, 01, 07, 59, 7),
Datetime.newInstance(2018, 07, 07, 07, 7, 7)
};
}
}
@@ -1,5 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>51.0</apiVersion>
<status>Active</status>
</ApexClass>
@@ -1,5 +1,5 @@
/**
* File Name: Array
* File Name: fflib_ArrayUtils
* @description
*
* @author architect ir. Wilhelmus G.J. Velzeboer
@@ -28,10 +28,10 @@
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
public class ArrayUtils
public class fflib_ArrayUtils
{
/* private constructor to prevent instantiation */
private ArrayUtils() {}
private fflib_ArrayUtils() {}
/**
* Creates a clone from the provided list and returns the sorted list