-
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.
Remove reduceType in favour of mapToType, similar to Java Stream. Ren…
…ame Relation to BinaryRelation, to reduce the chance of confusion with SObject relations. Collection's `mapToDouble` and `mapToDecimal` return DoubleCollection and DecimalCollection, which offer further functions like `sum`, `average` and `filter`.
- Loading branch information
Showing
35 changed files
with
887 additions
and
82 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
Binary file not shown.
Binary file not shown.
Large diffs are not rendered by default.
Oops, something went wrong.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Large diffs are not rendered by default.
Oops, something went wrong.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,3 +1,3 @@ | ||
public enum Relation { | ||
public enum BinaryRelation { | ||
LESS_THAN, GREATER_THAN, EQUALS, NOT_EQUALS, LESS_THAN_OR_EQUALS, GREATER_THAN_OR_EQUALS, IS_IN, NOT_IN | ||
} |
File renamed without changes.
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
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
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,58 @@ | ||
public class DecimalCollection { | ||
private List<Decimal> decimals; | ||
private List<Decimal> nonNulls; | ||
|
||
public DecimalCollection(List<Decimal> decimals) { | ||
this.decimals = decimals; | ||
nonNulls = filter(ObjectPredicates.NotNull).asList(); | ||
} | ||
|
||
public List<Decimal> asList() { | ||
return new List<Decimal>(decimals); | ||
} | ||
|
||
public Set<Decimal> asSet() { | ||
return new Set<Decimal>(decimals); | ||
} | ||
|
||
public Decimal sum() { | ||
if (nonNulls.isEmpty()) { | ||
return null; | ||
} | ||
Decimal sum = 0; | ||
for (Decimal d : nonNulls) { | ||
sum += d; | ||
} | ||
return sum; | ||
} | ||
|
||
public Decimal average(Integer scale) { | ||
Decimal s = sum(); | ||
return s != null ? s.divide(nonNulls.size(), scale) : null; | ||
} | ||
|
||
public Decimal average(Integer scale, System.RoundingMode roundingMode) { | ||
Decimal s = sum(); | ||
return s != null ? s.divide(nonNulls.size(), scale, roundingMode) : null; | ||
} | ||
|
||
public DecimalCollection filter(ObjectPredicate predicate) { | ||
List<Decimal> filtered = new List<Decimal>(); | ||
for (Decimal d : decimals) { | ||
if (predicate.apply(d)) { | ||
filtered.add(d); | ||
} | ||
} | ||
return new DecimalCollection(filtered); | ||
} | ||
|
||
public DecimalCollection filter(DecimalPredicate predicate) { | ||
List<Decimal> filtered = new List<Decimal>(); | ||
for (Decimal d : decimals) { | ||
if (predicate.apply(d)) { | ||
filtered.add(d); | ||
} | ||
} | ||
return new DecimalCollection(filtered); | ||
} | ||
} |
File renamed without changes.
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,44 @@ | ||
@IsTest | ||
public class DecimalCollectionTest { | ||
@IsTest | ||
static void testDecimalCollectionSum() { | ||
DecimalCollection c = new DecimalCollection(new List<Decimal>{100, 150}); | ||
System.assertEquals(250, c.sum()); | ||
} | ||
|
||
@IsTest | ||
static void testDecimalCollectionAverage() { | ||
DecimalCollection c = new DecimalCollection(new List<Decimal>{100, 150}); | ||
System.assertEquals(125, c.average(0)); | ||
} | ||
|
||
@IsTest | ||
static void testDecimalCollectionAverageWithRoundingMode() { | ||
DecimalCollection c = new DecimalCollection(new List<Decimal>{1, 2}); | ||
System.assertEquals(1, c.average(0, System.RoundingMode.DOWN)); | ||
} | ||
|
||
@IsTest | ||
static void testFilterWithObjectPredicate() { | ||
DecimalCollection c = new DecimalCollection(new List<Decimal>{null, 100, null, 150, null}); | ||
List<Decimal> filtered = c.filter(ObjectPredicates.NotNull).asList(); | ||
System.assertEquals(2, filtered.size()); | ||
System.assertEquals(100, filtered[0]); | ||
System.assertEquals(150, filtered[1]); | ||
} | ||
|
||
private class LowPassPredicate implements DecimalPredicate { | ||
public Boolean apply(Decimal d) { | ||
return d < 100; | ||
} | ||
} | ||
|
||
@IsTest | ||
static void testFilterWithDecimalPredicate() { | ||
DecimalCollection c = new DecimalCollection(new List<Decimal>{50, 101, 500, 25, 300}); | ||
List<Decimal> filtered = c.filter(new LowPassPredicate()).asList(); | ||
System.assertEquals(2, filtered.size()); | ||
System.assertEquals(50, filtered[0]); | ||
System.assertEquals(25, filtered[1]); | ||
} | ||
} |
File renamed without changes.
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,3 @@ | ||
public interface DecimalPredicate { | ||
Boolean apply(Decimal d); | ||
} |
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,5 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata"> | ||
<apiVersion>44.0</apiVersion> | ||
<status>Active</status> | ||
</ApexClass> |
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,58 @@ | ||
public class DoubleCollection { | ||
private List<Double> doubles; | ||
private List<Double> nonNulls; | ||
|
||
public DoubleCollection(List<Double> doubles) { | ||
this.doubles = doubles; | ||
nonNulls = filter(ObjectPredicates.NotNull).asList(); | ||
} | ||
|
||
public List<Double> asList() { | ||
return new List<Double>(doubles); | ||
} | ||
|
||
public Set<Double> asSet() { | ||
return new Set<Double>(doubles); | ||
} | ||
|
||
public Double sum() { | ||
if (nonNulls.isEmpty()) { | ||
return null; | ||
} | ||
Double sum = 0; | ||
for (Double d : nonNulls) { | ||
sum += d; | ||
} | ||
return sum; | ||
} | ||
|
||
public Double average() { | ||
Double s = sum(); | ||
return s != null ? s / nonNulls.size() : null; | ||
} | ||
|
||
public Double average(Integer scale, System.RoundingMode roundingMode) { | ||
Double s = sum(); | ||
return s != null ? s / nonNulls.size() : null; | ||
} | ||
|
||
public DoubleCollection filter(ObjectPredicate predicate) { | ||
List<Double> filtered = new List<Decimal>(); | ||
for (Double d : doubles) { | ||
if (predicate.apply(d)) { | ||
filtered.add(d); | ||
} | ||
} | ||
return new DoubleCollection(filtered); | ||
} | ||
|
||
public DoubleCollection filter(DoublePredicate predicate) { | ||
List<Double> filtered = new List<Decimal>(); | ||
for (Double d : doubles) { | ||
if (predicate.apply(d)) { | ||
filtered.add(d); | ||
} | ||
} | ||
return new DoubleCollection(filtered); | ||
} | ||
} |
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,5 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata"> | ||
<apiVersion>44.0</apiVersion> | ||
<status>Active</status> | ||
</ApexClass> |
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,38 @@ | ||
@IsTest | ||
public class DoubleCollectionTest { | ||
@IsTest | ||
static void testSum() { | ||
DoubleCollection c = new DoubleCollection(new List<Double>{100, 150}); | ||
System.assertEquals(250, c.sum()); | ||
} | ||
|
||
@IsTest | ||
static void testAverage() { | ||
DoubleCollection c = new DoubleCollection(new List<Double>{100, 150}); | ||
System.assertEquals(125, c.average()); | ||
} | ||
|
||
@IsTest | ||
static void testFilterWithObjectPredicate() { | ||
DoubleCollection c = new DoubleCollection(new List<Double>{null, 100, null, 150, null}); | ||
List<Double> filtered = c.filter(ObjectPredicates.NotNull).asList(); | ||
System.assertEquals(2, filtered.size()); | ||
System.assertEquals(100, filtered[0]); | ||
System.assertEquals(150, filtered[1]); | ||
} | ||
|
||
private class LowPassPredicate implements DoublePredicate { | ||
public Boolean apply(Double d) { | ||
return d < 100; | ||
} | ||
} | ||
|
||
@IsTest | ||
static void testFilterWithDoublePredicate() { | ||
DoubleCollection c = new DoubleCollection(new List<Double>{50, 101, 500, 25, 300}); | ||
List<Double> filtered = c.filter(new LowPassPredicate()).asList(); | ||
System.assertEquals(2, filtered.size()); | ||
System.assertEquals(50, filtered[0]); | ||
System.assertEquals(25, filtered[1]); | ||
} | ||
} |
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,5 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata"> | ||
<apiVersion>44.0</apiVersion> | ||
<status>Active</status> | ||
</ApexClass> |
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,3 @@ | ||
public interface DoublePredicate { | ||
Boolean apply(Double d); | ||
} |
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,5 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata"> | ||
<apiVersion>44.0</apiVersion> | ||
<status>Active</status> | ||
</ApexClass> |
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
Oops, something went wrong.