- java.lang.Object
-
- com.aoapps.lang.i18n.Money
-
- All Implemented Interfaces:
FastExternalizable,Externalizable,Serializable,Comparable<Money>
public final class Money extends Object implements FastExternalizable, Comparable<Money>
Stores a monetary value as a combination of currency and amount. It supports many of the basic operators fromBigDecimal, and more will be added as needed. AnArithmeticExceptionon any attempt to perform operations on monetary values of different currencies.- Author:
- AO Industries, Inc.
- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Constructor Description Money()Deprecated.Only required for implementation, do not use directly.Money(Currency currency, long value, int scale)Money(Currency currency, BigDecimal value)Will change the scale of the value to match the currency, but will not round.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Moneyadd(Money addend)intcompareTo(Money other)Sorts by currency code and then value.booleanequals(Object o)Equal when has same currency, value, and scale.CurrencygetCurrency()intgetScale()Gets the scale of this currency.longgetSerialVersionUID()Gets the serialVersionUID for this object.longgetUnscaledValue()Gets the unscaled value of this currency.BigDecimalgetValue()inthashCode()Moneymultiply(BigDecimal multiplicand)Multiplies without rounding.Moneymultiply(BigDecimal multiplicand, RoundingMode roundingMode)Multiplies with rounding.Moneynegate()Returns a monetary amount that is the negative of this amount.static MoneyparseMoney(Currency currency, Locale locale, String value)static BigDecimalparseMoneyAmount(Locale locale, String symbol, String value)Parses a monetary amount with an optional symbol prefix.voidreadExternal(ObjectInput in)Moneysubtract(Money subtrahend)StringtoString()Displays the monetary value as currency symbol (in Locale-specific display) either proceeding or following the value, such as"$100.00","Can$-100.50","100,00 $ CA"(with non-breaking spaces).voidwriteExternal(ObjectOutput out)
-
-
-
Constructor Detail
-
Money
public Money(Currency currency, BigDecimal value) throws NumberFormatException
Will change the scale of the value to match the currency, but will not round.- Throws:
NumberFormatException- if unable to scale the value.
-
Money
public Money(Currency currency, long value, int scale) throws NumberFormatException
- Throws:
NumberFormatException
-
Money
@Deprecated public Money()
Deprecated.Only required for implementation, do not use directly.- See Also:
FastExternalizable
-
-
Method Detail
-
parseMoneyAmount
public static BigDecimal parseMoneyAmount(Locale locale, String symbol, String value) throws NumberFormatException
Parses a monetary amount with an optional symbol prefix. Trims, strips the prefix, trims, removes any commas.TODO: A future version should be more locale-aware regarding assumption comma is used for group separator.
- Parameters:
symbol- The optional currency prefix to strip (seeCurrencyUtil.getSymbol(java.util.Currency, java.util.Locale))value- The value to parse- Returns:
- The monetary amount or
nullif empty - Throws:
NumberFormatException- when not a value amount after filtering
-
equals
public boolean equals(Object o)
Equal when has same currency, value, and scale.
-
compareTo
public int compareTo(Money other)
Sorts by currency code and then value.- Specified by:
compareToin interfaceComparable<Money>- See Also:
CurrencyComparator
-
getCurrency
public Currency getCurrency()
-
getValue
public BigDecimal getValue()
-
getUnscaledValue
public long getUnscaledValue()
Gets the unscaled value of this currency.
-
getScale
public int getScale()
Gets the scale of this currency.
-
toString
public String toString()
Displays the monetary value as currency symbol (in Locale-specific display) either proceeding or following the value, such as"$100.00","Can$-100.50","100,00 $ CA"(with non-breaking spaces).- Overrides:
toStringin classObject- See Also:
CurrencyUtil.getSymbol(java.util.Currency, java.util.Locale),ThreadLocale
-
add
public Money add(Money addend) throws ArithmeticException
- Throws:
ArithmeticException
-
multiply
public Money multiply(BigDecimal multiplicand) throws ArithmeticException
Multiplies without rounding.- Throws:
ArithmeticException
-
multiply
public Money multiply(BigDecimal multiplicand, RoundingMode roundingMode) throws ArithmeticException
Multiplies with rounding.- Throws:
ArithmeticException
-
negate
public Money negate()
Returns a monetary amount that is the negative of this amount.
-
subtract
public Money subtract(Money subtrahend) throws ArithmeticException
- Throws:
ArithmeticException
-
getSerialVersionUID
public long getSerialVersionUID()
Description copied from interface:FastExternalizableGets the serialVersionUID for this object. Since FastExternalizable-aware containers are not special JVM constructs, they do not have access to the private and protected fields. It is assumed that two separate instances of the same class will always return the same serialVersionUID.- Specified by:
getSerialVersionUIDin interfaceFastExternalizable
-
writeExternal
public void writeExternal(ObjectOutput out) throws IOException
- Specified by:
writeExternalin interfaceExternalizable- Throws:
IOException
-
readExternal
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException
- Specified by:
readExternalin interfaceExternalizable- Throws:
IOExceptionClassNotFoundException
-
-