/** * Converts the given value into a number. * * @param amount the value which should be converted into an <tt>Amount</tt> * @return an <tt>Amount</tt> representing the given input */ @Nonnull public static Amount of(int amount) { return of(new BigDecimal(amount)); }
/** * Converts the given value into a number. * * @param amount the value which should be converted into an <tt>Amount</tt> * @return an <tt>Amount</tt> representing the given input. <tt>NOTHING</tt> if the input was empty. */ @Nonnull public static Amount of(@Nullable BigDecimal amount) { if (amount == null) { return NOTHING; } return new Amount(amount); }
/** * Increases this number by the given amount in percent. If <tt>increase</tt> is 18 and the value of this is 100, * the result would by 118. * * @param increase the percent value by which the value of this will be increased * @return <tt>NOTHING</tt> if <tt>this</tt> is empty, {@code this * (1 + increase / 100)} otherwise */ @Nonnull @CheckReturnValue public Amount increasePercent(@Nonnull Amount increase) { return times(ONE.add(increase.asDecimal())); }
/** * 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)); }
/** * 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)); }
/** * Converts a file or byte size. * <p> * Supports sizes up to petabyte. Uses conventional SI-prefixed abbreviations like kB, MB. * * @param size the size to format in bytes * @return an english representation (using dot as decimal separator) along with one of the known abbreviations: * <tt>Bytes, KB, MB, GB, TB, PB</tt>. */ public static String formatSize(long size) { int index = 0; double sizeAsFloat = size; while (sizeAsFloat > 1000 && index < UNITS.length - 1) { sizeAsFloat = sizeAsFloat / 1000; index++; } return Amount.of(sizeAsFloat).toSmartRoundedString(NumberFormat.MACHINE_TWO_DECIMAL_PLACES) + " " + UNITS[index]; } }
/** * Used to multiply two percentages, like two discounts as if they where applied after each other. * <p> * This can be used to compute the effective discount if two discounts like 15% and 5% are applied after * each other. The result would be {@code (15 + 5) - (15 * 5 / 100)} which is <tt>19,25 %</tt> * * @param percent the second percent value which would be applied after this percent value. * @return the effective percent value after both percentages would have been applied or <tt>NOTHING</tt> if * <tt>this</tt> is empty. */ @Nonnull @CheckReturnValue public Amount multiplyPercent(@Nonnull Amount percent) { return add(percent).subtract(this.times(percent).divideBy(ONE_HUNDRED)); }
/** * 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; }
/** * Decreases this number by the given amount in percent. If <tt>decrease</tt> is 10 and the value of this is 100, * the result would by 90. * * @param decrease the percent value by which the value of this will be decreased * @return <tt>NOTHING</tt> if <tt>this</tt> is empty, {@code this * (1 - increase / 100)} otherwise */ @Nonnull @CheckReturnValue public Amount decreasePercent(@Nonnull Amount decrease) { return times(ONE.subtract(decrease.asDecimal())); }
/** * Returns the increase in percent of <tt>this</tt> over <tt>other</tt>. * This is equivalent to {@code ((this / other) - 1) * 100} * * @param other the base to compute the increase from. * @return an <tt>Amount</tt> representing the percentage increase between <tt>this</tt> and <tt>other</tt> * or <tt>NOTHING</tt> if either of both is empty. */ @Nonnull @CheckReturnValue public Amount percentageDifferenceOf(@Nonnull Amount other) { return divideBy(other).subtract(ONE).toPercent(); }
private void transformValue(Object obj, Map<String, String> valueMap, Field innerField) { try { innerField.setAccessible(true); Object val = innerField.get(obj); if (val != null) { if (val instanceof Amount) { valueMap.put(innerField.getName(), ((Amount) val).getAmount().toPlainString()); return; } valueMap.put(innerField.getName(), NLS.toMachineString(val)); } } catch (Exception e) { Exceptions.handle() .error(e) .to(IndexAccess.LOG) .withSystemErrorMessage("Cannot save POJO field %s of %s: %s (%s)", innerField.getName(), toString()) .handle(); } }
/** * Returns the ratio in percent from <tt>this</tt> to <tt>other</tt>. * This is equivalent to {@code this / other * 100} * * @param other the base to compute the percentage from. * @return an <tt>Amount</tt> representing the ratio between <tt>this</tt> and <tt>other</tt> * or <tt>NOTHING</tt> if either of both is empty. */ @Nonnull @CheckReturnValue public Amount percentageOf(@Nullable Amount other) { return divideBy(other).toPercent(); }
/** * Determines if this amount is less than or equal to the given one. * <p> * See {@link #compareTo(Amount)} for an explanation of how empty amounts are handled. * * @param o the other amount to compare against * @return <tt>true</tt> if this amount is less than or equals to the given one */ public boolean isLessThanOrEqual(Amount o) { return compareTo(o) <= 0; }
/** * Converts the number into a string just like {@link #toString(NumberFormat)}. However, if the number has no * decimal places, a rounded value (without .00) will be returned. * * @param format the {@code NumberFormat} used to obtain the number of decimal places, * the decimal format symbols and rounding mode * @return a <tt>Value</tt> containing the string representation according to the given format * or an empty <tt>Value</tt> if <tt>this</tt> is empty. Omits 0 as decimal places. * @see #toString() */ @Nonnull public Value toSmartRoundedString(@Nonnull NumberFormat format) { return convertToString(format, true); }
/** * Converts a percent value into a decimal fraction i.e. 34 % to 0.34 * Effectively this is {@code this / 100} * * @return a decimal representation fo the given percent value. */ @Nonnull @CheckReturnValue public Amount asDecimal() { return divideBy(ONE_HUNDRED); }
/** * 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)); }
/** * 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)); }
/** * String representation of the value along with its unit (is necessary) * * @return a string representation of the value */ public String getValueAsString() { return Amount.of(value).toSmartRoundedString(NumberFormat.TWO_DECIMAL_PLACES).append(" ", unit).toString(); }