Skip to content

Commit 38aa37b

Browse files
authored
Add scaled big decimal features (#56)
* add comparison and roundingmodes to ScaledBigDecimal * add isEqual comparing by value * add valueOf BigDecimal
1 parent 670925b commit 38aa37b

5 files changed

Lines changed: 59 additions & 6 deletions

File tree

src/main/java/it/aboutbits/springboot/toolbox/type/ScaledBigDecimal.java

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* It also provides a set of arithmetic operations that return new instances with the appropriate scale and rounding.
1414
*/
1515
@NullMarked
16+
@SuppressWarnings("unused")
1617
public record ScaledBigDecimal(
1718
BigDecimal value
1819
) implements CustomType<BigDecimal>, Comparable<ScaledBigDecimal> {
@@ -112,6 +113,10 @@ public static ScaledBigDecimal valueOf(String value) {
112113
return new ScaledBigDecimal(value);
113114
}
114115

116+
public static ScaledBigDecimal valueOf(BigDecimal value) {
117+
return new ScaledBigDecimal(value);
118+
}
119+
115120
public ScaledBigDecimal add(ScaledBigDecimal other) {
116121
return new ScaledBigDecimal(this.value().add(other.value()));
117122
}
@@ -188,10 +193,18 @@ public BigDecimal toBigDecimal(int scale) {
188193
return this.value().setScale(scale, RoundingMode.HALF_UP);
189194
}
190195

196+
public BigDecimal toBigDecimal(int scale, RoundingMode roundingMode) {
197+
return this.value().setScale(scale, roundingMode);
198+
}
199+
191200
public ScaledBigDecimal roundToScale(int scale) {
192201
return new ScaledBigDecimal(this.value().setScale(scale, RoundingMode.HALF_UP));
193202
}
194203

204+
public ScaledBigDecimal roundToScale(int scale, RoundingMode roundingMode) {
205+
return new ScaledBigDecimal(this.value().setScale(scale, roundingMode));
206+
}
207+
195208
@Override
196209
public String toString() {
197210
return this.value().toString();
@@ -203,8 +216,8 @@ public boolean equals(Object o) {
203216
return true;
204217
}
205218

206-
if (o instanceof ScaledBigDecimal other) {
207-
return this.value().compareTo(other.value()) == 0;
219+
if (o instanceof ScaledBigDecimal(BigDecimal other)) {
220+
return this.value().compareTo(other) == 0;
208221
}
209222

210223
return false;
@@ -261,4 +274,44 @@ public byte byteValue() {
261274
public short shortValue() {
262275
return (short) intValue();
263276
}
277+
278+
public boolean isZero() {
279+
return this.value().compareTo(BigDecimal.ZERO) == 0;
280+
}
281+
282+
public boolean isNegative() {
283+
return this.value().compareTo(BigDecimal.ZERO) < 0;
284+
}
285+
286+
public boolean isPositive() {
287+
return this.value().compareTo(BigDecimal.ZERO) > 0;
288+
}
289+
290+
public boolean isPositiveOrZero() {
291+
return this.value().compareTo(BigDecimal.ZERO) >= 0;
292+
}
293+
294+
public boolean isNegativeOrZero() {
295+
return this.value().compareTo(BigDecimal.ZERO) <= 0;
296+
}
297+
298+
public boolean isEqual(ScaledBigDecimal other) {
299+
return this.compareTo(other) == 0;
300+
}
301+
302+
public boolean isBiggerThan(ScaledBigDecimal other) {
303+
return this.compareTo(other) > 0;
304+
}
305+
306+
public boolean isEqualOrBiggerThan(ScaledBigDecimal other) {
307+
return this.compareTo(other) >= 0;
308+
}
309+
310+
public boolean isSmallerThan(ScaledBigDecimal other) {
311+
return this.compareTo(other) < 0;
312+
}
313+
314+
public boolean isEqualOrSmallerThan(ScaledBigDecimal other) {
315+
return this.compareTo(other) <= 0;
316+
}
264317
}

src/main/java/it/aboutbits/springboot/toolbox/validation/validator/NegativeOrZeroScaledBigDecimalValidator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ public boolean isValid(ScaledBigDecimal value, ConstraintValidatorContext contex
1212
return true;
1313
}
1414

15-
return value.compareTo(ScaledBigDecimal.ZERO) < 1;
15+
return value.isNegativeOrZero();
1616
}
1717
}

src/main/java/it/aboutbits/springboot/toolbox/validation/validator/NegativeScaledBigDecimalValidator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ public boolean isValid(ScaledBigDecimal value, ConstraintValidatorContext contex
1212
return true;
1313
}
1414

15-
return value.compareTo(ScaledBigDecimal.ZERO) < 0;
15+
return value.isNegative();
1616
}
1717
}

src/main/java/it/aboutbits/springboot/toolbox/validation/validator/PositiveOrZeroScaledBigDecimalValidator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ public boolean isValid(ScaledBigDecimal value, ConstraintValidatorContext contex
1212
return true;
1313
}
1414

15-
return value.compareTo(ScaledBigDecimal.ZERO) > -1;
15+
return value.isPositiveOrZero();
1616
}
1717
}

src/main/java/it/aboutbits/springboot/toolbox/validation/validator/PositiveScaledBigDecimalValidator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ public boolean isValid(ScaledBigDecimal value, ConstraintValidatorContext contex
1212
return true;
1313
}
1414

15-
return value.compareTo(ScaledBigDecimal.ZERO) > 0;
15+
return value.isPositive();
1616
}
1717
}

0 commit comments

Comments
 (0)