Skip to content

Commit

Permalink
Clean up. Add SObjectFunction interface to DebugSObject. Rename noun-…
Browse files Browse the repository at this point in the history
…like functions to verb-like.
  • Loading branch information
ipavlic committed Nov 26, 2024
1 parent 7952bc5 commit 3533a9e
Show file tree
Hide file tree
Showing 39 changed files with 149 additions and 207 deletions.
42 changes: 21 additions & 21 deletions force-app/main/default/classes/collection/ObjectCollection.cls
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
public with sharing class ObjectCollection {
private List<Object> objects;
private List<Object> objects;

public static ObjectCollection of(Iterable<Object> objects) {
return new ObjectCOllection(objects);
}
public static ObjectCollection of(Iterable<Object> objects) {
return new ObjectCollection(objects);
}

private ObjectCollection(Iterable<Object> objects) {
this.objects = new List<Object>();
Iterator<Object> iter = objects.iterator();
while (iter.hasNext()) {
this.objects.add(iter.next());
}
}
private ObjectCollection(Iterable<Object> objects) {
this.objects = new List<Object>();
Iterator<Object> iter = objects.iterator();
while (iter.hasNext()) {
this.objects.add(iter.next());
}
}

public List<Object> asList(Type listType) {
List<Object> typedObjects = (List<Object>) listType.newInstance();
typedObjects.addAll(this.objects);
return typedObjects;
}
public List<Object> asList(Type listType) {
List<Object> typedObjects = (List<Object>) listType.newInstance();
typedObjects.addAll(this.objects);
return typedObjects;
}

public Set<Object> asSet(Type setType) {
Set<Object> typedObjects = (Set<Object>) setType.newInstance();
typedObjects.addAll(this.objects);
return typedObjects;
}
public Set<Object> asSet(Type setType) {
Set<Object> typedObjects = (Set<Object>) setType.newInstance();
typedObjects.addAll(this.objects);
return typedObjects;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
public with sharing class SObjectCollection {

private List<SObject> records;
private SObjectFieldReader fieldReader;
private final List<SObject> records;
private final SObjectFieldReader fieldReader;

public static SObjectCollection of(Iterable<SObject> records) {
return new SObjectCollection(records);
Expand Down
2 changes: 1 addition & 1 deletion force-app/main/default/classes/function/DebugSObject.cls
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
public with sharing class DebugSObject {
public with sharing class DebugSObject implements SObjectFunction {
private System.LoggingLevel logLevel = System.LoggingLevel.DEBUG;

public DebugSObject logLevel(LoggingLevel logLevel) {
Expand Down
84 changes: 0 additions & 84 deletions force-app/main/default/classes/function/IncompleteFieldsMatch.cls

This file was deleted.

84 changes: 84 additions & 0 deletions force-app/main/default/classes/function/IncompleteMatchFields.cls
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
public class IncompleteMatchFields {
private final MatchFields baseMatch;
private final String fieldPath;

public IncompleteMatchFields(MatchFields baseMatch, Schema.SObjectField field) {
this.baseMatch = baseMatch;
this.fieldPath = field.getDescribe().getName();
}

public IncompleteMatchFields(MatchFields baseMatch, String fieldPath) {
this.baseMatch = baseMatch;
this.fieldPath = fieldPath;
}

// helper for all other methods
private MatchFields filterWith(BinaryRelation relation, Object value) {
this.baseMatch.addCondition(new MatchFieldCondition(this.fieldPath, relation, value));
return this.baseMatch;
}

public MatchFields equals(Object value) {
return filterWith(BinaryRelation.EQUALS, value);
}

public MatchFields eq(Object value) {
return equals(value);
}

public MatchFields notEquals(Object value) {
return filterWith(BinaryRelation.NOT_EQUALS, value);
}

public MatchFields neq(Object value) {
return notEquals(value);
}

public MatchFields lessThan(Object value) {
return filterWith(BinaryRelation.LESS_THAN, value);
}

public MatchFields lt(Object value) {
return lessThan(value);
}

public MatchFields lessThanOrEquals(Object value) {
return filterWith(BinaryRelation.LESS_THAN_OR_EQUALS, value);
}

public MatchFields leq(Object value) {
return lessThanOrEquals(value);
}

public MatchFields greaterThan(Object value) {
return filterWith(BinaryRelation.GREATER_THAN, value);
}

public MatchFields gt(Object value) {
return greaterThan(value);
}

public MatchFields greaterThanOrEquals(Object value) {
return filterWith(BinaryRelation.GREATER_THAN_OR_EQUALS, value);
}

public MatchFields geq(Object value) {
return greaterThanOrEquals(value);
}

public MatchFields hasValue() {
return notEquals(null);
}

public MatchFields isIn(Object value) {
return filterWith(BinaryRelation.IS_IN, value);
}

public MatchFields notIn(Object value) {
return filterWith(BinaryRelation.NOT_IN, value);
}

public MatchFields isNotIn(Object value) {
return notIn(value);
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
public class FieldMatchCondition {
public class MatchFieldCondition {

public String fieldPath { get; set;}
public BinaryRelation relation { get; set;}
public Object value { get; set;}

public FieldMatchCondition(String fieldPath, BinaryRelation relation, Object value) {
public MatchFieldCondition(String fieldPath, BinaryRelation relation, Object value) {
this.fieldPath = fieldPath;
this.relation = relation;
this.value = value;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
public class FieldsMatch implements SObjectPredicate {
public class MatchFields implements SObjectPredicate {

private static final PrimitiveComparer primitiveComparer = new PrimitiveComparer();
private static final SObjectFieldReader fieldReader = new SObjectFieldReader();
Expand All @@ -8,43 +8,43 @@ public class FieldsMatch implements SObjectPredicate {
BinaryRelation.NOT_IN
};

private final List<FieldMatchCondition> matchConditions;
private final List<MatchFieldCondition> matchConditions;

public FieldsMatch() {
this.matchConditions = new List<FieldMatchCondition>();
public MatchFields() {
this.matchConditions = new List<MatchFieldCondition>();
}

public FieldsMatch addCondition(FieldMatchCondition condition) {
public MatchFields addCondition(MatchFieldCondition condition) {
matchConditions.add(condition);
return this;
}

public IncompleteFieldsMatch also(Schema.SObjectField field) {
public IncompleteMatchFields also(Schema.SObjectField field) {
return field(field);
}

public IncompleteFieldsMatch also(String fieldPath) {
public IncompleteMatchFields also(String fieldPath) {
return field(fieldPath);
}

public IncompleteFieldsMatch field(Schema.SObjectField field) {
return new IncompleteFieldsMatch(this, field);
public IncompleteMatchFields field(Schema.SObjectField field) {
return new IncompleteMatchFields(this, field);
}

public IncompleteFieldsMatch field(String fieldPath) {
return new IncompleteFieldsMatch(this, fieldPath);
public IncompleteMatchFields field(String fieldPath) {
return new IncompleteMatchFields(this, fieldPath);
}

public Boolean call(SObject record) {
for (FieldMatchCondition condition : matchConditions) {
for (MatchFieldCondition condition : matchConditions) {
if (!conditionSatisfied(condition, record)) {
return false;
}
}
return true;
}

private Boolean conditionSatisfied(FieldMatchCondition condition, SObject record) {
private Boolean conditionSatisfied(MatchFieldCondition condition, SObject record) {
Object fieldValue = fieldReader.read(record, condition.fieldPath);
if (setComparisons.contains(condition.relation)) {
return setConditionSatisfied(condition, fieldValue);
Expand All @@ -53,7 +53,7 @@ public class FieldsMatch implements SObjectPredicate {
}
}

private Boolean setConditionSatisfied(FieldMatchCondition condition, Object fieldValue) {
private Boolean setConditionSatisfied(MatchFieldCondition condition, Object fieldValue) {
Boolean isValueContained = contains(condition.value, fieldValue);
switch on condition.relation {
when IS_IN {
Expand All @@ -68,7 +68,7 @@ public class FieldsMatch implements SObjectPredicate {
}
}

private Boolean comparisonConditionSatisfied(FieldMatchCondition condition, Object fieldValue) {
private Boolean comparisonConditionSatisfied(MatchFieldCondition condition, Object fieldValue) {
if (condition.value == null) {
switch on condition.relation {
when EQUALS {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
public class RecordFieldsMatch implements SObjectPredicate {
public class MatchRecordFields implements SObjectPredicate {
private final SObject prototype;

public RecordFieldsMatch(sObject prototype) {
public MatchRecordFields(sObject prototype) {
this.prototype = prototype;
}

Expand Down
12 changes: 6 additions & 6 deletions force-app/main/default/classes/function/MatchSObject.cls
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
public with sharing class MatchSObject {
public RecordFieldsMatch recordFields(SObject prototype) {
return new RecordFieldsMatch(prototype);
public MatchRecordFields recordFields(SObject prototype) {
return new MatchRecordFields(prototype);
}

public IncompleteFieldsMatch field(Schema.SObjectField field) {
return new IncompleteFieldsMatch(new FieldsMatch(), field);
public IncompleteMatchFields field(Schema.SObjectField field) {
return new IncompleteMatchFields(new MatchFields(), field);
}

public IncompleteFieldsMatch field(String fieldPath) {
return new IncompleteFieldsMatch(new FieldsMatch(), fieldPath);
public IncompleteMatchFields field(String fieldPath) {
return new IncompleteMatchFields(new MatchFields(), fieldPath);
}
}
Loading

0 comments on commit 3533a9e

Please sign in to comment.