@Override public boolean isValid(MonetaryAmount value, ConstraintValidatorContext context) { // null values are valid if ( value == null ) { return true; } return value.getNumber().numberValueExact( BigDecimal.class ).compareTo( minValue ) != -1; }
@Override public boolean isValid(MonetaryAmount value, ConstraintValidatorContext context) { // null values are valid if ( value == null ) { return true; } int comparisonResult = value.getNumber().numberValueExact( BigDecimal.class ).compareTo( maxValue ); return inclusive ? comparisonResult <= 0 : comparisonResult < 0; }
@Override public boolean isValid(MonetaryAmount value, ConstraintValidatorContext context) { // null values are valid if ( value == null ) { return true; } return value.getNumber().numberValueExact( BigDecimal.class ).compareTo( maxValue ) != 1; }
@Override public boolean isValid(MonetaryAmount value, ConstraintValidatorContext context) { // null values are valid if ( value == null ) { return true; } int comparisonResult = value.getNumber().numberValueExact( BigDecimal.class ).compareTo( minValue ); return inclusive ? comparisonResult >= 0 : comparisonResult > 0; }
@Override public boolean isValid(MonetaryAmount value, ConstraintValidatorContext context) { // null values are valid if ( value == null ) { return true; } return value.getNumber().numberValueExact( BigDecimal.class ).compareTo( minValue ) != -1; }
@Override public boolean isValid(MonetaryAmount value, ConstraintValidatorContext context) { // null values are valid if ( value == null ) { return true; } return value.getNumber().numberValueExact( BigDecimal.class ).compareTo( maxValue ) != 1; }
@Override public boolean isValid(MonetaryAmount value, ConstraintValidatorContext context) { // null values are valid if ( value == null ) { return true; } int comparisonResult = value.getNumber().numberValueExact( BigDecimal.class ).compareTo( maxValue ); return inclusive ? comparisonResult <= 0 : comparisonResult < 0; }
@Override public boolean isValid(MonetaryAmount value, ConstraintValidatorContext context) { // null values are valid if ( value == null ) { return true; } int comparisonResult = value.getNumber().numberValueExact( BigDecimal.class ).compareTo( minValue ); return inclusive ? comparisonResult >= 0 : comparisonResult > 0; }
@Override public BigDecimal write(@Nonnull final MonetaryAmount amount) { final BigDecimal decimal = amount.getNumber().numberValueExact(BigDecimal.class); final int defaultFractionDigits = amount.getCurrency().getDefaultFractionDigits(); final int scale = Math.max(decimal.scale(), defaultFractionDigits); return decimal.setScale(scale, RoundingMode.UNNECESSARY); }
@Override public BigDecimal write(@Nonnull final MonetaryAmount amount) { final BigDecimal decimal = amount.getNumber().numberValueExact(BigDecimal.class); final int defaultFractionDigits = amount.getCurrency().getDefaultFractionDigits(); final int scale = Math.max(decimal.scale(), defaultFractionDigits); return decimal.setScale(scale, RoundingMode.UNNECESSARY); }
/** * A protected helper method to multiply 2 {@link NumberValue} types.<br> * If either of the values is <code>null</code> an {@link ArithmeticException} is thrown. * * @param multiplicand the first value to be multiplied * @param multiplier the second value to be multiplied * @return the result of the multiplication as {@link NumberValue} */ protected static NumberValue multiply(NumberValue multiplicand, NumberValue multiplier) { if (Objects.isNull(multiplicand)) { throw new ArithmeticException("The multiplicand cannot be null"); } if (Objects.isNull(multiplier)) { throw new ArithmeticException("The multiplier cannot be null"); } return new DefaultNumberValue( multiplicand.numberValueExact(BigDecimal.class).multiply(multiplier.numberValue(BigDecimal.class))); }
/** * A protected helper method to divide 2 {@link NumberValue} types.<br> * If either of the values is <code>null</code> an {@link ArithmeticException} is thrown. * * @param dividend the first value to be divided * @param divisor the value to be divided by * @return the result of the division as {@link NumberValue} */ protected static NumberValue divide(NumberValue dividend, NumberValue divisor) { if (Objects.isNull(dividend)) { throw new ArithmeticException("The dividend cannot be null"); } if (Objects.isNull(divisor)) { throw new ArithmeticException("The divisor cannot be null"); } return new DefaultNumberValue( dividend.numberValueExact(BigDecimal.class).divide(divisor.numberValue(BigDecimal.class), MathContext.DECIMAL64)); }
/** * A protected helper method to divide 2 {@link NumberValue} types.<br> * If either of the values is <code>null</code> an {@link ArithmeticException} is thrown. * * @param dividend the first value to be divided * @param divisor the value to be divided by * @param context the {@link MathContext} to use * @return the result of the division as {@link NumberValue} */ protected static NumberValue divide(NumberValue dividend, NumberValue divisor, MathContext context) { if (Objects.isNull(dividend)) { throw new ArithmeticException("The dividend cannot be null"); } if (Objects.isNull(divisor)) { throw new ArithmeticException("The divisor cannot be null"); } return new DefaultNumberValue( dividend.numberValueExact(BigDecimal.class).divide(divisor.numberValue(BigDecimal.class), context)); }
protected static Long toCents(final MonetaryAmount money) { return money.getNumber().numberValueExact(BigDecimal.class).movePointRight(2).longValue(); }
"Section 4.2.3: Number value (Number, BigDecimal) returned is not correct for " + num + ", type; " + type.getName(), 0L, result.numberValue(BigDecimal.class).longValueExact()); result.numberValueExact(BigDecimal.class);