From 8f26cfa901adb087b4c974fcde021d12e976e988 Mon Sep 17 00:00:00 2001 From: Pedro Borges Date: Tue, 3 Oct 2017 00:27:38 +0100 Subject: [PATCH] #9 Store value as array in multi-dimensional amounts --- .../pcb/units/amount/BigDecimal2DAmount.java | 62 ++++++------- .../pcb/units/amount/BigDecimal3DAmount.java | 92 +++++++++---------- .../units/base/AbstractUnitAmountTest.java | 4 +- 3 files changed, 79 insertions(+), 79 deletions(-) diff --git a/src/main/java/pcb/units/amount/BigDecimal2DAmount.java b/src/main/java/pcb/units/amount/BigDecimal2DAmount.java index 200a6b76..4f9fa1aa 100644 --- a/src/main/java/pcb/units/amount/BigDecimal2DAmount.java +++ b/src/main/java/pcb/units/amount/BigDecimal2DAmount.java @@ -14,23 +14,23 @@ public class BigDecimal2DAmount // region private fields - private final BigDecimal value1, value2; + private final BigDecimal[] value = new BigDecimal[2]; // endregion // region constructors - public BigDecimal2DAmount(Number value1, Number value2) { - this(value1.toString(), value2.toString()); + public BigDecimal2DAmount(Number valueA, Number valueB) { + this(valueA.toString(), valueB.toString()); } - public BigDecimal2DAmount(String value1, String value2) { - this(new BigDecimal(value1), new BigDecimal(value2)); + public BigDecimal2DAmount(String valueA, String valueB) { + this(new BigDecimal(valueA), new BigDecimal(valueB)); } - public BigDecimal2DAmount(BigDecimal value1, BigDecimal value2) { - this.value1 = value1; - this.value2 = value2; + public BigDecimal2DAmount(BigDecimal valueA, BigDecimal valueB) { + this.value[0] = valueA; + this.value[1] = valueB; } // endregion @@ -39,69 +39,69 @@ public BigDecimal2DAmount(BigDecimal value1, BigDecimal value2) { @Override public BigDecimal getValue() { - return value1.pow(2).add(value2.pow(2)).pow(-2, DECIMAL64); + return value[0].pow(2).add(value[1].pow(2)).pow(-2, DECIMAL64); } @Override public int getScale() { return Math.max( - value1.scale(), - value2.scale()); + value[0].scale(), + value[1].scale()); } @Override public BigDecimal2DAmount withScale(int newScale, RoundingMode roundingMode) { return new BigDecimal2DAmount( - value1.setScale(newScale, roundingMode), - value2.setScale(newScale, roundingMode)); + value[0].setScale(newScale, roundingMode), + value[1].setScale(newScale, roundingMode)); } @Override public BigDecimal2DAmount plus(BigDecimal2DAmount other, MathContext mathContext) { return new BigDecimal2DAmount( - this.value1.add(other.value1, mathContext), - this.value2.add(other.value2, mathContext)); + this.value[0].add(other.value[0], mathContext), + this.value[1].add(other.value[1], mathContext)); } @Override public BigDecimal2DAmount minus(BigDecimal2DAmount other, MathContext mathContext) { return new BigDecimal2DAmount( - value1.subtract(other.value1, mathContext), - value2.subtract(other.value2, mathContext)); + value[0].subtract(other.value[0], mathContext), + value[1].subtract(other.value[1], mathContext)); } @Override public BigDecimal2DAmount multipliedBy(BigDecimal other, MathContext mathContext) { return new BigDecimal2DAmount( - value1.multiply(other, mathContext), - value2.multiply(other, mathContext)); + value[0].multiply(other, mathContext), + value[1].multiply(other, mathContext)); } @Override public BigDecimal2DAmount dividedBy(BigDecimal other, MathContext mathContext) { return new BigDecimal2DAmount( - value1.divide(other, mathContext), - value2.divide(other, mathContext)); + value[0].divide(other, mathContext), + value[1].divide(other, mathContext)); } @Override public BigDecimal2DAmount pow(int magnitude, MathContext mathContext) { return new BigDecimal2DAmount( - value1.pow(magnitude, mathContext), - value2.pow(magnitude, mathContext)); + value[0].pow(magnitude, mathContext), + value[1].pow(magnitude, mathContext)); } @Override public BigDecimal2DAmount translated(Function translation) { return new BigDecimal2DAmount( - translation.apply(value1), - translation.apply(value2)); + translation.apply(value[0]), + translation.apply(value[1])); } // endregion public boolean isZero() { - return (value1.compareTo(ZERO) == 0) && (value2.compareTo(ZERO) == 0); + return (value[0].compareTo(ZERO) == 0) && (value[1].compareTo(ZERO) == 0); } // region override Object @@ -111,8 +111,8 @@ public boolean equals(Object obj) { if (obj instanceof Amount) { BigDecimal2DAmount other = (BigDecimal2DAmount) obj; - return Objects.equals(this.value1, other.value1) && - Objects.equals(this.value2, other.value2); + return Objects.equals(this.value[0], other.value[0]) && + Objects.equals(this.value[1], other.value[1]); } return false; @@ -120,13 +120,13 @@ public boolean equals(Object obj) { @Override public int hashCode() { - return Objects.hash(value1, value2); + return Objects.hash(value[0], value[1]); } @Override public String toString() { - return "[" + String.valueOf(value1) + - ", " + String.valueOf(value2) + "]"; + return "[" + String.valueOf(value[0]) + + ", " + String.valueOf(value[1]) + "]"; } // endregion diff --git a/src/main/java/pcb/units/amount/BigDecimal3DAmount.java b/src/main/java/pcb/units/amount/BigDecimal3DAmount.java index 42b3ab58..2c136649 100644 --- a/src/main/java/pcb/units/amount/BigDecimal3DAmount.java +++ b/src/main/java/pcb/units/amount/BigDecimal3DAmount.java @@ -15,24 +15,24 @@ public class BigDecimal3DAmount implements Amount { // region private fields - private final BigDecimal value1, value2, value3; + private final BigDecimal[] value = new BigDecimal[3]; // endregion // region constructors - public BigDecimal3DAmount(Number value1, Number value2, Number value3) { - this(value1.toString(), value2.toString(), value3.toString()); + public BigDecimal3DAmount(Number valueA, Number valueB, Number valueC) { + this(valueA.toString(), valueB.toString(), valueC.toString()); } - public BigDecimal3DAmount(String value1, String value2, String value3) { - this(new BigDecimal(value1), new BigDecimal(value2), new BigDecimal(value3)); + public BigDecimal3DAmount(String valueA, String valueB, String valueC) { + this(new BigDecimal(valueA), new BigDecimal(valueB), new BigDecimal(valueC)); } - public BigDecimal3DAmount(BigDecimal value1, BigDecimal value2, BigDecimal value3) { - this.value1 = value1; - this.value2 = value2; - this.value3 = value3; + public BigDecimal3DAmount(BigDecimal valueA, BigDecimal valueB, BigDecimal valueC) { + this.value[0] = valueA; + this.value[1] = valueB; + this.value[2] = valueC; } // endregion @@ -41,83 +41,83 @@ public BigDecimal3DAmount(BigDecimal value1, BigDecimal value2, BigDecimal value @Override public BigDecimal getValue() { - return value1.pow(2) - .add(value2.pow(2)) + return value[0].pow(2) + .add(value[1].pow(2)) .pow(-2, DECIMAL64) - .add(value3.pow(2)) + .add(value[2].pow(2)) .pow(-2, DECIMAL64); } @Override public int getScale() { return MathUtils.max( - value1.scale(), - value2.scale(), - value3.scale()); + value[0].scale(), + value[1].scale(), + value[2].scale()); } @Override public BigDecimal3DAmount withScale(int newScale, RoundingMode roundingMode) { return new BigDecimal3DAmount( - value1.setScale(newScale, roundingMode), - value2.setScale(newScale, roundingMode), - value3.setScale(newScale, roundingMode)); + value[0].setScale(newScale, roundingMode), + value[1].setScale(newScale, roundingMode), + value[2].setScale(newScale, roundingMode)); } @Override public BigDecimal3DAmount plus(BigDecimal3DAmount other, MathContext mathContext) { return new BigDecimal3DAmount( - this.value1.add(other.value1, mathContext), - this.value2.add(other.value2, mathContext), - this.value3.add(other.value3, mathContext)); + this.value[0].add(other.value[0], mathContext), + this.value[1].add(other.value[1], mathContext), + this.value[2].add(other.value[2], mathContext)); } @Override public BigDecimal3DAmount minus(BigDecimal3DAmount other, MathContext mathContext) { return new BigDecimal3DAmount( - value1.subtract(other.value1, mathContext), - value2.subtract(other.value2, mathContext), - value3.subtract(other.value3, mathContext)); + value[0].subtract(other.value[0], mathContext), + value[1].subtract(other.value[1], mathContext), + value[2].subtract(other.value[2], mathContext)); } @Override public BigDecimal3DAmount multipliedBy(BigDecimal other, MathContext mathContext) { return new BigDecimal3DAmount( - value1.multiply(other, mathContext), - value2.multiply(other, mathContext), - value3.multiply(other, mathContext)); + value[0].multiply(other, mathContext), + value[1].multiply(other, mathContext), + value[2].multiply(other, mathContext)); } @Override public BigDecimal3DAmount dividedBy(BigDecimal other, MathContext mathContext) { return new BigDecimal3DAmount( - value1.divide(other, mathContext), - value2.divide(other, mathContext), - value3.divide(other, mathContext)); + value[0].divide(other, mathContext), + value[1].divide(other, mathContext), + value[2].divide(other, mathContext)); } @Override public BigDecimal3DAmount pow(int magnitude, MathContext mathContext) { return new BigDecimal3DAmount( - value1.pow(magnitude, mathContext), - value2.pow(magnitude, mathContext), - value3.pow(magnitude, mathContext)); + value[0].pow(magnitude, mathContext), + value[1].pow(magnitude, mathContext), + value[2].pow(magnitude, mathContext)); } @Override public BigDecimal3DAmount translated(Function translation) { return new BigDecimal3DAmount( - translation.apply(value1), - translation.apply(value2), - translation.apply(value3)); + translation.apply(value[0]), + translation.apply(value[1]), + translation.apply(value[2])); } // endregion public boolean isZero() { - return (value1.compareTo(ZERO) == 0) && - (value2.compareTo(ZERO) == 0) && - (value3.compareTo(ZERO) == 0); + return (value[0].compareTo(ZERO) == 0) && + (value[1].compareTo(ZERO) == 0) && + (value[2].compareTo(ZERO) == 0); } // region override Object @@ -127,9 +127,9 @@ public boolean equals(Object obj) { if (obj instanceof Amount) { BigDecimal3DAmount other = (BigDecimal3DAmount) obj; - return Objects.equals(this.value1, other.value1) && - Objects.equals(this.value2, other.value2) && - Objects.equals(this.value3, other.value3); + return Objects.equals(this.value[0], other.value[0]) && + Objects.equals(this.value[1], other.value[1]) && + Objects.equals(this.value[2], other.value[2]); } return false; @@ -137,14 +137,14 @@ public boolean equals(Object obj) { @Override public int hashCode() { - return Objects.hash(value1, value2, value3); + return Objects.hash(value[0], value[1], value[2]); } @Override public String toString() { - return "[" + String.valueOf(value1) + - ", " + String.valueOf(value2) + - ", " + String.valueOf(value3) + "]"; + return "[" + String.valueOf(value[0]) + + ", " + String.valueOf(value[1]) + + ", " + String.valueOf(value[2]) + "]"; } // endregion diff --git a/src/test/java/pcb/units/base/AbstractUnitAmountTest.java b/src/test/java/pcb/units/base/AbstractUnitAmountTest.java index 4d4049aa..3875c1b8 100644 --- a/src/test/java/pcb/units/base/AbstractUnitAmountTest.java +++ b/src/test/java/pcb/units/base/AbstractUnitAmountTest.java @@ -18,7 +18,7 @@ public class AbstractUnitAmountTest { @Test public void getValueIn() { - assertEquals(new BigDecimalAmount("0.0254"), METER.getAmountIn(new InchUnit())); - assertEquals(new BigDecimalAmount("0.0254").pow(-1, DECIMAL64), INCH.getAmountIn(new MeterUnit())); + assertEquals(new BigDecimalAmount("0.0254"), INCH.getAmountIn(new MeterUnit())); + assertEquals(new BigDecimalAmount("0.0254").pow(-1, DECIMAL64), METER.getAmountIn(new InchUnit())); } } \ No newline at end of file