|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Objectnet.sourceforge.xconf.toolbox.Money
public class Money
This class is based on the Money(488) pattern in "Patterns of Enterprise Application Architecture" by Martin Fowler, Addison-Wesley, 2002.
To create an instance of a Money object using a ORM persistence service like Hibernate, you have to set two properties:
Currency instance
MoneyFormat,
MoneyParser,
Serialized Form| Constructor Summary | |
|---|---|
protected |
Money()
For Hibernate. |
|
Money(double amount,
Currency currency)
Creates an instance from the given decimal amount and currency. |
|
Money(double amount,
String currencyCode)
Convenience constructor to create an instance using the given decimal amount and a currency represented by the given ISO 4217 currency code. |
|
Money(long amount,
Currency currency)
Creates an instance from the given fractional amount and currency. |
|
Money(long amount,
String currencyCode)
Convenience constructor to create an instance using the given fractional amount and a currency represented by the given ISO 4217 currency code. |
| Method Summary | |
|---|---|
Money |
add(Money other)
Money addition. |
Money[] |
allocate(int n)
Allocates the amount (almost) evenly amongst a number of targets without losing any cents. |
Money[] |
allocate(long[] ratios)
Allocates this amount according to the given ratio without losing any cents. |
void |
assertSameCurrency(Money other)
Asserts that the given object is of the same currency as this instance. |
int |
compareTo(Object obj)
Compares this Money instance to another Object. |
boolean |
equals(Object obj)
Compares this Money instance to the specified object. |
long |
getAmount()
Returns the base amount represented by this instance. |
Currency |
getCurrency()
Returns the currency of this instance. |
BigDecimal |
getDecimalAmount()
Returns the amount as a currency-adjusted decimal value. |
boolean |
greaterThan(Money other)
Convenience implementation of greater than function. |
int |
hashCode()
A hash code for a Money instance is computed using its fully-qualified classname, amount value and currency. |
boolean |
isNegative()
Returns true if this instance represents a negative value
(ie. |
boolean |
isNotZero()
Returns true if this instance represents a non-zero value. |
boolean |
isPositive()
Returns true if this instance represents a postive value
(ie. |
boolean |
isZero()
Returns true if this instance represents a zero value. |
boolean |
lessThan(Money other)
Convenience implementation of less than function. |
Money |
multiply(BigDecimal multiplier)
Money multiplication with a default rounding mode. |
Money |
multiply(BigDecimal multiplier,
int roundingMode)
Money multiplication with user specified rounding mode. |
Money |
multiply(double multiplier)
Money multiplication with a default rounding mode. |
Money |
multiply(long multiplier)
Money multiplication without any rounding. |
Money |
negate()
Convert a negative amount into a positive, and vice versa. |
boolean |
sameCurrency(Money other)
Returns true if the given object is of the same currency as this instance. |
protected void |
setAmount(long amount)
For Hibernate. |
protected void |
setCurrency(Currency currency)
For Hibernate. |
Money |
subtract(Money other)
Money subtraction. |
String |
toString()
Uses the platform default locale. |
String |
toString(Locale locale)
Returns the ISO 4217 currency code followed by a localised string representation of this instance's decimal amount. |
| Methods inherited from class java.lang.Object |
|---|
clone, finalize, getClass, notify, notifyAll, wait, wait, wait |
| Constructor Detail |
|---|
protected Money()
public Money(long amount,
Currency currency)
125 cents.
public Money(long amount,
String currencyCode)
Money(long, Currency)
public Money(double amount,
Currency currency)
1.25 dollars.
public Money(double amount,
String currencyCode)
Money(double, Currency)| Method Detail |
|---|
public long getAmount()
protected void setAmount(long amount)
public Currency getCurrency()
protected void setCurrency(Currency currency)
public BigDecimal getDecimalAmount()
public Money add(Money other)
null to an instance simply
returns a copy of the instance.
IllegalArgumentException - If the currencies do not match.public Money subtract(Money other)
null from an instance
simply returns a copy of the instance.
IllegalArgumentException - If the currencies do not match.public Money multiply(long multiplier)
public Money multiply(double multiplier)
multiply(BigDecimal, int)public Money multiply(BigDecimal multiplier)
multiply(BigDecimal, int),
BigDecimal.ROUND_HALF_UP
public Money multiply(BigDecimal multiplier,
int roundingMode)
public Money negate()
public Money[] allocate(int n)
public Money[] allocate(long[] ratios)
public boolean isZero()
true if this instance represents a zero value.
public boolean isNotZero()
true if this instance represents a non-zero value.
public boolean isNegative()
true if this instance represents a negative value
(ie. amount is less than zero).
public boolean isPositive()
true if this instance represents a postive value
(ie. amount is greater than zero).
public boolean greaterThan(Money other)
compareTo(Object)public boolean lessThan(Money other)
compareTo(Object)public int compareTo(Object obj)
compareTo in interface ComparableIllegalArgumentException - If the currencies do not match.public int hashCode()
hashCode in class Objectpublic boolean equals(Object obj)
true if and only if the argument is not
null and is a Money object that represents
the same amount and currency as this instance.
equals in class Objectpublic String toString()
toString in class ObjecttoString(Locale)public String toString(Locale locale)
public boolean sameCurrency(Money other)
true if the given object is of the same currency as this instance.
public void assertSameCurrency(Money other)
IllegalArgumentException - If the currencies do not match.
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||