Skip to content

Commit e6cf8ca

Browse files
committed
add comparison and roundingmodes to ScaledBigDecimal
1 parent 670925b commit e6cf8ca

5 files changed

Lines changed: 51 additions & 6 deletions

File tree

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

Lines changed: 47 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> {
@@ -188,10 +189,18 @@ public BigDecimal toBigDecimal(int scale) {
188189
return this.value().setScale(scale, RoundingMode.HALF_UP);
189190
}
190191

192+
public BigDecimal toBigDecimal(int scale, RoundingMode roundingMode) {
193+
return this.value().setScale(scale, roundingMode);
194+
}
195+
191196
public ScaledBigDecimal roundToScale(int scale) {
192197
return new ScaledBigDecimal(this.value().setScale(scale, RoundingMode.HALF_UP));
193198
}
194199

200+
public ScaledBigDecimal roundToScale(int scale, RoundingMode roundingMode) {
201+
return new ScaledBigDecimal(this.value().setScale(scale, roundingMode));
202+
}
203+
195204
@Override
196205
public String toString() {
197206
return this.value().toString();
@@ -203,8 +212,8 @@ public boolean equals(Object o) {
203212
return true;
204213
}
205214

206-
if (o instanceof ScaledBigDecimal other) {
207-
return this.value().compareTo(other.value()) == 0;
215+
if (o instanceof ScaledBigDecimal(BigDecimal other)) {
216+
return this.value().compareTo(other) == 0;
208217
}
209218

210219
return false;
@@ -261,4 +270,40 @@ public byte byteValue() {
261270
public short shortValue() {
262271
return (short) intValue();
263272
}
273+
274+
public boolean isZero() {
275+
return this.value().compareTo(BigDecimal.ZERO) == 0;
276+
}
277+
278+
public boolean isNegative() {
279+
return this.value().compareTo(BigDecimal.ZERO) < 0;
280+
}
281+
282+
public boolean isPositive() {
283+
return this.value().compareTo(BigDecimal.ZERO) > 0;
284+
}
285+
286+
public boolean isPositiveOrZero() {
287+
return this.value().compareTo(BigDecimal.ZERO) >= 0;
288+
}
289+
290+
public boolean isNegativeOrZero() {
291+
return this.value().compareTo(BigDecimal.ZERO) <= 0;
292+
}
293+
294+
public boolean isBiggerThan(ScaledBigDecimal other) {
295+
return this.compareTo(other) > 0;
296+
}
297+
298+
public boolean isEqualOrBiggerThan(ScaledBigDecimal other) {
299+
return this.compareTo(other) >= 0;
300+
}
301+
302+
public boolean isSmallerThan(ScaledBigDecimal other) {
303+
return this.compareTo(other) < 0;
304+
}
305+
306+
public boolean isEqualOrSmallerThan(ScaledBigDecimal other) {
307+
return this.compareTo(other) <= 0;
308+
}
264309
}

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)