Skip to content

Commit

Permalink
Refactors DecimalCollection and DoubleCollection, as well as Optional…
Browse files Browse the repository at this point in the history
…Decimal/OptionalDouble to be a bit more concise by taking advantage of the new(er) null coalesce operator
  • Loading branch information
jamessimone committed Jul 3, 2024
1 parent e4fcf5d commit e879ff0
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 95 deletions.
71 changes: 35 additions & 36 deletions force-app/main/default/classes/collection/DecimalCollection.cls
Original file line number Diff line number Diff line change
@@ -1,84 +1,83 @@
public class DecimalCollection {
private List<Decimal> decimals;
private List<Decimal> 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<Decimal> 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>();
Iterator<Decimal> 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<Decimal> asList() {
return new List<Decimal>(decimals);
return new List<Decimal>(this.decimals);
}

public Set<Decimal> asSet() {
return new Set<Decimal>(decimals);
return new Set<Decimal>(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;
}
}
return OptionalDecimal.of(max);
}

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;
}
}
return OptionalDecimal.of(min);
}

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<Decimal> filtered = new List<Decimal>();
for (Decimal d : decimals) {
for (Decimal d : this.decimals) {
if (predicate.call(d)) {
filtered.add(d);
}
Expand Down
68 changes: 34 additions & 34 deletions force-app/main/default/classes/collection/DoubleCollection.cls
Original file line number Diff line number Diff line change
@@ -1,79 +1,79 @@
public class DoubleCollection {
private List<Double> doubles;
private List<Double> 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<Double> 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>();
Iterator<Double> 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<Double> asList() {
return new List<Double>(doubles);
return new List<Double>(this.doubles);
}

public Set<Double> asSet() {
return new Set<Double>(doubles);
return new Set<Double>(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;
}
}
return OptionalDouble.of(max);
}

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;
}
}
return OptionalDouble.of(min);
}

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<Double> filtered = new List<Double>();
for (Double d : doubles) {
for (Double d : this.doubles) {
if (predicate.call(d)) {
filtered.add(d);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Decimal>{100, 150});
System.Assert.areEqual(250, c.sum().get());
}
Expand Down Expand Up @@ -31,20 +31,20 @@ private class DecimalCollectionTest {
}

@IsTest
private static void testDecimalCollectionAverage() {
private static void averageShouldReturnAverageIfExists() {
DecimalCollection c = DecimalCollection.of(new List<Decimal>{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<Decimal>{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;
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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<Double>{100, 150});
System.Assert.areEqual(250, c.sum().get());
}
Expand Down Expand Up @@ -31,7 +31,7 @@ private class DoubleCollectionTest {
}

@IsTest
private static void testAverage() {
private static void averageShouldReturnAverageIfItExists() {
DoubleCollection c = DoubleCollection.of(new List<Double>{100, 150});
System.Assert.areEqual(125, c.average().get());
}
Expand Down
23 changes: 13 additions & 10 deletions force-app/main/default/classes/util/OptionalDecimal.cls
Original file line number Diff line number Diff line change
@@ -1,41 +1,44 @@
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;
}
}

public Decimal get() {
if (value == null) {
if (this.value == null) {
throw new NoSuchElementException('No value present');
}
return value;
Expand Down
24 changes: 15 additions & 9 deletions force-app/main/default/classes/util/OptionalDouble.cls
Original file line number Diff line number Diff line change
@@ -1,41 +1,47 @@
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);
}

public static OptionalDouble empty() {
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;
}
}

public Double get() {
if (value == null) {
if (this.value == null) {
throw new NoSuchElementException('No value present');
}
return value;
Expand Down

0 comments on commit e879ff0

Please sign in to comment.