-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Simplify the code base, and make Collection a base class for function…
…al methods, removing reliance on static classes
- Loading branch information
Showing
55 changed files
with
1,334 additions
and
1,395 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,318 @@ | ||
public with sharing class Collection { | ||
|
||
private List<SObject> records; | ||
|
||
public static Collection of(Iterable<SObject> records) { | ||
return new Collection(records); | ||
} | ||
|
||
public static Collection of(Set<SObject> records) { | ||
return new Collection(new List<SObject>(records)); | ||
} | ||
|
||
private Collection(Iterable<SObject> records) { | ||
this.records = new List<SObject>(); | ||
Iterator<SObject> iter = records.iterator(); | ||
while (iter.hasNext()) { | ||
this.records.add(iter.next()); | ||
} | ||
} | ||
|
||
public List<SObject> asList() { | ||
return new List<SObject>(records); | ||
} | ||
|
||
public List<SObject> asList(Type listType) { | ||
List<SObject> typedList = (List<SObject>) listType.newInstance(); | ||
typedList.addAll(records); | ||
return typedList; | ||
} | ||
|
||
public Set<SObject> asSet() { | ||
return new Set<SObject>(records); | ||
} | ||
|
||
public Set<SObject> asSet(Type setType) { | ||
Set<SObject> typedSet = (Set<SObject>) setType.newInstance(); | ||
typedSet.addAll(records); | ||
return typedSet; | ||
} | ||
|
||
public Collection filter(SObjectPredicate predicate) { | ||
List<SObject> filtered = new List<SObject>(); | ||
for (SObject record : records) { | ||
if (predicate.apply(record)) { | ||
filtered.add(record); | ||
} | ||
} | ||
return Collection.of(filtered); | ||
} | ||
|
||
public Collection remove(SObjectPredicate predicate) { | ||
List<SObject> remaining = new List<SObject>(); | ||
for (SObject record : records) { | ||
if (!predicate.apply(record)) { | ||
remaining.add(record); | ||
} | ||
} | ||
return Collection.of(remaining); | ||
} | ||
|
||
public List<Boolean> pluckBooleans(Schema.SObjectField field) { | ||
List<Boolean> results = new List<Boolean>(); | ||
for (SObject rec : records) { | ||
results.add((Boolean)rec.get(field)); | ||
} | ||
return results; | ||
} | ||
|
||
public List<Boolean> pluckBooleans(String relation) { | ||
List<Boolean> results = new List<Boolean>(); | ||
SObjectFieldReader reader = new SObjectFieldReader(); | ||
for (SObject rec : records) { | ||
results.add((Boolean)reader.read(rec, relation)); | ||
} | ||
return results; | ||
} | ||
|
||
public List<Date> pluckDates(Schema.SObjectField field) { | ||
List<Date> results = new List<Date>(); | ||
for (SObject rec : records) { | ||
results.add((Date)rec.get(field)); | ||
} | ||
return results; | ||
} | ||
|
||
public List<Date> pluckDates(String relation) { | ||
List<Date> results = new List<Date>(); | ||
SObjectFieldReader reader = new SObjectFieldReader(); | ||
for (SObject rec : records) { | ||
results.add((Date)reader.read(rec, relation)); | ||
} | ||
return results; | ||
} | ||
|
||
public List<Datetime> pluckDatetimes(Schema.SObjectField field) { | ||
List<Datetime> results = new List<Datetime>(); | ||
for (SObject rec : records) { | ||
results.add((Datetime)rec.get(field)); | ||
} | ||
return results; | ||
} | ||
|
||
public List<Datetime> pluckDatetimes(String relation) { | ||
List<Datetime> results = new List<Datetime>(); | ||
SObjectFieldReader reader = new SObjectFieldReader(); | ||
for (SObject rec : records) { | ||
results.add((Datetime)reader.read(rec, relation)); | ||
} | ||
return results; | ||
} | ||
|
||
public List<Decimal> pluckDecimals(Schema.SObjectField field) { | ||
List<Decimal> results = new List<Decimal>(); | ||
for (SObject rec : records) { | ||
results.add((Decimal)rec.get(field)); | ||
} | ||
return results; | ||
} | ||
|
||
public List<Decimal> pluckDecimals(String relation) { | ||
List<Decimal> results = new List<Decimal>(); | ||
SObjectFieldReader reader = new SObjectFieldReader(); | ||
for (SObject rec : records) { | ||
results.add((Decimal)reader.read(rec, relation)); | ||
} | ||
return results; | ||
} | ||
|
||
public List<Id> pluckIds() { | ||
List<Id> results = new List<Id>(); | ||
for (SObject rec : records) { | ||
results.add(rec.Id); | ||
} | ||
return results; | ||
} | ||
|
||
public List<Id> pluckIds(Schema.SObjectField field) { | ||
List<Id> results = new List<Id>(); | ||
for (SObject rec : records) { | ||
results.add((Id)rec.get(field)); | ||
} | ||
return results; | ||
} | ||
|
||
public List<Id> pluckIds(String relation) { | ||
List<Id> results = new List<Id>(); | ||
SObjectFieldReader reader = new SObjectFieldReader(); | ||
for (SObject rec : records) { | ||
results.add((Id)reader.read(rec, relation)); | ||
} | ||
return results; | ||
} | ||
|
||
public List<String> pluckStrings(Schema.SObjectField field) { | ||
List<String> results = new List<String>(); | ||
for (SObject rec : records) { | ||
results.add((String)rec.get(field)); | ||
} | ||
return results; | ||
} | ||
|
||
public List<String> pluckStrings(String relation) { | ||
List<String> results = new List<String>(); | ||
SObjectFieldReader reader = new SObjectFieldReader(); | ||
for (SObject rec : records) { | ||
results.add((String) reader.read(rec, relation)); | ||
} | ||
return results; | ||
} | ||
|
||
public Map<Boolean, List<SObject>> groupByBooleans(Schema.SObjectField field, Type listType) { | ||
Map<Boolean, List<SObject>> grouped = new Map<Boolean, List<SObject>>(); | ||
for (SObject rec : records) { | ||
Boolean key = (Boolean) rec.get(field); | ||
if (!grouped.containsKey(key)) { | ||
grouped.put(key, (List<SObject>) listType.newInstance()); | ||
} | ||
grouped.get(key).add(rec); | ||
} | ||
return grouped; | ||
} | ||
|
||
public Map<Boolean, List<SObject>> groupByBooleans(Schema.SObjectField field) { | ||
return groupByBooleans(field, List<SObject>.class); | ||
} | ||
|
||
public Map<Date, List<SObject>> groupByDates(Schema.SObjectField field, Type listType) { | ||
Map<Date, List<SObject>> grouped = new Map<Date, List<SObject>>(); | ||
for (SObject rec : records) { | ||
Date key = (Date) rec.get(field); | ||
if (!grouped.containsKey(key)) { | ||
grouped.put(key, (List<SObject>) listType.newInstance()); | ||
} | ||
grouped.get(key).add(rec); | ||
} | ||
return grouped; | ||
} | ||
|
||
public Map<Datetime, List<SObject>> groupByDatetimes(Schema.SObjectField field) { | ||
return groupByDatetimes(field, List<SObject>.class); | ||
} | ||
|
||
public Map<Datetime, List<SObject>> groupByDatetimes(Schema.SObjectField field, Type listType) { | ||
Map<Datetime, List<SObject>> grouped = new Map<Datetime, List<SObject>>(); | ||
for (SObject rec : records) { | ||
Datetime key = (Datetime) rec.get(field); | ||
if (!grouped.containsKey(key)) { | ||
grouped.put(key, (List<SObject>) listType.newInstance()); | ||
} | ||
grouped.get(key).add(rec); | ||
} | ||
return grouped; | ||
} | ||
|
||
public Map<Date, List<SObject>> groupByDates(Schema.SObjectField field) { | ||
return groupByDates(field, List<SObject>.class); | ||
} | ||
|
||
public Map<Decimal, List<SObject>> groupByDecimals(Schema.SObjectField field, Type listType) { | ||
Map<Decimal, List<SObject>> grouped = new Map<Decimal, List<SObject>>(); | ||
for (SObject rec : records) { | ||
Decimal key = (Decimal) rec.get(field); | ||
if (!grouped.containsKey(key)) { | ||
grouped.put(key, (List<SObject>) listType.newInstance()); | ||
} | ||
grouped.get(key).add(rec); | ||
} | ||
return grouped; | ||
} | ||
|
||
public Map<Decimal, List<SObject>> groupByDecimals(Schema.SObjectField field) { | ||
return groupByDecimals(field, List<SObject>.class); | ||
} | ||
|
||
public Map<Id, List<SObject>> groupByIds(Schema.SObjectField field, Type listType) { | ||
Map<Id, List<SObject>> grouped = new Map<Id, List<SObject>>(); | ||
for (SObject rec : records) { | ||
Id key = (Id) rec.get(field); | ||
if (!grouped.containsKey(key)) { | ||
grouped.put(key, (List<SObject>) listType.newInstance()); | ||
} | ||
grouped.get(key).add(rec); | ||
} | ||
return grouped; | ||
} | ||
|
||
public Map<Id, List<SObject>> groupByIds(Schema.SObjectField field) { | ||
return groupByIds(field, List<SObject>.class); | ||
} | ||
|
||
public Map<String, List<SObject>> groupByStrings(Schema.SObjectField field, Type listType) { | ||
Map<String, List<SObject>> grouped = new Map<String, List<SObject>>(); | ||
for (SObject rec : records) { | ||
String key = (String) rec.get(field); | ||
if (!grouped.containsKey(key)) { | ||
grouped.put(key, (List<SObject>) listType.newInstance()); | ||
} | ||
grouped.get(key).add(rec); | ||
} | ||
return grouped; | ||
} | ||
|
||
public Map<String, List<SObject>> groupByStrings(Schema.SObjectField field) { | ||
return groupByStrings(field, List<SObject>.class); | ||
} | ||
|
||
public Collection pick(List<Schema.SObjectField> fields) { | ||
return pick(new Set<Schema.SObjectField>(fields)); | ||
} | ||
|
||
public Collection pick(Set<Schema.SObjectField> fields) { | ||
Set<String> fieldNames = new Set<String>(); | ||
for (Schema.SObjectField field : fields) { | ||
Schema.DescribeFieldResult describe = field.getDescribe(); | ||
fieldNames.add(describe.getName()); | ||
} | ||
return pick(fieldNames); | ||
} | ||
|
||
public Collection pick(Set<String> apiFieldNames) { | ||
List<SObject> picked = new List<SObject>(); | ||
for (SObject record : records) { | ||
SObject result = record.getSObjectType().newSObject(); | ||
Map<String, Object> fieldMap = record.getPopulatedFieldsAsMap(); | ||
for (String fieldName : apiFieldNames) { | ||
if (fieldMap.containsKey(fieldName)) { | ||
result.put(fieldName, record.get(fieldName)); | ||
} | ||
} | ||
picked.add(result); | ||
} | ||
return Collection.of(picked); | ||
} | ||
|
||
public Collection pick(List<String> apiFieldNames) { | ||
return pick(new Set<String>(apiFieldNames)); | ||
} | ||
|
||
public Collection mapAll(SObjectToSObjectFunction fn) { | ||
List<SObject> mapped = new List<SObject>(); | ||
for (SObject record : records) { | ||
mapped.add(fn.apply(record)); | ||
} | ||
return Collection.of(mapped); | ||
} | ||
|
||
public Collection mapSome(SObjectPredicate predicate, SObjectToSObjectFunction fn) { | ||
List<SObject> transformed = new List<SObject>(); | ||
for (SObject record : records) { | ||
if (predicate.apply(record)) { | ||
transformed.add(fn.apply(record)); | ||
} else { | ||
transformed.add(record); | ||
} | ||
} | ||
return Collection.of(transformed); | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
src/classes/Filter.cls-meta.xml → src/classes/Collection.cls-meta.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata"> | ||
<apiVersion>28.0</apiVersion> | ||
<apiVersion>44.0</apiVersion> | ||
<status>Active</status> | ||
</ApexClass> |
Oops, something went wrong.