@Override protected Object transformFromSource(Object value) { if (value != null) { return Amount.of(new BigDecimal(value.toString())); } return Amount.NOTHING; }
/** * 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 */ @Nonnull public static Amount of(long 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 Integer amount) { if (amount == null) { return NOTHING; } 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 Long amount) { if (amount == null) { return NOTHING; } 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 */ @Nonnull public static Amount of(double amount) { if (Double.isInfinite(amount) || Double.isNaN(amount)) { return NOTHING; } return of(BigDecimal.valueOf(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 Double amount) { if (amount == null || Double.isInfinite(amount) || Double.isNaN(amount)) { return NOTHING; } return of(BigDecimal.valueOf(amount)); }
/** * Returns the <tt>Amount</tt> for the wrapped value. * <p> * If the wrapped value can be converted to a BigDecimal ({@link #getBigDecimal(java.math.BigDecimal)}, * an <tt>Amount</tt> for the result is returned. Otherwise an empty <tt>Amount</tt> is returned. * * @return the wrapped value converted to <tt>Amount</tt>. The result might be an empty amount, if the wrapped * value is <tt>null</tt> or if no conversion was possible. * @see #getBigDecimal(java.math.BigDecimal) */ public Amount getAmount() { return Amount.of(getBigDecimal()); }
/** * 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)); }
/** * 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]; } }
@Override protected Object transformFromElastic(Value data) { String valueAsString = data.asString(); if (Strings.isEmpty(valueAsString)) { return Amount.NOTHING; } return Amount.of(new BigDecimal(valueAsString)); }
/** * 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)); }
/** * Converts the given string into a number. If the string is empty, <tt>NOTHING</tt> is returned. * If the string is malformed an exception will be thrown. * * @param value the string value which should be converted into a numeric value. * @return an <tt>Amount</tt> representing the given input. <tt>NOTHING</tt> if the input was empty. */ @Nonnull public static Amount ofMachineString(@Nullable String value) { if (Strings.isEmpty(value)) { return NOTHING; } return of(NLS.parseMachineString(BigDecimal.class, value)); }
/** * 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)); }
/** * 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)); }
/** * 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(); }
/** * 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)); }
return (V) Amount.of(parseDecimalNumberFromUser(value, lang));