/** * If this actual number if empty, the given supplier is used to compute one. Otherwise this will be returned. * * @param supplier the supplier which is used to compute a value if there is no internal value * @return <tt>this</tt> if there is an internal value, the computed value of <tt>supplier</tt> otherwise */ @Nonnull public Amount computeIfNull(Supplier<Amount> supplier) { if (isEmpty()) { return supplier.get(); } return this; }
/** * If this actual number if empty, the given value will be returned. Otherwise this will be returned. * * @param valueIfNothing the value which is used if there is no internal value * @return <tt>this</tt> if there is an internal value, <tt>valueIfNothing</tt> otherwise */ @Nonnull public Amount fill(@Nonnull Amount valueIfNothing) { if (isEmpty()) { return valueIfNothing; } else { return this; } }
/** * Adds the given number to <tt>this</tt>, if <tt>other</tt> is not empty. Otherwise <tt>this</tt> will be * returned. * * @param other the operand to add to this. * @return an <tt>Amount</tt> representing the sum of <tt>this</tt> and <tt>other</tt> if both values were filled. * If <tt>other</tt> is empty, <tt>this</tt> is returned. If this is empty, <tt>NOTHING</tt> is returned. */ @Nonnull @CheckReturnValue public Amount add(@Nullable Amount other) { if (other == null || other.isEmpty()) { return this; } if (isEmpty()) { return NOTHING; } return Amount.of(value.add(other.value)); }
@Override protected Object transformToJDBC(Object object) { return object == null || ((Amount) object).isEmpty() ? null : ((Amount) object).getAmount(); }
/** * Multiplies the given number with <tt>this</tt>. If either of both is empty, <tt>NOTHING</tt> will be returned. * * @param other the operand to multiply with this. * @return an <tt>Amount</tt> representing the product of <tt>this</tt> and <tt>other</tt> if both values were * filled. * If <tt>other</tt> is empty or if <tt>this</tt> is empty, <tt>NOTHING</tt> is returned. */ @Nonnull @CheckReturnValue public Amount times(@Nonnull Amount other) { if (other.isEmpty() || isEmpty()) { return NOTHING; } return Amount.of(value.multiply(other.value)); }
/** * Subtracts the given number from <tt>this</tt>, if <tt>other</tt> is not empty. Otherwise <tt>this</tt> will be * returned. * * @param other the operand to subtract from this. * @return an <tt>Amount</tt> representing the difference of <tt>this</tt> and <tt>other</tt> if both values were * filled. * If <tt>other</tt> is empty, <tt>this</tt> is returned. If this is empty, <tt>NOTHING</tt> is returned. */ @Nonnull @CheckReturnValue public Amount subtract(@Nullable Amount other) { if (other == null || other.isEmpty()) { return this; } if (isEmpty()) { return NOTHING; } return Amount.of(value.subtract(other.value)); }
@Override protected Object transformToElastic(Object object) { return object == null || ((Amount) object).isEmpty() ? null : ((Amount) object).getAmount().toPlainString(); }
/** * Compares this amount against the given amount and returns the one with the lower value. * <p> * If either of the amounts is empty, the filled one is returned. If both are empty, an empty amount is returned. * * @param other the amount to compare against * @return the amount with the lower value or an empty amount, if both amounts are empty */ @Nonnull @SuppressWarnings("squid:S1698") @Explain("Indentity against this is safe and a shortcut to speed up comparisons") public Amount min(@Nullable Amount other) { if (other == this || other == null) { return this; } if (isEmpty()) { return other; } if (other.isEmpty()) { return this; } return this.value.compareTo(other.value) < 0 ? this : other; }
/** * Compares this amount against the given amount and returns the one with the higher value. * <p> * If either of the amounts is empty, the filled one is returned. If both are empty, an empty amount is returned. * * @param other the amount to compare against * @return the amount with the higher value or an empty amount, if both amounts are empty */ @Nonnull @SuppressWarnings("squid:S1698") @Explain("Indentity against this is safe and a shortcut to speed up comparisons") public Amount max(@Nullable Amount other) { if (other == this || other == null) { return this; } if (isEmpty()) { return other; } if (other.isEmpty()) { return this; } return this.value.compareTo(other.value) > 0 ? this : other; }
/** * Divides <tt>this</tt> by the given number. If either of both is empty, or the given number is zero, * <tt>NOTHING</tt> will be returned. The division uses {@link MathContext#DECIMAL128} * * @param other the operand to divide this by. * @return an <tt>Amount</tt> representing the division of <tt>this</tt> by <tt>other</tt> or <tt>NOTHING</tt> * if either of both is empty. */ @Nonnull @CheckReturnValue public Amount divideBy(@Nullable Amount other) { if (other == null || other.isZeroOrNull() || isEmpty()) { return NOTHING; } return Amount.of(value.divide(other.value, MathContext.DECIMAL128)); }
/** * Rounds the number according to the given format. In contrast to only round values when displaying them as * string, this method returns a modified <tt>Amount</tt> which as potentially lost some precision. Depending on * the next computation this might return significantly different values in contrast to first performing all * computations and round at the end when rendering the values as string. * * @param scale the precision * @param roundingMode the rounding operation * @return returns an <tt>Amount</tt> which is rounded using the given {@code RoundingMode} * or <tt>NOTHING</tt> if the value is empty. */ @Nonnull @CheckReturnValue public Amount round(int scale, @Nonnull RoundingMode roundingMode) { if (isEmpty()) { return NOTHING; } return Amount.of(value.setScale(scale, roundingMode)); }
if (isEmpty()) { return "";
/** * Returns a {@link Amount} whose value is {@code (this % other)}. * * @param other the divisor value by which this {@code Amount} is to be divided. * @return an {@link Amount} representing the remainder of the two amounts * @see BigDecimal#remainder(BigDecimal) */ public Amount remainder(Amount other) { if (isEmpty() || other.isZeroOrNull()) { return Amount.NOTHING; } return Amount.of(value.remainder(other.value)); }
private Value convertToString(NumberFormat format, boolean smartRound) { if (isEmpty()) { return Value.of(null); } DecimalFormat df = new DecimalFormat(); df.setMinimumFractionDigits(smartRound ? 0 : format.getScale()); df.setMaximumFractionDigits(format.getScale()); df.setDecimalFormatSymbols(format.getDecimalFormatSymbols()); df.setGroupingUsed(format.isUseGrouping()); return Value.of(df.format(value)).append(" ", format.getSuffix()); }
@Override protected void checkNullability(Object propertyValue) { super.checkNullability(propertyValue); if (!isNullable() && ((Amount) propertyValue).isEmpty()) { throw Exceptions.createHandled().withNLSKey("Property.fieldNotNullable").set("field", getLabel()).handle(); } }