Top Description Fields Constructors Methods
org.javamoney.moneta

public final Class FastMoney

extends Object
implements MonetaryAmount, Comparable<MonetaryAmount>, Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable, java.lang.Comparable, javax.money.MonetaryAmount, javax.money.NumberSupplier, javax.money.CurrencySupplier
Imports
org.javamoney.moneta.format.ToStringMonetaryAmountFormat, .ToStringMonetaryAmountFormat.ToStringMonetaryAmountFormatStyle, .MonetaryAmountDecimalFormat, org.javamoney.moneta.spi.FastMoneyAmountFactory, .DefaultNumberValue, .MonetaryConfig, .MoneyUtils, javax.money.*, javax.money.format.MonetaryAmountFormat, java.io.Serializable, java.math.BigDecimal, java.util.Objects, java.util.logging.Level, .Logger

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.

Authors
Anatole Tresch, Werner Keil
Since
1.0
Version
1.2

Field Summary

Modifier and TypeField 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 MonetaryContext used by this instance, e.g. on division.

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

Constructor Summary

AccessConstructor and Description
private
FastMoney(Number
the amount, not null.
number
,
CurrencyUnit
the currency, not null.
currency
)

Creates a new instance of FastMoney.

private
FastMoney(NumberValue
the numeric value, not null.
numberValue
,
CurrencyUnit
the currency, not null.
currency
)

Creates a new instance of FastMoney.

private
FastMoney(long
The format number value
number
,
CurrencyUnit
the currency, not null.
currency
)

Creates a new instance of FastMoney.

Method Summary

Modifier and TypeMethod and Description
public FastMoney
abs()

Implements javax.money.MonetaryAmount.abs.

Returns a MonetaryAmount whose value is the absolute value of this MonetaryAmount, and whose scale is this.getContext().getMaxScale().

public FastMoney
add(MonetaryAmount amount)

Implements javax.money.MonetaryAmount.add.

Returns a MonetaryAmount whose value is this + augend, and whose scale is max(this.getContext().getMaxScale(), augend.getContext().getMaxScale()).

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.lang.Comparable.compareTo.

Compares this object with the specified object for order.

private static MonetaryAmountFormat
public FastMoney
divide(Number divisor)

Implements javax.money.MonetaryAmount.divide.

Returns a MonetaryAmount whose value is this / divisor, and whose preferred scale is this.getContext().getMaxScale() - divisor.getContext().getMaxScale(); if the exact quotient cannot be represented an ArithmeticException is thrown.

public FastMoney
divide(long divisor)

Implements javax.money.MonetaryAmount.divide.

Returns a MonetaryAmount whose value is this / divisor, and whose preferred scale is this.getContext().getMaxScale() - divisor.getContext().getMaxScale(); if the exact quotient cannot be represented an ArithmeticException is thrown.

public FastMoney
divide(double divisor)

Implements javax.money.MonetaryAmount.divide.

Returns a MonetaryAmount whose value is this / divisor, and whose preferred scale is this.getContext().getMaxScale() - divisor.getContext().getMaxScale(); if the exact quotient cannot be represented an ArithmeticException is thrown.

public FastMoney[]
divideAndRemainder(Number divisor)

Implements javax.money.MonetaryAmount.divideAndRemainder.

Returns a two-element MonetaryAmount array containing the result of divideToIntegralValue followed by the result of remainder on the two operands.

public FastMoney[]
divideAndRemainder(long divisor)

Implements javax.money.MonetaryAmount.divideAndRemainder.

Returns a two-element MonetaryAmount array containing the result of divideToIntegralValue followed by the result of remainder on the two operands.

public FastMoney[]
divideAndRemainder(double divisor)

Implements javax.money.MonetaryAmount.divideAndRemainder.

Returns a two-element MonetaryAmount array containing the result of divideToIntegralValue followed by the result of remainder on the two operands.

public FastMoney
divideToIntegralValue(Number divisor)

