Top Description Fields Constructors Methods
org.javamoney.moneta

public final Class Money

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.MoneyAmountFactory, .DefaultNumberValue, .MonetaryConfig, .MoneyUtils, javax.money.*, javax.money.format.MonetaryAmountFormat, java.io.Serializable, java.math.BigDecimal, .MathContext, .RoundingMode, java.util.Objects, java.util.logging.Level, .Logger

Default immutable implementation of MonetaryAmount based on BigDecimal as numeric representation.

As required by MonetaryAmount this class is final, thread-safe, immutable and serializable.

This class can be configured with an arbitrary MonetaryContext. The default MonetaryContext used models by default the same settings as MathContext#DECIMAL64 . This default MonetaryContext can also be reconfigured by adding a file /javamoney.properties to the classpath, with the following content:

# Default MathContext for Money
#-------------------------------
# Custom MonetaryContext, overrides default entries from
# org.javamoney.moneta.Money.monetaryContext
# RoundingMode hereby is optional (default = HALF_EVEN)
org.javamoney.moneta.Money.defaults.precision=256
org.javamoney.moneta.Money.defaults.roundingMode=HALF_EVEN
Authors
Anatole Tresch, Werner Keil
Since
1.0
Version
1.3

Field Summary

Modifier and TypeField and Description
private final CurrencyUnit
currency

The currency of this amount.

public static final MonetaryContext
DEFAULT_MONETARY_CONTEXT

The default MonetaryContext applied, if not set explicitly on creation.

private final MonetaryContext
monetaryContext

the MonetaryContext used by this instance, e.g. on division.

private final BigDecimal
number

The numeric part of this amount.

private static final long
serialVersionUID

serialVersionUID.

Constructor Summary

AccessConstructor and Description
private
Money(BigDecimal
the amount, not null.
number
,
CurrencyUnit
the currency, not null.
currency
)

Creates a new instance of Money.

private
Money(BigDecimal
the amount, not null.
number
,
CurrencyUnit
the currency, not null.
currency
,
MonetaryContext
the MonetaryContext, if null, the default is used.
monetaryContext
)

Creates a new instance of Money.

Method Summary

Modifier and TypeMethod and Description
public Money
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 Money
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()).

public static void
checkNoInfinityOrNaN(Number
the amount, not null.
number
)
Deprecated Will be removed. Use

Just to don't break the compatibility.

public int
compareTo(MonetaryAmount o)

Implements java.lang.Comparable.compareTo.

Compares this object with the specified object for order.

private static MonetaryAmountFormat
public Money
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 Money
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 Money
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 Money[]
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 Money[]
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 Money[]
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 Money
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 Money
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 Money
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 boolean
equals(Object obj)

Overrides java.lang.Object.equals.

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

public static Money

Returns:

an according Money instance.
from
(MonetaryAmount
the amount to be converted
amt
)

Converts (if necessary) the given MonetaryAmount to a Money instance.

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<Money>
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.

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 BigDecimal

Returns:

the stripped number value.
getNumberStripped
()

Method that returns BigDecimal.ZERO, if isZero(), and stripTrailingZeros() in all other cases.

public int

Returns:

the precision of this RoundedMoney.
getPrecision
()

Returns the precision of this RoundedMoney.

public int

Returns:

the scale of this Money.
getScale
()

Returns the scale of this Money.

public int
hashCode()

Overrides java.lang.Object.hashCode.

Returns a hash code value for this object.

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
isGreaterThanOrEqualTo(MonetaryAmount amount)

Implements javax.money.MonetaryAmount.isGreaterThanOrEqualTo.

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

public static boolean
isInfinityAndNotNaN(Number
the amount, not null
number
)
Deprecated Will be removed.

Just to don't break the compatibility.

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
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
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.

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 Money
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 Money
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 Money
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 Money
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 Money

Returns:

a Money combining the numeric value and currency unit.
of
(BigDecimal
numeric value, not null.
number
,
CurrencyUnit
currency unit, not null.
currency
)

Creates a new instance of Money, using the default MonetaryContext.

public static Money

Returns:

a Money instance based on the monetary context with the given numeric value, currency unit.
of
(BigDecimal
numeric value, not null.
number
,
CurrencyUnit
currency unit, not null.
currency
,
MonetaryContext
the MonetaryContext to be used, if null the default MonetaryContext is used.
monetaryContext
)

Creates a new instance of Money, using an explicit MonetaryContext.

public static Money

Returns:

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

Creates a new instance of Money, using the default MonetaryContext.

public static Money

Returns:

A new instance of Money.
of
(Number
The numeric part, not null.
number
,
CurrencyUnit
The target currency, not null.
currency
,
MonetaryContext
the MonetaryContext to be used, if null the default MonetaryContext is used.
monetaryContext
)

Creates a new instance of Money, using an explicit MonetaryContext.

public static Money

Returns:

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

Static factory method for creating a new instance of Money.

public static Money

Returns:

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

Static factory method for creating a new instance of Money.

public static Money

Returns:

A new instance of Money.
of
(Number
The numeric part, not null.
number
,
String
The target currency as ISO currency code.
currencyCode
,
MonetaryContext
the MonetaryContext to be used, if null the default MonetaryContext is used.
monetaryContext
)

Static factory method for creating a new instance of Money.

public static Money

Returns:

A new instance of Money.
of
(BigDecimal
The numeric part, not null.
number
,
String
The target currency as ISO currency code.
currencyCode
,
MonetaryContext
the MonetaryContext to be used, if null the default MonetaryContext is used.
monetaryContext
)

Static factory method for creating a new instance of Money.

public static Money

Returns:

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

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

public static Money

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
fractionDigits
)

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

public static Money

Returns:

Money instance
parse
(CharSequence
the text to parse not null
text
)

Obtains an instance of Money from a text string such as 'EUR 25.25'.

public static Money

Returns:

Money instance
parse
(CharSequence
the text to parse not null
text
,
MonetaryAmountFormat
the formatter to use not null
formatter
)

Obtains an instance of Money from a text using specific formatter.

public Money
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 Money
remainder(long divisor)

Implements javax.money.MonetaryAmount.remainder.

Returns a MonetaryAmount whose value is this % divisor.

public Money
remainder(double divisor)

Implements javax.money.MonetaryAmount.remainder.

Returns a MonetaryAmount whose value is this % divisor.

public Money
remainder(Number divisor)

Implements javax.money.MonetaryAmount.remainder.

Returns a MonetaryAmount whose value is this % divisor.

public Money
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 Money
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 Money
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 Money
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 Money

Returns:

an instance of Money representing zero.
zero
(CurrencyUnit
the currency, not null.
currency
)

Obtains an instance of Money representing zero.

Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAllwaitwaitwait