long based implementation of MonetaryAmount.This class internally uses a
single long number as numeric representation, which basically is interpreted as minor units.
It suggested to have a performance advantage of a 10-15 times faster compared to Money,
which internally uses BigDecimal. Nevertheless this comes with a price of less precision.
As an example performing the following calculation one million times, results in slightly
different results:
Money money1 = money1.add(Money.of(1234567.3444, "EUR"));
money1 = money1.subtract(Money.of(232323, "EUR"));
money1 = money1.multiply(3.4);
money1 = money1.divide(5.456);
Executed one million (1000000) times this results in EUR 1657407.962529182, calculated in
3680 ms, or roughly 3ns/loop.
whereas
FastMoney money1 = money1.add(FastMoney.of(1234567.3444, "EUR"));
money1 = money1.subtract(FastMoney.of(232323, "EUR"));
money1 = money1.multiply(3.4);
money1 = money1.divide(5.456);
executed one million (1000000) times results in EUR 1657407.96251, calculated in 179 ms,
which is less than 1ns/loop.
Also note than mixing up types may drastically change the performance behavior. E.g. replacing the code above with the following: *
FastMoney money1 = money1.add(Money.of(1234567.3444, "EUR"));
money1 = money1.subtract(FastMoney.of(232323, "EUR"));
money1 = money1.multiply(3.4);
money1 = money1.divide(5.456);
executed one million (1000000) times may execute significantly longer, since monetary amount type conversion is involved.
Basically, when mixing amount implementations, the performance of the amount, on which most of the operations are operated, has the most significant impact on the overall performance behavior.
| Modifier and Type | Field and Description |
|---|---|
| private final CurrencyUnit | currency
The currency of this amount. |
| private static final Logger | LOG
The logger used. |
| private static final BigDecimal | MAX_BD
Maximum possible numeric value supported. |
| public static final FastMoney | MAX_VALUE
Maximum possible value supported, using XX (no currency). |
| private static final BigDecimal | MIN_BD
Minimum possible numeric value supported. |
| public static final FastMoney | MIN_VALUE
Minimum possible value supported, using XX (no currency). |
| private static final MonetaryContext | MONETARY_CONTEXT
the |
| private final long | number
The numeric part of this amount. |
| private static final int | SCALE
The current scale represented by the number. |
| private static final long |
| Access | Constructor and Description |
|---|---|
| private | FastMoney(Number
the amount, not null. number, CurrencyUnit the currency, not null. currency)Creates a new instance of |
| private | FastMoney(NumberValue
the numeric value, not null. numberValue, CurrencyUnit the currency, not null. currency)Creates a new instance of |
| private | FastMoney(long
The format number value number, CurrencyUnit the currency, not null. currency)Creates a new instance of |
| Modifier and Type | Method and Description |
|---|---|
| public FastMoney | abs()
Implements javax. Returns a |
| public FastMoney | add(MonetaryAmount amount)
Implements javax. Returns a |
| private void | |
| protected void | checkNumber(Number
the number to be checked, including null.. number)Internal method to check for correct number parameter. |
| public int | compareTo(MonetaryAmount o)
Implements java. Compares this object with the specified object for order. |
| private static MonetaryAmountFormat | |
| public FastMoney | divide(Number divisor)
Implements javax. Returns a |
| public FastMoney | divide(long divisor)
Implements javax. Returns a |
| public FastMoney | divide(double divisor)
Implements javax. Returns a |
| public FastMoney[] | divideAndRemainder(Number divisor)
Implements javax. Returns a two-element |
| public FastMoney[] | divideAndRemainder(long divisor)
Implements javax. Returns a two-element |
| public FastMoney[] | divideAndRemainder(double divisor)
Implements javax. Returns a two-element |
| public FastMoney | divideToIntegralValue(Number divisor)
Implements javax. Returns a |
| public FastMoney | divideToIntegralValue(long divisor)
Implements javax. Returns a |
| public FastMoney | divideToIntegralValue(double divisor)
Implements javax. Returns a |
| public boolean | equals(Object obj)
Overrides java. Indicates whether some other object is "equal to" this one. |
| public static FastMoney | |
| private BigDecimal | |
| public MonetaryContext | Returns: theMonetaryContext used, never null.Implements javax. Access the |
| public CurrencyUnit | Returns: the currency, nevernullImplements javax. Returns the amount’s currency, modelled as |
| public MonetaryAmountFactory | getFactory()
Implements javax. Creates a new |
| private long | |
| public NumberValue | Returns: TheNumber representation matching best.Implements javax. Gets the number representation of the numeric value of this item. |
| public int | Returns: the precision of thisRoundedMoney.Returns the precision of this |
| public int | |
| public int | |
| public boolean | |
| public boolean | isEqualTo(MonetaryAmount amount)
Implements javax. Compares two instances of |
| public boolean | isGreaterThan(MonetaryAmount amount)
Implements javax. Compares two instances of |
| public boolean | |
| public boolean | isGreaterThanOrEqualTo(MonetaryAmount amount)
Implements javax. Compares two instances of |
| public boolean | |
| public boolean | isLessThan(MonetaryAmount amount)
Implements javax. Compares two instances of |
| public boolean | |
| public boolean | isLessThanOrEqualTo(MonetaryAmount amount)
Implements javax. Compares two instances of |
| public boolean | |
| public boolean | isNegative()
Overrides default javax. Checks if a |
| public boolean | isNegativeOrZero()
Overrides default javax. Checks if a |
| private boolean | |
| public boolean | isPositive()
Overrides default javax. Checks if a |
| public boolean | isPositiveOrZero()
Overrides default javax. Checks if a |
| public boolean | |
| public FastMoney | multiply(Number multiplicand)
Implements javax. Returns a |
| public FastMoney | multiply(double multiplicand)
Implements javax. Returns a |
| public FastMoney | multiply(long multiplicand)
Implements javax. Returns a |
| public FastMoney | negate()
Implements javax. Returns a |
| public static FastMoney | Returns: A new instance ofFastMoney.The numeric part, not null. numberBinding, CurrencyUnit The target currency, not null. currency)Static factory method for creating a new instance of |
| public static FastMoney | Returns: A new instance ofFastMoney.The numeric part, not null. number, CurrencyUnit The target currency, not null. currency)Static factory method for creating a new instance of |
| public static FastMoney | |
| public static FastMoney | Returns: the monetary amount from minor unitsthe currency, not null currency, long the amount of money in the minor division of the currency amountMinor)Obtains an instance of |
| public static FastMoney | Returns: the monetary amount from minor unitsthe currency, not null currency, long the amount of money in the minor division of the currency amountMinor, int number of digits factionDigits)Obtains an instance of |
| public static FastMoney | Returns: FastMoney instancethe text to parse not null text)Obtains an instance of FastMoney from a text string such as 'EUR 25.25'. |
| public static FastMoney | Returns: FastMoney instancethe text to parse not null text, MonetaryAmountFormat the formatter to use not null formatter)Obtains an instance of FastMoney from a text using specific formatter. |
| public FastMoney | |
| public <R> R | query(MonetaryQuery<R> query)
Overrides default javax. Queries this monetary amount for a value. |
| public FastMoney | remainder(Number divisor)
Implements javax. Returns a |
| public FastMoney | remainder(long divisor)
Implements javax. Returns a |
| public FastMoney | remainder(double divisor)
Implements javax. Returns a |
| public FastMoney | scaleByPowerOfTen(int power)
Implements javax. Returns a |
| public int | signum()
Implements javax. Returns the signum function of this |
| public FastMoney | stripTrailingZeros()
Implements javax. Returns a |
| public FastMoney | subtract(MonetaryAmount subtrahend)
Implements javax. Returns a |
| public String | |
| public FastMoney | with(MonetaryOperator operator)
Overrides default javax. Returns an operated object of the same type as this object with the operation made. |
| public static FastMoney | Returns: an instance ofFastMoney representing zero.the target currency currency)Obtains an instance of |