Implements javax.money.MonetaryAmount.divideToIntegralValue.

Returns a MonetaryAmount whose value is the integer part of the quotient this / divisor rounded down.

public FastMoney
divideToIntegralValue(long divisor)

Implements javax.money.MonetaryAmount.divideToIntegralValue.

Returns a MonetaryAmount whose value is the integer part of the quotient this / divisor rounded down.

public FastMoney
divideToIntegralValue(double divisor)

Implements javax.money.MonetaryAmount.divideToIntegralValue.

Returns a MonetaryAmount whose value is the integer part of the quotient this / divisor rounded down.

public boolean
equals(Object obj)

Overrides java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

public static FastMoney
private BigDecimal
public MonetaryContext

Returns:

the MonetaryContext used, never null.
getContext
()

Implements javax.money.MonetaryAmount.getContext.

Access the MonetaryContext used by this instance.

public CurrencyUnit

Returns:

the currency, never null
getCurrency
()

Implements javax.money.CurrencySupplier.getCurrency.

Returns the amount’s currency, modelled as CurrencyUnit.

public MonetaryAmountFactory<FastMoney>
getFactory()

Implements javax.money.MonetaryAmount.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.

private long
public NumberValue

Returns:

The Number representation matching best.
getNumber
()

Implements javax.money.NumberSupplier.getNumber.

Gets the number representation of the numeric value of this item.

public int

Returns:

the precision of this RoundedMoney.
getPrecision
()

Returns the precision of this RoundedMoney.

public int

Returns:

the scale of this FastMoney.
getScale
()

Returns the scale of this FastMoney.

public int
hashCode()

Overrides java.lang.Object.hashCode.

Returns a hash code value for this object.

public boolean
public boolean
isEqualTo(MonetaryAmount amount)

Implements javax.money.MonetaryAmount.isEqualTo.

Compares two instances of MonetaryAmount, hereby ignoring non significant trailing zeroes and different numeric capabilities.

public boolean
isGreaterThan(MonetaryAmount amount)

Implements javax.money.MonetaryAmount.isGreaterThan.

Compares two instances of MonetaryAmount, hereby ignoring non significant trailing zeroes and different numeric capabilities.

public boolean
public boolean
isGreaterThanOrEqualTo(MonetaryAmount amount)

Implements javax.money.MonetaryAmount.isGreaterThanOrEqualTo.

Compares two instances of MonetaryAmount, hereby ignoring non significant trailing zeroes and different numeric capabilities.

public boolean
public boolean
isLessThan(MonetaryAmount amount)

Implements javax.money.MonetaryAmount.isLessThan.

Compares two instances of MonetaryAmount, hereby ignoring non significant trailing zeroes and different numeric capabilities.

public boolean
public boolean
isLessThanOrEqualTo(MonetaryAmount amount)

Implements javax.money.MonetaryAmount.isLessThanOrEqualTo.

Compares two instances of MonetaryAmount, hereby ignoring non significant trailing zeroes and different numeric capabilities.

public boolean
public boolean
isNegative()

Overrides default javax.money.MonetaryAmount.isNegative.

Checks if a MonetaryAmount is negative.

public boolean
isNegativeOrZero()

Overrides default javax.money.MonetaryAmount.isNegativeOrZero.

Checks if a MonetaryAmount is negative or zero.

private boolean
isOne(Number number)

public boolean
isPositive()

Overrides default javax.money.MonetaryAmount.isPositive.

Checks if a MonetaryAmount is positive.

public boolean
isPositiveOrZero()

Overrides default javax.money.MonetaryAmount.isPositiveOrZero.

Checks if a MonetaryAmount is positive or zero.

public boolean
isZero()

Overrides default javax.money.MonetaryAmount.isZero.

Checks if an MonetaryAmount is zero.

public FastMoney
multiply(Number multiplicand)

Implements javax.money.MonetaryAmount.multiply.

Returns a MonetaryAmount whose value is (this × multiplicand), and whose scale is this.getContext().getMaxScale() + multiplicand.getContext().getMaxScale().

