From e879ff02ddc0b6656625229635f41101411de24c Mon Sep 17 00:00:00 2001 From: James Simone <16430727+jamessimone@users.noreply.github.com> Date: Wed, 3 Jul 2024 11:34:10 -0400 Subject: [PATCH] Refactors DecimalCollection and DoubleCollection, as well as OptionalDecimal/OptionalDouble to be a bit more concise by taking advantage of the new(er) null coalesce operator --- .../classes/collection/DecimalCollection.cls | 71 +++++++++---------- .../classes/collection/DoubleCollection.cls | 68 +++++++++--------- .../collection/DecimalCollectionTest.cls | 8 +-- .../collection/DoubleCollectionTest.cls | 4 +- .../default/classes/util/OptionalDecimal.cls | 23 +++--- .../default/classes/util/OptionalDouble.cls | 24 ++++--- 6 files changed, 103 insertions(+), 95 deletions(-) diff --git a/force-app/main/default/classes/collection/DecimalCollection.cls b/force-app/main/default/classes/collection/DecimalCollection.cls index 54b1a58..0908c6a 100644 --- a/force-app/main/default/classes/collection/DecimalCollection.cls +++ b/force-app/main/default/classes/collection/DecimalCollection.cls @@ -1,6 +1,21 @@ public class DecimalCollection { - private List decimals; - private List nonNulls; + private static final Decimal MAX_DECIMAL_VALUE { + get { + MAX_DECIMAL_VALUE = MAX_DECIMAL_VALUE ?? Math.pow(2, 63) - 1; + return MAX_DECIMAL_VALUE; + } + set; + } + + private static final Decimal MIN_DECIMAL_VALUE { + get { + MIN_DECIMAL_VALUE = MIN_DECIMAL_VALUE ?? MAX_DECIMAL_VALUE * -1; + return MIN_DECIMAL_VALUE; + } + set; + } + + private final List decimals; public static DecimalCollection of(Iterable decimals) { return new DecimalCollection(decimals); @@ -8,44 +23,33 @@ public class DecimalCollection { private DecimalCollection(Iterable decimals) { this.decimals = new List(); - nonNulls = new List(); - Iterator iter = decimals.iterator(); - while (iter.hasNext()) { - Decimal d = iter.next(); + for (Decimal d : decimals) { this.decimals.add(d); - if (d != null) { - nonNulls.add(d); - } } } public List asList() { - return new List(decimals); + return new List(this.decimals); } public Set asSet() { - return new Set(decimals); + return new Set(this.decimals); } public OptionalDecimal sum() { - if (nonNulls.isEmpty()) { - return OptionalDecimal.empty(); - } - Decimal sum = 0; - for (Decimal d : nonNulls) { - sum += d; + Decimal sum; + for (Decimal d : this.decimals) { + if (d != null) { + sum = (sum ?? 0) + (d ?? 0); + } } return OptionalDecimal.of(sum); } public OptionalDecimal max() { - if (nonNulls.isEmpty()) { - return OptionalDecimal.empty(); - } - Decimal max = nonNulls.get(0); - for (Integer i = 1; i < nonNulls.size(); i++) { - Decimal d = nonNulls[i]; - if (d > max) { + Decimal max; + for (Decimal d : this.decimals) { + if ((d ?? MIN_DECIMAL_VALUE) > (max ?? MIN_DECIMAL_VALUE)) { max = d; } } @@ -53,13 +57,9 @@ public class DecimalCollection { } public OptionalDecimal min() { - if (nonNulls.isEmpty()) { - return OptionalDecimal.empty(); - } - Decimal min = nonNulls.get(0); - for (Integer i = 1; i < nonNulls.size(); i++) { - Decimal d = nonNulls[i]; - if (d < min) { + Decimal min ; + for (Decimal d : this.decimals) { + if ((d ?? MAX_DECIMAL_VALUE) < (min ?? MAX_DECIMAL_VALUE)) { min = d; } } @@ -67,18 +67,17 @@ public class DecimalCollection { } public OptionalDecimal average(Integer scale) { - OptionalDecimal s = sum(); - return s.isPresent() ? OptionalDecimal.of(s.get().divide(nonNulls.size(), scale)) : OptionalDecimal.empty(); + return this.average(scale, System.RoundingMode.HALF_EVEN); } public OptionalDecimal average(Integer scale, System.RoundingMode roundingMode) { - OptionalDecimal s = sum(); - return s.isPresent() ? OptionalDecimal.of(s.get().divide(nonNulls.size(), scale, roundingMode)) : OptionalDecimal.empty(); + OptionalDecimal s = this.sum(); + return s.isPresent() ? OptionalDecimal.of(s.get().divide(this.decimals.size(), scale, roundingMode)) : s; } public DecimalCollection filter(DecimalPredicate predicate) { List filtered = new List(); - for (Decimal d : decimals) { + for (Decimal d : this.decimals) { if (predicate.call(d)) { filtered.add(d); } diff --git a/force-app/main/default/classes/collection/DoubleCollection.cls b/force-app/main/default/classes/collection/DoubleCollection.cls index c2bcea2..8c0db21 100644 --- a/force-app/main/default/classes/collection/DoubleCollection.cls +++ b/force-app/main/default/classes/collection/DoubleCollection.cls @@ -1,6 +1,21 @@ public class DoubleCollection { - private List doubles; - private List nonNulls; + private static final Double MAX_DOUBLE_VALUE { + get { + MAX_DOUBLE_VALUE = MAX_DOUBLE_VALUE ?? Math.pow(2, 63) - 1; + return MAX_DOUBLE_VALUE; + } + set; + } + + private static final Double MIN_DOUBLE_VALUE { + get { + MIN_DOUBLE_VALUE = MIN_DOUBLE_VALUE ?? MAX_DOUBLE_VALUE * -1; + return MIN_DOUBLE_VALUE; + } + set; + } + + private final List doubles; public static DoubleCollection of(Iterable doubles) { return new DoubleCollection(doubles); @@ -8,44 +23,33 @@ public class DoubleCollection { private DoubleCollection(Iterable doubles) { this.doubles = new List(); - nonNulls = new List(); - Iterator iter = doubles.iterator(); - while (iter.hasNext()) { - Double d = iter.next(); + for (Double d : doubles) { this.doubles.add(d); - if (d != null) { - nonNulls.add(d); - } } } public List asList() { - return new List(doubles); + return new List(this.doubles); } public Set asSet() { - return new Set(doubles); + return new Set(this.doubles); } public OptionalDouble sum() { - if (nonNulls.isEmpty()) { - return OptionalDouble.empty(); - } - Double sum = 0; - for (Double d : nonNulls) { - sum += d; + Double sum; + for (Double d : this.doubles) { + if (d != null) { + sum = (sum ?? 0) + (d ?? 0); + } } return OptionalDouble.of(sum); } public OptionalDouble max() { - if (nonNulls.isEmpty()) { - return OptionalDouble.empty(); - } - Double max = nonNulls.get(0); - for (Integer i = 1; i < nonNulls.size(); i++) { - Double d = nonNulls[i]; - if (d > max) { + Double max; + for (Double d : this.doubles) { + if ((d ?? MIN_DOUBLE_VALUE) > (max ?? MIN_DOUBLE_VALUE)) { max = d; } } @@ -53,13 +57,9 @@ public class DoubleCollection { } public OptionalDouble min() { - if (nonNulls.isEmpty()) { - return OptionalDouble.empty(); - } - Double min = nonNulls.get(0); - for (Integer i = 1; i < nonNulls.size(); i++) { - Double d = nonNulls[i]; - if (d < min) { + Double min ; + for (Double d : this.doubles) { + if ((d ?? MAX_DOUBLE_VALUE) < (min ?? MAX_DOUBLE_VALUE)) { min = d; } } @@ -67,13 +67,13 @@ public class DoubleCollection { } public OptionalDouble average() { - OptionalDouble s = sum(); - return s.isPresent() ? OptionalDouble.of(s.get() / nonNulls.size()) : OptionalDouble.empty(); + OptionalDouble s = this.sum(); + return s.isPresent() ? OptionalDouble.of(s.get() / this.doubles.size()) : s; } public DoubleCollection filter(DoublePredicate predicate) { List filtered = new List(); - for (Double d : doubles) { + for (Double d : this.doubles) { if (predicate.call(d)) { filtered.add(d); } diff --git a/force-app/main/default/classes/test/classes/collection/DecimalCollectionTest.cls b/force-app/main/default/classes/test/classes/collection/DecimalCollectionTest.cls index eeadea6..4dcb6e8 100644 --- a/force-app/main/default/classes/test/classes/collection/DecimalCollectionTest.cls +++ b/force-app/main/default/classes/test/classes/collection/DecimalCollectionTest.cls @@ -1,7 +1,7 @@ @IsTest(isParallel=true) private class DecimalCollectionTest { @IsTest - private static void testDecimalCollectionSum() { + private static void sumShouldReturnSumIfItExists() { DecimalCollection c = DecimalCollection.of(new List{100, 150}); System.Assert.areEqual(250, c.sum().get()); } @@ -31,20 +31,20 @@ private class DecimalCollectionTest { } @IsTest - private static void testDecimalCollectionAverage() { + private static void averageShouldReturnAverageIfExists() { DecimalCollection c = DecimalCollection.of(new List{100, 150}); System.Assert.areEqual(125, c.average(0).get()); } @IsTest - private static void testDecimalCollectionAverageWithRoundingMode() { + private static void averageShouldReturnAverageIfExistsWithRoundingMode() { DecimalCollection c = DecimalCollection.of(new List{1, 2}); System.Assert.areEqual(1, c.average(0, System.RoundingMode.DOWN).get()); } private class IsTen implements DecimalPredicate { public Boolean call(Decimal d) { - return d != null && d == 10; + return d == 10; } } diff --git a/force-app/main/default/classes/test/classes/collection/DoubleCollectionTest.cls b/force-app/main/default/classes/test/classes/collection/DoubleCollectionTest.cls index 33dafdf..768cb73 100644 --- a/force-app/main/default/classes/test/classes/collection/DoubleCollectionTest.cls +++ b/force-app/main/default/classes/test/classes/collection/DoubleCollectionTest.cls @@ -1,7 +1,7 @@ @IsTest(isParallel=true) private class DoubleCollectionTest { @IsTest - private static void testSum() { + private static void sumShouldReturnSumIfItExists() { DoubleCollection c = DoubleCollection.of(new List{100, 150}); System.Assert.areEqual(250, c.sum().get()); } @@ -31,7 +31,7 @@ private class DoubleCollectionTest { } @IsTest - private static void testAverage() { + private static void averageShouldReturnAverageIfItExists() { DoubleCollection c = DoubleCollection.of(new List{100, 150}); System.Assert.areEqual(125, c.average().get()); } diff --git a/force-app/main/default/classes/util/OptionalDecimal.cls b/force-app/main/default/classes/util/OptionalDecimal.cls index 836a2ce..1a5e044 100644 --- a/force-app/main/default/classes/util/OptionalDecimal.cls +++ b/force-app/main/default/classes/util/OptionalDecimal.cls @@ -1,33 +1,36 @@ public with sharing class OptionalDecimal { + private static final OptionalDecimal EMPTY { + get { + EMPTY = EMPTY ?? new OptionalDecimal(null); + return EMPTY; + } + set; + } + private final Decimal value; - private static final OptionalDecimal EMPTY = new OptionalDecimal(); public static OptionalDecimal of(Decimal value) { - return new OptionalDecimal(value); + return value == null ? EMPTY : new OptionalDecimal(value); } public static OptionalDecimal empty() { return EMPTY; } - private OptionalDecimal() { - this.value = null; - } - private OptionalDecimal(Decimal value) { this.value = value; } public Boolean isPresent() { - return value != null; + return this.value != null; } public Decimal orElse(Decimal other) { - return value != null ? value : other; + return this.value ?? other; } public Decimal orElseThrow(Exception e) { - if (value != null) { + if (this.value != null) { return value; } else { throw e; @@ -35,7 +38,7 @@ public with sharing class OptionalDecimal { } public Decimal get() { - if (value == null) { + if (this.value == null) { throw new NoSuchElementException('No value present'); } return value; diff --git a/force-app/main/default/classes/util/OptionalDouble.cls b/force-app/main/default/classes/util/OptionalDouble.cls index 95988c7..3bbc873 100644 --- a/force-app/main/default/classes/util/OptionalDouble.cls +++ b/force-app/main/default/classes/util/OptionalDouble.cls @@ -1,8 +1,18 @@ public with sharing class OptionalDouble { + private static final OptionalDouble EMPTY { + get { + EMPTY = EMPTY ?? new OptionalDouble(null); + return EMPTY; + } + set; + } + private final Double value; - private static final OptionalDouble EMPTY = new OptionalDouble(); public static OptionalDouble of(Double value) { + if (value == null) { + return EMPTY; + } return new OptionalDouble(value); } @@ -10,24 +20,20 @@ public with sharing class OptionalDouble { return EMPTY; } - private OptionalDouble() { - this.value = null; - } - private OptionalDouble(Double value) { this.value = value; } public Boolean isPresent() { - return value != null; + return this.value != null; } public Double orElse(Double other) { - return value != null ? value : other; + return this.value ?? other; } public Double orElseThrow(Exception e) { - if (value != null) { + if (this.value != null) { return value; } else { throw e; @@ -35,7 +41,7 @@ public with sharing class OptionalDouble { } public Double get() { - if (value == null) { + if (this.value == null) { throw new NoSuchElementException('No value present'); } return value;