Skip to content

Commit

Permalink
Match SObjectCollection.of building interface for DecimalCollection a…
Browse files Browse the repository at this point in the history
…nd DoubleCollection. Remove ObjectCollection
  • Loading branch information
ipavlic committed Sep 28, 2023
1 parent 4c85c38 commit f4cda68
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 49 deletions.
13 changes: 10 additions & 3 deletions sfdx-source/apex-fp/main/classes/collection/DecimalCollection.cls
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,17 @@ public class DecimalCollection {
private List<Decimal> decimals;
private List<Decimal> nonNulls;

public DecimalCollection(List<Decimal> decimals) {
this.decimals = decimals;
public static DecimalCollection of(Iterable<Decimal> decimals) {
return new DecimalCollection(decimals);
}

private DecimalCollection(Iterable<Decimal> decimals) {
this.decimals = new List<Decimal>();
nonNulls = new List<Decimal>();
for (Decimal d : decimals) {
Iterator<Decimal> iter = decimals.iterator();
while (iter.hasNext()) {
Decimal d = iter.next();
this.decimals.add(d);
if (d != null) {
nonNulls.add(d);
}
Expand Down
13 changes: 10 additions & 3 deletions sfdx-source/apex-fp/main/classes/collection/DoubleCollection.cls
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,17 @@ public class DoubleCollection {
private List<Double> doubles;
private List<Double> nonNulls;

public DoubleCollection(List<Double> doubles) {
this.doubles = doubles;
public static DoubleCollection of(Iterable<Double> doubles) {
return new DoubleCollection(doubles);
}

private DoubleCollection(Iterable<Double> doubles) {
this.doubles = new List<Double>();
nonNulls = new List<Double>();
for (Double d : doubles) {
Iterator<Double> iter = doubles.iterator();
while (iter.hasNext()) {
Double d = iter.next();
this.doubles.add(d);
if (d != null) {
nonNulls.add(d);
}
Expand Down
19 changes: 0 additions & 19 deletions sfdx-source/apex-fp/main/classes/collection/ObjectCollection.cls

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,43 @@
private class DecimalCollectionTest {
@IsTest
private static void testDecimalCollectionSum() {
DecimalCollection c = new DecimalCollection(new List<Decimal>{100, 150});
DecimalCollection c = DecimalCollection.of(new List<Decimal>{100, 150});
System.Assert.areEqual(250, c.sum().get());
}

@IsTest
private static void maxShouldReturnAMaxIfItExists() {
DecimalCollection c = new DecimalCollection(new List<Decimal>{100, 150, 120});
DecimalCollection c = DecimalCollection.of(new List<Decimal>{100, 150, 120});
System.Assert.areEqual(150, c.max().get());
}

@IsTest
private static void maxShouldReturnEmptyIfItDoesNotExist() {
DecimalCollection c = new DecimalCollection(new List<Decimal>{null, null, null});
DecimalCollection c = DecimalCollection.of(new List<Decimal>{null, null, null});
System.Assert.areEqual(OptionalDecimal.empty(), c.max());
}

@IsTest
private static void minShouldReturnAMinIfItExists() {
DecimalCollection c = new DecimalCollection(new List<Decimal>{70, 80, 150, 120});
DecimalCollection c = DecimalCollection.of(new List<Decimal>{70, 80, 150, 120});
System.Assert.areEqual(70, c.min().get());
}

@IsTest
private static void minShouldReturnEmptyIfItDoesNotExist() {
DecimalCollection c = new DecimalCollection(new List<Decimal>{null, null, null});
DecimalCollection c = DecimalCollection.of(new List<Decimal>{null, null, null});
System.Assert.areEqual(OptionalDecimal.empty(), c.min());
}

@IsTest
private static void testDecimalCollectionAverage() {
DecimalCollection c = new DecimalCollection(new List<Decimal>{100, 150});
DecimalCollection c = DecimalCollection.of(new List<Decimal>{100, 150});
System.Assert.areEqual(125, c.average(0).get());
}

@IsTest
private static void testDecimalCollectionAverageWithRoundingMode() {
DecimalCollection c = new DecimalCollection(new List<Decimal>{1, 2});
DecimalCollection c = DecimalCollection.of(new List<Decimal>{1, 2});
System.Assert.areEqual(1, c.average(0, System.RoundingMode.DOWN).get());
}

Expand All @@ -50,7 +50,7 @@ private class DecimalCollectionTest {

@IsTest
private static void decimalPredicateShouldFilterNumbers() {
DecimalCollection c = new DecimalCollection(new List<Decimal>{null, 10, null, 20, null, 10});
DecimalCollection c = DecimalCollection.of(new List<Decimal>{null, 10, null, 20, null, 10});
List<Decimal> filtered = c.filter(new IsTen()).asList();
System.Assert.areEqual(2, filtered.size());
System.Assert.areEqual(10, filtered[0]);
Expand All @@ -59,14 +59,14 @@ private class DecimalCollectionTest {

@IsTest
private static void nullsShouldBeSummedToEmpty() {
DecimalCollection c = new DecimalCollection(new List<Decimal>{null, null});
DecimalCollection c = DecimalCollection.of(new List<Decimal>{null, null});
OptionalDecimal d = c.sum();
System.Assert.isFalse(d.isPresent(), 'Result should be empty');
}

@IsTest
private static void asSetShouldReturnASet() {
DecimalCollection c = new DecimalCollection(new List<Decimal>{null, 1, 1, null, 2});
DecimalCollection c = DecimalCollection.of(new List<Decimal>{null, 1, 1, null, 2});
Set<Decimal> s = c.asSet();
System.Assert.areEqual(3, s.size(), 'Set has more than 3 elements');
System.Assert.isTrue(s.contains(1));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,37 @@
private class DoubleCollectionTest {
@IsTest
private static void testSum() {
DoubleCollection c = new DoubleCollection(new List<Double>{100, 150});
DoubleCollection c = DoubleCollection.of(new List<Double>{100, 150});
System.Assert.areEqual(250, c.sum().get());
}

@IsTest
private static void maxShouldReturnAMaxIfItExists() {
DoubleCollection c = new DoubleCollection(new List<Decimal>{100, 150, 120});
DoubleCollection c = DoubleCollection.of(new List<Double>{100, 150, 120});
System.Assert.areEqual(150, c.max().get());
}

@IsTest
private static void maxShouldReturnEmptyIfItDoesNotExist() {
DoubleCollection c = new DoubleCollection(new List<Decimal>{null, null, null});
DoubleCollection c = DoubleCollection.of(new List<Double>{null, null, null});
System.Assert.areEqual(OptionalDouble.empty(), c.max());
}

@IsTest
private static void minShouldReturnAMinIfItExists() {
DoubleCollection c = new DoubleCollection(new List<Decimal>{70, 80, 150, 120});
DoubleCollection c = DoubleCollection.of(new List<Double>{70, 80, 150, 120});
System.Assert.areEqual(70, c.min().get());
}

@IsTest
private static void minShouldReturnEmptyIfItDoesNotExist() {
DoubleCollection c = new DoubleCollection(new List<Decimal>{null, null, null});
DoubleCollection c = DoubleCollection.of(new List<Double>{null, null, null});
System.Assert.areEqual(OptionalDouble.empty(), c.min());
}

@IsTest
private static void testAverage() {
DoubleCollection c = new DoubleCollection(new List<Double>{100, 150});
DoubleCollection c = DoubleCollection.of(new List<Double>{100, 150});
System.Assert.areEqual(125, c.average().get());
}

Expand All @@ -44,7 +44,7 @@ private class DoubleCollectionTest {

@IsTest
private static void doublePredicateShouldFilterNumbers() {
DoubleCollection c = new DoubleCollection(new List<Double>{null, 20, 5, 23});
DoubleCollection c = DoubleCollection.of(new List<Double>{null, 20, 5, 23});
List<Double> filtered = c.filter(new IsGreaterThanTen()).asList();
System.Assert.areEqual(2, filtered.size());
System.Assert.areEqual(20, filtered[0]);
Expand All @@ -53,14 +53,14 @@ private class DoubleCollectionTest {

@IsTest
private static void nullsShouldBeSummedToEmpty() {
DoubleCollection c = new DoubleCollection(new List<Double>{null, null});
DoubleCollection c = DoubleCollection.of(new List<Double>{null, null});
OptionalDouble d = c.sum();
System.Assert.isFalse(d.isPresent(), 'Result should be empty');
}

@IsTest
private static void asSetShouldReturnASet() {
DoubleCollection c = new DoubleCollection(new List<Double>{null, 1, 1, null, 2});
DoubleCollection c = DoubleCollection.of(new List<Double>{null, 1, 1, null, 2});
Set<Double> s = c.asSet();
System.Assert.areEqual(3, s.size(), 'Set has more than 3 elements');
System.Assert.isTrue(s.contains(1));
Expand Down

0 comments on commit f4cda68

Please sign in to comment.