public FastMoney
multiply(double multiplicand)

Implements javax.money.MonetaryAmount.multiply.

Returns a MonetaryAmount whose value is (this × multiplicand), and whose scale is this.getContext().getMaxScale() + multiplicand.getContext().getMaxScale().

public FastMoney
multiply(long multiplicand)

Implements javax.money.MonetaryAmount.multiply.

Returns a MonetaryAmount whose value is (this × multiplicand), and whose scale is this.getContext().getMaxScale() + multiplicand.getContext().getMaxScale().

public FastMoney
negate()

Implements javax.money.MonetaryAmount.negate.

Returns a MonetaryAmount whose value is -this, and whose scale is this.getContext().getMaxScale(), with rounding according to the context settings.

public static FastMoney

Returns:

A new instance of FastMoney.
of
(NumberValue
The numeric part, not null.
numberBinding
,
CurrencyUnit
The target currency, not null.
currency
)

Static factory method for creating a new instance of FastMoney.

public static FastMoney

Returns:

A new instance of FastMoney.
of
(Number
The numeric part, not null.
number
,
CurrencyUnit
The target currency, not null.
currency
)

Static factory method for creating a new instance of FastMoney.

public static FastMoney

Returns:

A new instance of FastMoney.
of
(Number
The numeric part, not null.
number
,
String
The target currency as currency code.
currencyCode
)

Static factory method for creating a new instance of FastMoney.

public static FastMoney

Returns:

the monetary amount from minor units
ofMinor
(CurrencyUnit
the currency, not null
currency
,
long
the amount of money in the minor division of the currency
amountMinor
)

Obtains an instance of FastMoney from an amount in minor units.

public static FastMoney

Returns:

the monetary amount from minor units
ofMinor
(CurrencyUnit
the 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 FastMoney from an amount in minor units.

public static FastMoney

Returns:

FastMoney instance
parse
(CharSequence
the 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 instance
parse
(CharSequence
the 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
plus()

Implements javax.money.MonetaryAmount.plus.

Returns a MonetaryAmount whose value is +this.

public <R> R
query(MonetaryQuery<R> query)

Overrides default javax.money.MonetaryAmount.query.

Queries this monetary amount for a value.

public FastMoney
remainder(Number divisor)

Implements javax.money.MonetaryAmount.remainder.

Returns a MonetaryAmount whose value is this % divisor.

public FastMoney
remainder(long divisor)

Implements javax.money.MonetaryAmount.remainder.

Returns a MonetaryAmount whose value is this % divisor.

public FastMoney
remainder(double divisor)

Implements javax.money.MonetaryAmount.remainder.

Returns a MonetaryAmount whose value is this % divisor.

public FastMoney
scaleByPowerOfTen(int power)

Implements javax.money.MonetaryAmount.scaleByPowerOfTen.

Returns a MonetaryAmount whose numerical value is equal to ( this * 10n).

public int
signum()

Implements javax.money.MonetaryAmount.signum.

Returns the signum function of this MonetaryAmount.

public FastMoney
stripTrailingZeros()

Implements javax.money.MonetaryAmount.stripTrailingZeros.

Returns a MonetaryAmount which is numerically equal to this one but with any trailing zeros removed from the representation.

public FastMoney
subtract(MonetaryAmount subtrahend)

Implements javax.money.MonetaryAmount.subtract.

Returns a MonetaryAmount whose value is this - amount, and whose scale is max(this.getContext().getMaxScale(), subtrahend.getContext().getMaxScale().

public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.

public FastMoney
with(MonetaryOperator operator)

Overrides default javax.money.MonetaryAmount.with.

Returns an operated object of the same type as this object with the operation made.

public static FastMoney

Returns:

an instance of FastMoney representing zero.
zero
(CurrencyUnit
the target currency
currency
)

Obtains an instance of FastMoney representing zero.

Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAllwaitwaitwait