diff --git a/sfdx-source/apex-fp/main/classes/collection/DecimalCollection.cls b/sfdx-source/apex-fp/main/classes/collection/DecimalCollection.cls index fa71db1..54b1a58 100644 --- a/sfdx-source/apex-fp/main/classes/collection/DecimalCollection.cls +++ b/sfdx-source/apex-fp/main/classes/collection/DecimalCollection.cls @@ -2,10 +2,17 @@ public class DecimalCollection { private List decimals; private List nonNulls; - public DecimalCollection(List decimals) { - this.decimals = decimals; + public static DecimalCollection of(Iterable decimals) { + return new DecimalCollection(decimals); + } + + private DecimalCollection(Iterable decimals) { + this.decimals = new List(); nonNulls = new List(); - for (Decimal d : decimals) { + Iterator iter = decimals.iterator(); + while (iter.hasNext()) { + Decimal d = iter.next(); + this.decimals.add(d); if (d != null) { nonNulls.add(d); } diff --git a/sfdx-source/apex-fp/main/classes/collection/DoubleCollection.cls b/sfdx-source/apex-fp/main/classes/collection/DoubleCollection.cls index 1467750..c2bcea2 100644 --- a/sfdx-source/apex-fp/main/classes/collection/DoubleCollection.cls +++ b/sfdx-source/apex-fp/main/classes/collection/DoubleCollection.cls @@ -2,10 +2,17 @@ public class DoubleCollection { private List doubles; private List nonNulls; - public DoubleCollection(List doubles) { - this.doubles = doubles; + public static DoubleCollection of(Iterable doubles) { + return new DoubleCollection(doubles); + } + + private DoubleCollection(Iterable doubles) { + this.doubles = new List(); nonNulls = new List(); - for (Double d : doubles) { + Iterator iter = doubles.iterator(); + while (iter.hasNext()) { + Double d = iter.next(); + this.doubles.add(d); if (d != null) { nonNulls.add(d); } diff --git a/sfdx-source/apex-fp/main/classes/collection/ObjectCollection.cls b/sfdx-source/apex-fp/main/classes/collection/ObjectCollection.cls deleted file mode 100644 index 3a3a7a0..0000000 --- a/sfdx-source/apex-fp/main/classes/collection/ObjectCollection.cls +++ /dev/null @@ -1,19 +0,0 @@ -public with sharing class ObjectCollection { - private List objects; - - public ObjectCollection(List objects) { - this.objects = objects; - } - - public List asList(Type listType) { - List typedObjects = (List) listType.newInstance(); - typedObjects.addAll(objects); - return typedObjects; - } - - public Set asSet(Type setType) { - Set typedObjects = (Set) setType.newInstance(); - typedObjects.addAll(objects); - return typedObjects; - } -} diff --git a/sfdx-source/apex-fp/main/classes/collection/ObjectCollection.cls-meta.xml b/sfdx-source/apex-fp/main/classes/collection/ObjectCollection.cls-meta.xml deleted file mode 100644 index fbbad0a..0000000 --- a/sfdx-source/apex-fp/main/classes/collection/ObjectCollection.cls-meta.xml +++ /dev/null @@ -1,5 +0,0 @@ - - - 56.0 - Active - diff --git a/sfdx-source/apex-fp/test/classes/collection/DecimalCollectionTest.cls b/sfdx-source/apex-fp/test/classes/collection/DecimalCollectionTest.cls index 121f1ac..eeadea6 100644 --- a/sfdx-source/apex-fp/test/classes/collection/DecimalCollectionTest.cls +++ b/sfdx-source/apex-fp/test/classes/collection/DecimalCollectionTest.cls @@ -2,43 +2,43 @@ private class DecimalCollectionTest { @IsTest private static void testDecimalCollectionSum() { - DecimalCollection c = new DecimalCollection(new List{100, 150}); + DecimalCollection c = DecimalCollection.of(new List{100, 150}); System.Assert.areEqual(250, c.sum().get()); } @IsTest private static void maxShouldReturnAMaxIfItExists() { - DecimalCollection c = new DecimalCollection(new List{100, 150, 120}); + DecimalCollection c = DecimalCollection.of(new List{100, 150, 120}); System.Assert.areEqual(150, c.max().get()); } @IsTest private static void maxShouldReturnEmptyIfItDoesNotExist() { - DecimalCollection c = new DecimalCollection(new List{null, null, null}); + DecimalCollection c = DecimalCollection.of(new List{null, null, null}); System.Assert.areEqual(OptionalDecimal.empty(), c.max()); } @IsTest private static void minShouldReturnAMinIfItExists() { - DecimalCollection c = new DecimalCollection(new List{70, 80, 150, 120}); + DecimalCollection c = DecimalCollection.of(new List{70, 80, 150, 120}); System.Assert.areEqual(70, c.min().get()); } @IsTest private static void minShouldReturnEmptyIfItDoesNotExist() { - DecimalCollection c = new DecimalCollection(new List{null, null, null}); + DecimalCollection c = DecimalCollection.of(new List{null, null, null}); System.Assert.areEqual(OptionalDecimal.empty(), c.min()); } @IsTest private static void testDecimalCollectionAverage() { - DecimalCollection c = new DecimalCollection(new List{100, 150}); + DecimalCollection c = DecimalCollection.of(new List{100, 150}); System.Assert.areEqual(125, c.average(0).get()); } @IsTest private static void testDecimalCollectionAverageWithRoundingMode() { - DecimalCollection c = new DecimalCollection(new List{1, 2}); + DecimalCollection c = DecimalCollection.of(new List{1, 2}); System.Assert.areEqual(1, c.average(0, System.RoundingMode.DOWN).get()); } @@ -50,7 +50,7 @@ private class DecimalCollectionTest { @IsTest private static void decimalPredicateShouldFilterNumbers() { - DecimalCollection c = new DecimalCollection(new List{null, 10, null, 20, null, 10}); + DecimalCollection c = DecimalCollection.of(new List{null, 10, null, 20, null, 10}); List filtered = c.filter(new IsTen()).asList(); System.Assert.areEqual(2, filtered.size()); System.Assert.areEqual(10, filtered[0]); @@ -59,14 +59,14 @@ private class DecimalCollectionTest { @IsTest private static void nullsShouldBeSummedToEmpty() { - DecimalCollection c = new DecimalCollection(new List{null, null}); + DecimalCollection c = DecimalCollection.of(new List{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{null, 1, 1, null, 2}); + DecimalCollection c = DecimalCollection.of(new List{null, 1, 1, null, 2}); Set s = c.asSet(); System.Assert.areEqual(3, s.size(), 'Set has more than 3 elements'); System.Assert.isTrue(s.contains(1)); diff --git a/sfdx-source/apex-fp/test/classes/collection/DoubleCollectionTest.cls b/sfdx-source/apex-fp/test/classes/collection/DoubleCollectionTest.cls index 72620c4..33dafdf 100644 --- a/sfdx-source/apex-fp/test/classes/collection/DoubleCollectionTest.cls +++ b/sfdx-source/apex-fp/test/classes/collection/DoubleCollectionTest.cls @@ -2,37 +2,37 @@ private class DoubleCollectionTest { @IsTest private static void testSum() { - DoubleCollection c = new DoubleCollection(new List{100, 150}); + DoubleCollection c = DoubleCollection.of(new List{100, 150}); System.Assert.areEqual(250, c.sum().get()); } @IsTest private static void maxShouldReturnAMaxIfItExists() { - DoubleCollection c = new DoubleCollection(new List{100, 150, 120}); + DoubleCollection c = DoubleCollection.of(new List{100, 150, 120}); System.Assert.areEqual(150, c.max().get()); } @IsTest private static void maxShouldReturnEmptyIfItDoesNotExist() { - DoubleCollection c = new DoubleCollection(new List{null, null, null}); + DoubleCollection c = DoubleCollection.of(new List{null, null, null}); System.Assert.areEqual(OptionalDouble.empty(), c.max()); } @IsTest private static void minShouldReturnAMinIfItExists() { - DoubleCollection c = new DoubleCollection(new List{70, 80, 150, 120}); + DoubleCollection c = DoubleCollection.of(new List{70, 80, 150, 120}); System.Assert.areEqual(70, c.min().get()); } @IsTest private static void minShouldReturnEmptyIfItDoesNotExist() { - DoubleCollection c = new DoubleCollection(new List{null, null, null}); + DoubleCollection c = DoubleCollection.of(new List{null, null, null}); System.Assert.areEqual(OptionalDouble.empty(), c.min()); } @IsTest private static void testAverage() { - DoubleCollection c = new DoubleCollection(new List{100, 150}); + DoubleCollection c = DoubleCollection.of(new List{100, 150}); System.Assert.areEqual(125, c.average().get()); } @@ -44,7 +44,7 @@ private class DoubleCollectionTest { @IsTest private static void doublePredicateShouldFilterNumbers() { - DoubleCollection c = new DoubleCollection(new List{null, 20, 5, 23}); + DoubleCollection c = DoubleCollection.of(new List{null, 20, 5, 23}); List filtered = c.filter(new IsGreaterThanTen()).asList(); System.Assert.areEqual(2, filtered.size()); System.Assert.areEqual(20, filtered[0]); @@ -53,14 +53,14 @@ private class DoubleCollectionTest { @IsTest private static void nullsShouldBeSummedToEmpty() { - DoubleCollection c = new DoubleCollection(new List{null, null}); + DoubleCollection c = DoubleCollection.of(new List{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{null, 1, 1, null, 2}); + DoubleCollection c = DoubleCollection.of(new List{null, 1, 1, null, 2}); Set s = c.asSet(); System.Assert.areEqual(3, s.size(), 'Set has more than 3 elements'); System.Assert.isTrue(s.contains(1));