Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* It also provides a set of arithmetic operations that return new instances with the appropriate scale and rounding.
*/
@NullMarked
@SuppressWarnings("unused")
public record ScaledBigDecimal(
BigDecimal value
) implements CustomType<BigDecimal>, Comparable<ScaledBigDecimal> {
Expand Down Expand Up @@ -112,6 +113,10 @@ public static ScaledBigDecimal valueOf(String value) {
return new ScaledBigDecimal(value);
}

public static ScaledBigDecimal valueOf(BigDecimal value) {
return new ScaledBigDecimal(value);
}

public ScaledBigDecimal add(ScaledBigDecimal other) {
return new ScaledBigDecimal(this.value().add(other.value()));
}
Expand Down Expand Up @@ -188,10 +193,18 @@ public BigDecimal toBigDecimal(int scale) {
return this.value().setScale(scale, RoundingMode.HALF_UP);
}

public BigDecimal toBigDecimal(int scale, RoundingMode roundingMode) {
return this.value().setScale(scale, roundingMode);
}

public ScaledBigDecimal roundToScale(int scale) {
return new ScaledBigDecimal(this.value().setScale(scale, RoundingMode.HALF_UP));
}

public ScaledBigDecimal roundToScale(int scale, RoundingMode roundingMode) {
return new ScaledBigDecimal(this.value().setScale(scale, roundingMode));
}

@Override
public String toString() {
return this.value().toString();
Expand All @@ -203,8 +216,8 @@ public boolean equals(Object o) {
return true;
}

if (o instanceof ScaledBigDecimal other) {
return this.value().compareTo(other.value()) == 0;
if (o instanceof ScaledBigDecimal(BigDecimal other)) {
return this.value().compareTo(other) == 0;
}

return false;
Expand Down Expand Up @@ -261,4 +274,44 @@ public byte byteValue() {
public short shortValue() {
return (short) intValue();
}

public boolean isZero() {
return this.value().compareTo(BigDecimal.ZERO) == 0;
}

public boolean isNegative() {
return this.value().compareTo(BigDecimal.ZERO) < 0;
}

public boolean isPositive() {
return this.value().compareTo(BigDecimal.ZERO) > 0;
}

public boolean isPositiveOrZero() {
return this.value().compareTo(BigDecimal.ZERO) >= 0;
}

public boolean isNegativeOrZero() {
return this.value().compareTo(BigDecimal.ZERO) <= 0;
}

public boolean isEqual(ScaledBigDecimal other) {
return this.compareTo(other) == 0;
}

public boolean isBiggerThan(ScaledBigDecimal other) {
return this.compareTo(other) > 0;
}

public boolean isEqualOrBiggerThan(ScaledBigDecimal other) {
return this.compareTo(other) >= 0;
}

public boolean isSmallerThan(ScaledBigDecimal other) {
return this.compareTo(other) < 0;
}

public boolean isEqualOrSmallerThan(ScaledBigDecimal other) {
return this.compareTo(other) <= 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ public boolean isValid(ScaledBigDecimal value, ConstraintValidatorContext contex
return true;
}

return value.compareTo(ScaledBigDecimal.ZERO) < 1;
return value.isNegativeOrZero();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ public boolean isValid(ScaledBigDecimal value, ConstraintValidatorContext contex
return true;
}

return value.compareTo(ScaledBigDecimal.ZERO) < 0;
return value.isNegative();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ public boolean isValid(ScaledBigDecimal value, ConstraintValidatorContext contex
return true;
}

return value.compareTo(ScaledBigDecimal.ZERO) > -1;
return value.isPositiveOrZero();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ public boolean isValid(ScaledBigDecimal value, ConstraintValidatorContext contex
return true;
}

return value.compareTo(ScaledBigDecimal.ZERO) > 0;
return value.isPositive();
}
}