public final class FastMoney extends Object implements MonetaryAmount, Comparable<MonetaryAmount>, Serializable
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("EUR", 1234567.3444));
money1 = money1.subtract(Money.of("EUR", 232323));
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("EUR", 1234567.3444));
money1 = money1.subtract(FastMoney.of("EUR", 232323));
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("EUR", 1234567.3444));
money1 = money1.subtract(FastMoney.of("EUR", 232323));
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 |
|---|---|
static FastMoney |
MAX_VALUE
Maximum possible value supported, using XX (no currency).
|
static FastMoney |
MIN_VALUE
Minimum possible value supported, using XX (no currency).
|
| Modifier and Type | Method and Description |
|---|---|
FastMoney |
abs() |
FastMoney |
add(MonetaryAmount amount) |
protected void |
checkNumber(Number number)
Internal method to check for correct number parameter.
|
int |
compareTo(MonetaryAmount o) |
FastMoney |
divide(double divisor) |
FastMoney |
divide(long divisor) |
FastMoney |
divide(Number divisor) |
FastMoney[] |
divideAndRemainder(double divisor) |
FastMoney[] |
divideAndRemainder(long divisor) |
FastMoney[] |
divideAndRemainder(Number divisor) |
FastMoney |
divideToIntegralValue(double divisor) |
FastMoney |
divideToIntegralValue(long divisor) |
FastMoney |
divideToIntegralValue(Number divisor) |
boolean |
equals(Object obj) |
static FastMoney |
from(MonetaryAmount amount) |
MonetaryContext |
getContext()
Access the
MonetaryContext used by this instance. |
CurrencyUnit |
getCurrency()
Returns the amount’s currency, modelled as
CurrencyUnit. |
MonetaryAmountFactory<FastMoney> |
getFactory() |
NumberValue |
getNumber()
Gets the number representation of the numeric value of this item.
|
int |
getPrecision() |
int |
getScale() |
int |
hashCode() |
boolean |
hasSameNumberAs(Number number) |
boolean |
isEqualTo(MonetaryAmount amount) |
boolean |
isGreaterThan(MonetaryAmount amount) |
boolean |
isGreaterThan(Number number) |
boolean |
isGreaterThanOrEqualTo(MonetaryAmount amount) |
boolean |
isGreaterThanOrEqualTo(Number number) |
boolean |
isLessThan(MonetaryAmount amount) |
boolean |
isLessThan(Number number) |
boolean |
isLessThanOrEqualTo(MonetaryAmount amount) |
boolean |
isLessThanOrEqualTo(Number number) |
boolean |
isNegative() |
boolean |
isNegativeOrZero() |
boolean |
isPositive() |
boolean |
isPositiveOrZero() |
boolean |
isZero() |
FastMoney |
multiply(double multiplicand) |
FastMoney |
multiply(long multiplicand) |
FastMoney |
multiply(Number multiplicand) |
FastMoney |
negate() |
static FastMoney |
of(Number number,
CurrencyUnit currency)
Static factory method for creating a new instance of
FastMoney. |
static FastMoney |
of(Number number,
String currencyCode)
Static factory method for creating a new instance of
FastMoney. |
static FastMoney |
of(NumberValue numberBinding,
CurrencyUnit currency)
Static factory method for creating a new instance of
FastMoney. |
static FastMoney |
ofMinor(CurrencyUnit currency,
long amountMinor)
Obtains an instance of
FastMoney from an amount in minor units. |
static FastMoney |
ofMinor(CurrencyUnit currency,
long amountMinor,
int factionDigits)
Obtains an instance of
FastMoney from an amount in minor units. |
static FastMoney |
parse(CharSequence text)
Obtains an instance of FastMoney from a text string such as 'EUR 25.25'.
|
static FastMoney |
parse(CharSequence text,
MonetaryAmountFormat formatter)
Obtains an instance of FastMoney from a text using specific formatter.
|
FastMoney |
plus() |
<R> R |
query(MonetaryQuery<R> query) |
FastMoney |
remainder(double divisor) |
FastMoney |
remainder(long divisor) |
FastMoney |
remainder(Number divisor) |
FastMoney |
scaleByPowerOfTen(int n) |
int |
signum() |
FastMoney |
stripTrailingZeros() |
FastMoney |
subtract(MonetaryAmount subtrahend) |
String |
toString() |
FastMoney |
with(MonetaryOperator operator) |
static FastMoney |
zero(CurrencyUnit currency)
Obtains an instance of
FastMoney representing zero. |
public static final FastMoney MAX_VALUE
public CurrencyUnit getCurrency()
CurrencyUnit.
Implementations may co-variantly change the return type to a more
specific implementation of CurrencyUnit if desired.getCurrency in interface CurrencySuppliernullCurrencySupplier.getCurrency()public MonetaryContext getContext()
MonetaryContext used by this instance.getContext in interface MonetaryAmountMonetaryContext used, never null.MonetaryAmount.getContext()public static FastMoney of(NumberValue numberBinding, CurrencyUnit currency)
FastMoney.currency - The target currency, not null.numberBinding - The numeric part, not null.FastMoney.public static FastMoney of(Number number, CurrencyUnit currency)
FastMoney.currency - The target currency, not null.number - The numeric part, not null.FastMoney.public static FastMoney of(Number number, String currencyCode)
FastMoney.currencyCode - The target currency as currency code.number - The numeric part, not null.FastMoney.public static FastMoney zero(CurrencyUnit currency)
FastMoney representing zero.currency - the target currencyFastMoney representing zero.public static FastMoney ofMinor(CurrencyUnit currency, long amountMinor)
FastMoney from an amount in minor units.
For example, ofMinor(USD, 1234) creates the instance USD 12.34.currency - the currency, not nullamountMinor - the amount of money in the minor division of the currencyNullPointerException - when the currency is nullIllegalArgumentException - when CurrencyUnit.getDefaultFractionDigits() is lesser than zero.CurrencyUnit.getDefaultFractionDigits(),
ofMinor(CurrencyUnit, long, int)public static FastMoney ofMinor(CurrencyUnit currency, long amountMinor, int factionDigits)
FastMoney from an amount in minor units.
For example, ofMinor(USD, 1234, 2) creates the instance USD 12.34.currency - the currency, not nullamountMinor - the amount of money in the minor division of the currencyfactionDigits - number of digitsNullPointerException - when the currency is nullIllegalArgumentException - when the factionDigits is negativeCurrencyUnit.getDefaultFractionDigits()public int compareTo(MonetaryAmount o)
compareTo in interface Comparable<MonetaryAmount>public FastMoney abs()
abs in interface MonetaryAmountpublic FastMoney add(MonetaryAmount amount)
add in interface MonetaryAmountpublic FastMoney divide(Number divisor)
divide in interface MonetaryAmountpublic FastMoney[] divideAndRemainder(Number divisor)
divideAndRemainder in interface MonetaryAmountpublic FastMoney divideToIntegralValue(Number divisor)
divideToIntegralValue in interface MonetaryAmountpublic FastMoney multiply(Number multiplicand)
multiply in interface MonetaryAmountpublic FastMoney negate()
negate in interface MonetaryAmountpublic FastMoney plus()
plus in interface MonetaryAmountpublic FastMoney subtract(MonetaryAmount subtrahend)
subtract in interface MonetaryAmountpublic FastMoney remainder(Number divisor)
remainder in interface MonetaryAmountpublic FastMoney scaleByPowerOfTen(int n)
scaleByPowerOfTen in interface MonetaryAmountpublic boolean isZero()
isZero in interface MonetaryAmountpublic boolean isPositive()
isPositive in interface MonetaryAmountpublic boolean isPositiveOrZero()
isPositiveOrZero in interface MonetaryAmountpublic boolean isNegative()
isNegative in interface MonetaryAmountpublic boolean isNegativeOrZero()
isNegativeOrZero in interface MonetaryAmountpublic int getScale()
public int getPrecision()
public int signum()
signum in interface MonetaryAmountpublic boolean isLessThan(MonetaryAmount amount)
isLessThan in interface MonetaryAmountpublic boolean isLessThan(Number number)
public boolean isLessThanOrEqualTo(MonetaryAmount amount)
isLessThanOrEqualTo in interface MonetaryAmountpublic boolean isLessThanOrEqualTo(Number number)
public boolean isGreaterThan(MonetaryAmount amount)
isGreaterThan in interface MonetaryAmountpublic boolean isGreaterThan(Number number)
public boolean isGreaterThanOrEqualTo(MonetaryAmount amount)
isGreaterThanOrEqualTo in interface MonetaryAmountpublic boolean isGreaterThanOrEqualTo(Number number)
public boolean isEqualTo(MonetaryAmount amount)
isEqualTo in interface MonetaryAmountpublic boolean hasSameNumberAs(Number number)
public NumberValue getNumber()
getNumber in interface NumberSupplierNumber represention matching best.protected void checkNumber(Number number)
number - the number to be checked, including null..NullPointerException - If the number is nullArithmeticException - If the number exceeds the capabilities of this class.public FastMoney with(MonetaryOperator operator)
with in interface MonetaryAmountpublic <R> R query(MonetaryQuery<R> query)
query in interface MonetaryAmountpublic static FastMoney from(MonetaryAmount amount)
public static FastMoney parse(CharSequence text)
text - the text to parse not nullNumberFormatException - if the amount is not a numberUnknownCurrencyException - if the currency is not resolvablepublic static FastMoney parse(CharSequence text, MonetaryAmountFormat formatter)
text - the text to parse not nullformatter - the formatter to use not nullpublic FastMoney multiply(double multiplicand)
multiply in interface MonetaryAmountpublic FastMoney divide(long divisor)
divide in interface MonetaryAmountpublic FastMoney divide(double divisor)
divide in interface MonetaryAmountpublic FastMoney remainder(long divisor)
remainder in interface MonetaryAmountpublic FastMoney remainder(double divisor)
remainder in interface MonetaryAmountpublic FastMoney[] divideAndRemainder(long divisor)
divideAndRemainder in interface MonetaryAmountpublic FastMoney[] divideAndRemainder(double divisor)
divideAndRemainder in interface MonetaryAmountpublic FastMoney stripTrailingZeros()
stripTrailingZeros in interface MonetaryAmountpublic FastMoney multiply(long multiplicand)
multiply in interface MonetaryAmountpublic FastMoney divideToIntegralValue(long divisor)
divideToIntegralValue in interface MonetaryAmountpublic FastMoney divideToIntegralValue(double divisor)
divideToIntegralValue in interface MonetaryAmountpublic MonetaryAmountFactory<FastMoney> getFactory()
getFactory in interface MonetaryAmountCopyright © 2012–2020 JavaMoney. All rights reserved.