public final class FastMoney extends AbstractMoney implements java.lang.Comparable<javax.money.MonetaryAmount>, java.io.Serializable
MonetaryAmount. This class internally uses a
single long number as numeric representation, which basically is interpreted as minor units.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(EURO, 1234567.3444)); money1 = money1.subtract(Money.of(EURO, 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.
whrereas
FastMoney money1 = money1.add(FastMoney.of(EURO, 1234567.3444)); money1 = money1.subtract(FastMoney.of(EURO, 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 my drastically change the performance behavior. E.g. replacing the
code above with the following: *
FastMoney money1 = money1.add(Money.of(EURO, 1234567.3444)); money1 = money1.subtract(FastMoney.of(EURO, 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.
currency, monetaryContext| Modifier and Type | Method and Description |
|---|---|
FastMoney |
abs()
Returns a
MonetaryAmount whose value is the absolute value of this
MonetaryAmount, and whose scale is this.scale(). |
FastMoney |
add(javax.money.MonetaryAmount amount)
Returns a
MonetaryAmount whose value is (this +
amount), and whose scale is max(this.scale(),
amount.scale()). |
int |
compareTo(javax.money.MonetaryAmount o) |
FastMoney |
divide(double number)
Returns a
MonetaryAmount whose value is (this /
divisor), and whose preferred scale is (this.scale() -
divisor.scale()); if the exact quotient cannot be represented an ArithmeticException
is thrown. |
FastMoney |
divide(long amount)
Returns a
MonetaryAmount whose value is (this /
divisor), and whose preferred scale is (this.scale() -
divisor.scale()); if the exact quotient cannot be represented an ArithmeticException
is thrown. |
FastMoney |
divide(java.lang.Number divisor)
Returns a
MonetaryAmount whose value is (this /
divisor), and whose preferred scale is (this.scale() -
divisor.scale()); if the exact quotient cannot be represented an ArithmeticException
is thrown. |
FastMoney[] |
divideAndRemainder(double amount)
Returns a two-element
MonetaryAmount array containing the result of
divideToIntegralValue followed by the result of remainder on the two
operands. |
FastMoney[] |
divideAndRemainder(long amount)
Returns a two-element
MonetaryAmount array containing the result of
divideToIntegralValue followed by the result of remainder on the two
operands. |
FastMoney[] |
divideAndRemainder(java.lang.Number divisor)
Returns a two-element
MonetaryAmount array containing the result of
divideToIntegralValue followed by the result of remainder on the two
operands. |
FastMoney |
divideToIntegralValue(double divisor)
Returns a
MonetaryAmount whose value is the integer part of the quotient
(this / divisor) rounded down. |
FastMoney |
divideToIntegralValue(long divisor)
Returns a
MonetaryAmount whose value is the integer part of the quotient
(this / divisor) rounded down. |
FastMoney |
divideToIntegralValue(java.lang.Number divisor)
Returns a
MonetaryAmount whose value is the integer part of the quotient
(this / divisor) rounded down. |
boolean |
equals(java.lang.Object obj) |
static FastMoney |
from(javax.money.MonetaryAmount amount) |
javax.money.CurrencyUnit |
getCurrency()
Returns the amount’s currency, modelled as
CurrencyUnit. |
protected javax.money.MonetaryContext |
getDefaultMonetaryContext()
Method to be implemented by superclasses to provide the default
MonetaryContext, when not explicit MonetaryContext is
available. |
javax.money.MonetaryAmountFactory<FastMoney> |
getFactory()
Creates a new
MonetaryAmountFactory, returning the same implementation type Hereby
this given amount is used as a template, so reusing the CurrencyUnit, its numeric
value, the algorithmic implementation as well as the current MonetaryContext. |
javax.money.MonetaryContext |
getMonetaryContext()
Access the
MonetaryContext used by this instance. |
javax.money.NumberValue |
getNumber()
Gets the number representation of the numeric value of this item.
|
int |
getPrecision() |
int |
getScale() |
int |
hashCode() |
boolean |
hasSameNumberAs(java.lang.Number number) |
boolean |
isEqualTo(javax.money.MonetaryAmount amount)
Compares two instances of
MonetaryAmount, hereby ignoring non significant trailing
zeroes and different numeric capabilities. |
boolean |
isGreaterThan(javax.money.MonetaryAmount amount)
Compares two instances of
MonetaryAmount, hereby ignoring non significant trailing
zeroes and different numeric capabilities. |
boolean |
isGreaterThan(java.lang.Number number) |
boolean |
isGreaterThanOrEqualTo(javax.money.MonetaryAmount amount)
Compares two instances of
MonetaryAmount, hereby ignoring non significant trailing
zeroes and different numeric capabilities. |
boolean |
isGreaterThanOrEqualTo(java.lang.Number number) |
boolean |
isLessThan(javax.money.MonetaryAmount amount)
Compares two instances of
MonetaryAmount, hereby ignoring non significant trailing
zeroes and different numeric capabilities. |
boolean |
isLessThan(java.lang.Number number) |
boolean |
isLessThanOrEqualTo(javax.money.MonetaryAmount amount)
Compares two instances of
MonetaryAmount, hereby ignoring non significant trailing
zeroes and different numeric capabilities. |
boolean |
isLessThanOrEqualTo(java.lang.Number number) |
boolean |
isNegative()
Checks if a
MonetaryAmount is negative. |
boolean |
isNegativeOrZero()
Checks if a
MonetaryAmount is negative or zero. |
boolean |
isNotEqualTo(javax.money.MonetaryAmount amount) |
boolean |
isNotEqualTo(java.lang.Number number) |
boolean |
isPositive()
Checks if a
MonetaryAmount is positive. |
boolean |
isPositiveOrZero()
Checks if a
MonetaryAmount is positive or zero. |
boolean |
isZero()
Checks if an
MonetaryAmount is zero. |
FastMoney |
multiply(double amount)
Returns a
MonetaryAmount whose value is (this ×
multiplicand), and whose scale is (this.scale() +
multiplicand.scale()). |
FastMoney |
multiply(long multiplicand)
Returns a
MonetaryAmount whose value is (this ×
multiplicand), and whose scale is (this.scale() +
multiplicand.scale()). |
FastMoney |
multiply(java.lang.Number multiplicand)
Returns a
MonetaryAmount whose value is (this ×
multiplicand), and whose scale is (this.scale() +
multiplicand.scale()). |
FastMoney |
negate()
Returns a
MonetaryAmount whose value is (-this), and whose scale is
this.scale(). |
static FastMoney |
of(javax.money.CurrencyUnit currency,
java.lang.Number number)
Static factory method for creating a new instance of
FastMoney. |
static FastMoney |
of(javax.money.CurrencyUnit currency,
javax.money.NumberValue numberBinding)
Static factory method for creating a new instance of
FastMoney. |
static FastMoney |
of(java.lang.String currencyCode,
java.lang.Number number)
Static factory method for creating a new instance of
FastMoney. |
FastMoney |
plus()
Returns a
MonetaryAmount whose value is (+this), with rounding according to
the context settings. |
<R> R |
query(javax.money.MonetaryQuery<R> query)
Queries this monetary amount for a value.
|
FastMoney |
remainder(double amount)
Returns a
MonetaryAmount whose value is (this % divisor). |
FastMoney |
remainder(long number)
Returns a
MonetaryAmount whose value is (this % divisor). |
FastMoney |
remainder(java.lang.Number divisor)
Returns a
MonetaryAmount whose value is (this % divisor). |
FastMoney |
scaleByPowerOfTen(int n)
Returns a
MonetaryAmount whose numerical value is equal to ( this *
10n). |
int |
signum()
Returns the signum function of this
MonetaryAmount. |
FastMoney |
stripTrailingZeros()
Returns a
MonetaryAmount which is numerically equal to this one but with any trailing
zeros removed from the representation. |
FastMoney |
subtract(javax.money.MonetaryAmount subtrahend)
Returns a
MonetaryAmount whose value is (this -
amount), and whose scale is max(this.scale(),
subtrahend.scale()). |
java.lang.String |
toString() |
FastMoney |
with(javax.money.MonetaryOperator operator)
Returns an operated object of the same type as this object with the operation made.
|
checkAmountParameter, checkNumberParameter, getBigDecimal, getBigDecimal, getBigDecimal, getBigDecimal, getMathContextpublic FastMoney abs()
javax.money.MonetaryAmountMonetaryAmount whose value is the absolute value of this
MonetaryAmount, and whose scale is this.scale().abs in interface javax.money.MonetaryAmountabs(this)public FastMoney add(javax.money.MonetaryAmount amount)
javax.money.MonetaryAmountMonetaryAmount whose value is (this +
amount), and whose scale is max(this.scale(),
amount.scale()).add in interface javax.money.MonetaryAmountamount - value to be added to this MonetaryAmount.this + amountpublic int compareTo(javax.money.MonetaryAmount o)
compareTo in interface java.lang.Comparable<javax.money.MonetaryAmount>public FastMoney divide(double number)
javax.money.MonetaryAmountMonetaryAmount whose value is (this /
divisor), and whose preferred scale is (this.scale() -
divisor.scale()); if the exact quotient cannot be represented an ArithmeticException
is thrown.divide in interface javax.money.MonetaryAmountnumber - value by which this MonetaryAmount is to be divided.this / divisorpublic FastMoney divide(long amount)
javax.money.MonetaryAmountMonetaryAmount whose value is (this /
divisor), and whose preferred scale is (this.scale() -
divisor.scale()); if the exact quotient cannot be represented an ArithmeticException
is thrown.divide in interface javax.money.MonetaryAmountamount - value by which this MonetaryAmount is to be divided.this / divisorpublic FastMoney divide(java.lang.Number divisor)
javax.money.MonetaryAmountMonetaryAmount whose value is (this /
divisor), and whose preferred scale is (this.scale() -
divisor.scale()); if the exact quotient cannot be represented an ArithmeticException
is thrown.divide in interface javax.money.MonetaryAmountdivisor - value by which this MonetaryAmount is to be divided.this / divisorpublic FastMoney[] divideAndRemainder(double amount)
javax.money.MonetaryAmountMonetaryAmount array containing the result of
divideToIntegralValue followed by the result of remainder on the two
operands.
Note that if both the integer quotient and remainder are needed, this method is faster than
using the divideToIntegralValue and remainder methods separately because the
division need only be carried out once.
divideAndRemainder in interface javax.money.MonetaryAmountamount - value by which this MonetaryAmount is to be divided, and the remainder
computed.MonetaryAmount array: the quotient (the result of
divideToIntegralValue) is the initial element and the remainder is the final
element.MonetaryAmount.divideToIntegralValue(double),
MonetaryAmount.remainder(double)public FastMoney[] divideAndRemainder(long amount)
javax.money.MonetaryAmountMonetaryAmount array containing the result of
divideToIntegralValue followed by the result of remainder on the two
operands.
Note that if both the integer quotient and remainder are needed, this method is faster than
using the divideToIntegralValue and remainder methods separately because the
division need only be carried out once.
divideAndRemainder in interface javax.money.MonetaryAmountamount - value by which this MonetaryAmount is to be divided, and the remainder
computed.MonetaryAmount array: the quotient (the result of
divideToIntegralValue) is the initial element and the remainder is the final
element.MonetaryAmount.divideToIntegralValue(long),
MonetaryAmount.remainder(long)public FastMoney[] divideAndRemainder(java.lang.Number divisor)
javax.money.MonetaryAmountMonetaryAmount array containing the result of
divideToIntegralValue followed by the result of remainder on the two
operands.
Note that if both the integer quotient and remainder are needed, this method is faster than
using the divideToIntegralValue and remainder methods separately because the
division need only be carried out once.
divideAndRemainder in interface javax.money.MonetaryAmountdivisor - value by which this MonetaryAmount is to be divided, and the remainder
computed.MonetaryAmount array: the quotient (the result of
divideToIntegralValue) is the initial element and the remainder is the final
element.MonetaryAmount.divideToIntegralValue(Number),
MonetaryAmount.remainder(Number)public FastMoney divideToIntegralValue(double divisor)
javax.money.MonetaryAmountMonetaryAmount whose value is the integer part of the quotient
(this / divisor) rounded down. The preferred scale of the result is
(this.scale() -
divisor.scale()).divideToIntegralValue in interface javax.money.MonetaryAmountdivisor - value by which this BigDecimal is to be divided.this / divisor.BigDecimal.divideToIntegralValue(java.math.BigDecimal)public FastMoney divideToIntegralValue(long divisor)
javax.money.MonetaryAmountMonetaryAmount whose value is the integer part of the quotient
(this / divisor) rounded down. The preferred scale of the result is
(this.scale() -
divisor.scale()).divideToIntegralValue in interface javax.money.MonetaryAmountdivisor - value by which this BigDecimal is to be divided.this / divisor.BigDecimal.divideToIntegralValue(java.math.BigDecimal)public FastMoney divideToIntegralValue(java.lang.Number divisor)
javax.money.MonetaryAmountMonetaryAmount whose value is the integer part of the quotient
(this / divisor) rounded down. The preferred scale of the result is
(this.scale() -
divisor.scale()).divideToIntegralValue in interface javax.money.MonetaryAmountdivisor - value by which this BigDecimal is to be divided.this / divisor.BigDecimal.divideToIntegralValue(java.math.BigDecimal)public boolean equals(java.lang.Object obj)
equals in class java.lang.Objectpublic static FastMoney from(javax.money.MonetaryAmount amount)
public javax.money.CurrencyUnit getCurrency()
AbstractMoneyCurrencyUnit.
Implementations may co-variantly change the return type to a more
specific implementation of CurrencyUnit if desired.getCurrency in interface javax.money.CurrencySuppliergetCurrency in interface javax.money.MonetaryAmountgetCurrency in class AbstractMoneynullMonetaryAmount.getCurrency()protected javax.money.MonetaryContext getDefaultMonetaryContext()
AbstractMoneyMonetaryContext, when not explicit MonetaryContext is
available.getDefaultMonetaryContext in class AbstractMoneyMonetaryContext, never null.public javax.money.MonetaryAmountFactory<FastMoney> getFactory()
javax.money.MonetaryAmountMonetaryAmountFactory, returning the same implementation type Hereby
this given amount is used as a template, so reusing the CurrencyUnit, its numeric
value, the algorithmic implementation as well as the current MonetaryContext.
This method is used for creating a new amount result after having done calculations that are not directly mappable to the default monetary arithmetics, e.g. currency conversion.
getFactory in interface javax.money.MonetaryAmountMonetaryAmountFactory with the given MonetaryAmount as its
default values.public javax.money.MonetaryContext getMonetaryContext()
AbstractMoneyMonetaryContext used by this instance.getMonetaryContext in interface javax.money.MonetaryAmountgetMonetaryContext in class AbstractMoneyMonetaryContext used, never null.MonetaryAmount.getMonetaryContext()public javax.money.NumberValue getNumber()
getNumber in interface javax.money.MonetaryAmountgetNumber in interface javax.money.NumberSupplierNumber represention matching best.public int getPrecision()
public int getScale()
public int hashCode()
hashCode in class java.lang.Objectpublic boolean hasSameNumberAs(java.lang.Number number)
public boolean isEqualTo(javax.money.MonetaryAmount amount)
javax.money.MonetaryAmountMonetaryAmount, hereby ignoring non significant trailing
zeroes and different numeric capabilities.isEqualTo in interface javax.money.MonetaryAmountamount - the MonetaryAmount to be compared with this instance.true if amount == this.public boolean isGreaterThan(javax.money.MonetaryAmount amount)
javax.money.MonetaryAmountMonetaryAmount, hereby ignoring non significant trailing
zeroes and different numeric capabilities.isGreaterThan in interface javax.money.MonetaryAmountamount - the MonetaryAmount to be compared with this instance.true if amount > this.public boolean isGreaterThan(java.lang.Number number)
public boolean isGreaterThanOrEqualTo(javax.money.MonetaryAmount amount)
javax.money.MonetaryAmountMonetaryAmount, hereby ignoring non significant trailing
zeroes and different numeric capabilities.isGreaterThanOrEqualTo in interface javax.money.MonetaryAmountamount - the MonetaryAmount to be compared with this instance.true if amount >= this.public boolean isGreaterThanOrEqualTo(java.lang.Number number)
public boolean isLessThan(javax.money.MonetaryAmount amount)
javax.money.MonetaryAmountMonetaryAmount, hereby ignoring non significant trailing
zeroes and different numeric capabilities.isLessThan in interface javax.money.MonetaryAmountamount - the MonetaryAmount to be compared with this instance.true if amount < this.public boolean isLessThan(java.lang.Number number)
public boolean isLessThanOrEqualTo(javax.money.MonetaryAmount amount)
javax.money.MonetaryAmountMonetaryAmount, hereby ignoring non significant trailing
zeroes and different numeric capabilities.isLessThanOrEqualTo in interface javax.money.MonetaryAmountamount - the MonetaryAmount to be compared with this instance.true if amount <= this.public boolean isLessThanOrEqualTo(java.lang.Number number)
public boolean isNegative()
javax.money.MonetaryAmountMonetaryAmount is negative.isNegative in interface javax.money.MonetaryAmounttrue if MonetaryAmount.signum() < 0.public boolean isNegativeOrZero()
javax.money.MonetaryAmountMonetaryAmount is negative or zero.isNegativeOrZero in interface javax.money.MonetaryAmounttrue if MonetaryAmount.signum() <= 0.public boolean isNotEqualTo(javax.money.MonetaryAmount amount)
public boolean isNotEqualTo(java.lang.Number number)
public boolean isPositive()
javax.money.MonetaryAmountMonetaryAmount is positive.isPositive in interface javax.money.MonetaryAmounttrue if MonetaryAmount.signum() > 0.public boolean isPositiveOrZero()
javax.money.MonetaryAmountMonetaryAmount is positive or zero.isPositiveOrZero in interface javax.money.MonetaryAmounttrue if MonetaryAmount.signum() >= 0.public boolean isZero()
javax.money.MonetaryAmountMonetaryAmount is zero.isZero in interface javax.money.MonetaryAmounttrue if MonetaryAmount.signum() == 0.public FastMoney multiply(double amount)
javax.money.MonetaryAmountMonetaryAmount whose value is (this ×
multiplicand), and whose scale is (this.scale() +
multiplicand.scale()).multiply in interface javax.money.MonetaryAmountamount - value to be multiplied by this MonetaryAmount.this * multiplicandpublic FastMoney multiply(long multiplicand)
javax.money.MonetaryAmountMonetaryAmount whose value is (this ×
multiplicand), and whose scale is (this.scale() +
multiplicand.scale()).multiply in interface javax.money.MonetaryAmountmultiplicand - value to be multiplied by this MonetaryAmount.this * multiplicandpublic FastMoney multiply(java.lang.Number multiplicand)
javax.money.MonetaryAmountMonetaryAmount whose value is (this ×
multiplicand), and whose scale is (this.scale() +
multiplicand.scale()).multiply in interface javax.money.MonetaryAmountmultiplicand - value to be multiplied by this MonetaryAmount.this * multiplicandpublic FastMoney negate()
javax.money.MonetaryAmountMonetaryAmount whose value is (-this), and whose scale is
this.scale().negate in interface javax.money.MonetaryAmount-this.public static FastMoney of(javax.money.CurrencyUnit currency, java.lang.Number number)
FastMoney.currency - The target currency, not null.number - The numeric part, not null.FastMoney.public static FastMoney of(javax.money.CurrencyUnit currency, javax.money.NumberValue numberBinding)
FastMoney.currency - The target currency, not null.numberBinding - The numeric part, not null.FastMoney.public static FastMoney of(java.lang.String currencyCode, java.lang.Number number)
FastMoney.currencyCode - The target currency as currency code.number - The numeric part, not null.FastMoney.public FastMoney plus()
javax.money.MonetaryAmountMonetaryAmount whose value is (+this), with rounding according to
the context settings.plus in interface javax.money.MonetaryAmountthis, rounded as necessary. A zero result will have a scale of 0.BigDecimal.plus()public <R> R query(javax.money.MonetaryQuery<R> query)
javax.money.MonetaryAmountThis queries this amount using the specified query strategy object.
Implementations must ensure that no observable state is altered when this read-only method is invoked.
query in interface javax.money.MonetaryAmountR - the type of the resultquery - the query to invoke, not nullpublic FastMoney remainder(double amount)
javax.money.MonetaryAmountMonetaryAmount whose value is (this % divisor).
The remainder is given by
this.subtract(this.divideToIntegralValue(divisor).multiply(divisor)) . Note that this
is not the modulo operation (the result can be negative).
remainder in interface javax.money.MonetaryAmountamount - value by which this MonetaryAmount is to be divided.this % divisor.public FastMoney remainder(long number)
javax.money.MonetaryAmountMonetaryAmount whose value is (this % divisor).
The remainder is given by
this.subtract(this.divideToIntegralValue(divisor).multiply(divisor)) . Note that this
is not the modulo operation (the result can be negative).
remainder in interface javax.money.MonetaryAmountnumber - value by which this MonetaryAmount is to be divided.this % divisor.public FastMoney remainder(java.lang.Number divisor)
javax.money.MonetaryAmountMonetaryAmount whose value is (this % divisor).
The remainder is given by
this.subtract(this.divideToIntegralValue(divisor).multiply(divisor)) . Note that this
is not the modulo operation (the result can be negative).
remainder in interface javax.money.MonetaryAmountdivisor - value by which this MonetaryAmount is to be divided.this % divisor.public FastMoney scaleByPowerOfTen(int n)
javax.money.MonetaryAmountMonetaryAmount whose numerical value is equal to ( this *
10n). The scale of the result is (this.scale() - n).scaleByPowerOfTen in interface javax.money.MonetaryAmountn - the power.public int signum()
javax.money.MonetaryAmountMonetaryAmount.signum in interface javax.money.MonetaryAmountMonetaryAmount is negative, zero, or
positive.public FastMoney stripTrailingZeros()
javax.money.MonetaryAmountMonetaryAmount which is numerically equal to this one but with any trailing
zeros removed from the representation. For example, stripping the trailing zeros from the
MonetaryAmount value CHF 600.0, which has [BigInteger, scale]
components equals to [6000, 1], yields 6E2 with [ BigInteger, scale]
components equals to [6, -2]stripTrailingZeros in interface javax.money.MonetaryAmountMonetaryAmount with any trailing zeros removed.public FastMoney subtract(javax.money.MonetaryAmount subtrahend)
javax.money.MonetaryAmountMonetaryAmount whose value is (this -
amount), and whose scale is max(this.scale(),
subtrahend.scale()).subtract in interface javax.money.MonetaryAmountsubtrahend - value to be subtracted from this MonetaryAmount.this - amountpublic java.lang.String toString()
toString in class java.lang.Objectpublic FastMoney with(javax.money.MonetaryOperator operator)
javax.money.MonetaryAmountMonetaryAmountFactory instances: // converting from Money to MyMoney Money m = ...; MonetartyAmountFactoryf = MonetaryAmounts.getAmountFactory(MyMoney.class); MyMoney myMoney = f.setAmount(m).create();
This converts this monetary amount according to the rules of the specified operator. A typical operator will change the amount and leave the currency unchanged. A more complex operator might also change the currency.
Some example code indicating how and why this method is used:
Hereby also the method signature on the implementation type must return the concrete type, to enable a fluent API, e.g.MonetaryAmount money = money.with(amountMultipliedBy(2)); money = money.with(amountRoundedToNearestWholeUnit());
public final class MyMoney implements MonetaryAmount{
...
public MyMoney with(MonetaryOperator operator){
...
}
...
}
with in interface javax.money.MonetaryAmountoperator - the operator to use, not null