net.sourceforge.xconf.toolbox
Class Money

java.lang.Object
  extended by net.sourceforge.xconf.toolbox.Money
All Implemented Interfaces:
Serializable, Comparable

public class Money
extends Object
implements Comparable, Serializable

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:

  1. amount - a long primitive
  2. currency - a Currency instance

See Also:
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

Money

protected Money()
For Hibernate.


Money

public Money(long amount,
             Currency currency)
Creates an instance from the given fractional amount and currency. Please note that the given amount is expected to be in the lowest denomination, so if currency is Australian Dollars, for example, and you wish to represent $1.25, the amount would be 125 cents.


Money

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

See Also:
Money(long, Currency)

Money

public Money(double amount,
             Currency currency)
Creates an instance from the given decimal amount and currency. For example, if the currency is Australian Dollars, and you wish to represent $1.25, the amount would be 1.25 dollars.


Money

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

See Also:
Money(double, Currency)
Method Detail

getAmount

public long getAmount()
Returns the base amount represented by this instance.


setAmount

protected void setAmount(long amount)
For Hibernate.


getCurrency

public Currency getCurrency()
Returns the currency of this instance.


setCurrency

protected void setCurrency(Currency currency)
For Hibernate.


getDecimalAmount

public BigDecimal getDecimalAmount()
Returns the amount as a currency-adjusted decimal value.


add

public Money add(Money other)
Money addition. Adding a null to an instance simply returns a copy of the instance.

Throws:
IllegalArgumentException - If the currencies do not match.

subtract

public Money subtract(Money other)
Money subtraction. Subtracting a null from an instance simply returns a copy of the instance.

Throws:
IllegalArgumentException - If the currencies do not match.

multiply

public Money multiply(long multiplier)
Money multiplication without any rounding.


multiply

public Money multiply(double multiplier)
Money multiplication with a default rounding mode.

See Also:
multiply(BigDecimal, int)

multiply

public Money multiply(BigDecimal multiplier)
Money multiplication with a default rounding mode.

See Also:
multiply(BigDecimal, int), BigDecimal.ROUND_HALF_UP

multiply

public Money multiply(BigDecimal multiplier,
                      int roundingMode)
Money multiplication with user specified rounding mode.


negate

public Money negate()
Convert a negative amount into a positive, and vice versa.


allocate

public Money[] allocate(int n)
Allocates the amount (almost) evenly amongst a number of targets without losing any cents.


allocate

public Money[] allocate(long[] ratios)
Allocates this amount according to the given ratio without losing any cents.


isZero

public boolean isZero()
Returns true if this instance represents a zero value.


isNotZero

public boolean isNotZero()
Returns true if this instance represents a non-zero value.


isNegative

public boolean isNegative()
Returns true if this instance represents a negative value (ie. amount is less than zero).


isPositive

public boolean isPositive()
Returns true if this instance represents a postive value (ie. amount is greater than zero).


greaterThan

public boolean greaterThan(Money other)
Convenience implementation of greater than function.

See Also:
compareTo(Object)

lessThan

public boolean lessThan(Money other)
Convenience implementation of less than function.

See Also:
compareTo(Object)

compareTo

public int compareTo(Object obj)
Compares this Money instance to another Object. If the Object is a Money, this method simply compares its amount with this instance's amount. It is illegal to compare Money instances with different currencies.

Specified by:
compareTo in interface Comparable
Throws:
IllegalArgumentException - If the currencies do not match.

hashCode

public int hashCode()
A hash code for a Money instance is computed using its fully-qualified classname, amount value and currency.

Overrides:
hashCode in class Object

equals

public boolean equals(Object obj)
Compares this Money instance to the specified object. The result is 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.

Overrides:
equals in class Object

toString

public String toString()
Uses the platform default locale.

Overrides:
toString in class Object
See Also:
toString(Locale)

toString

public String toString(Locale locale)
Returns the ISO 4217 currency code followed by a localised string representation of this instance's decimal amount.


sameCurrency

public boolean sameCurrency(Money other)
Returns true if the given object is of the same currency as this instance.


assertSameCurrency

public void assertSameCurrency(Money other)
Asserts that the given object is of the same currency as this instance.

Throws:
IllegalArgumentException - If the currencies do not match.


Copyright © 2007 Thomas Czarniecki. All Rights Reserved.