Venice 0.751beta

nz.org.venice.util
Class Money

java.lang.Object
  extended bynz.org.venice.util.Money
All Implemented Interfaces:
java.lang.Cloneable, java.lang.Comparable

public class Money
extends java.lang.Object
implements java.lang.Cloneable, java.lang.Comparable

An immutable representation of Money. This class was created to reference sums of money. Due to rounding issues, the use of float and to a lesser extent double are not recommended to represent money. Therefore money should be represented by an integer type, which would require conversion from the more natural floating point type. This class was created to handle all this conversion work and provide a nice simple abstraction.

Author:
Andrew Leppard
See Also:
Currency

Field Summary
static Money ZERO
          Immutable representation of zero money in the default currency.
 
Constructor Summary
Money(Currency currency, double amount)
          Create a new Money instance from the given amount using the given currency.
Money(Currency currency, java.lang.String string)
          Create a new Money instance by parsing the given string.
Money(double amount)
          Create a new Money instance from the given amount using the default currency.
 
Method Summary
 Money add(double money)
          Add the given money to this money.
 Money add(Money money)
          Add the given money to this money.
 java.lang.Object clone()
          Return a clone of this object.
 int compareTo(java.lang.Object object)
          Compare this money to the specified money.
 Money divide(int number)
          Divide this money by the given number.
 double doubleValue()
          Return the value of this Money as a double.
 boolean equals(java.lang.Object object)
          Compare this money for equality with the specified money.
 Money exchange(Currency currency, double exchangeRate)
          Exchange this money for another currency at the given exchange rate.
 java.lang.String export()
          Returns a String object representing this Money's value without the currency sign.
 Currency getCurrency()
          Return the currency of this money.
 int hashCode()
          Return a hash code representation of this money.
 boolean isGreaterThan(Money money)
          Return whether this money is greater than to the given money.
 boolean isGreaterThanEqual(Money money)
          Return whether this money is greater than or equal to the given money.
 boolean isLessThan(Money money)
          Return whether this money is less than the given money.
 boolean isLessThanEqual(Money money)
          Return whether this money is less than or equal to the given money.
 Money multiply(int number)
          Multiply this money by the given number.
 Money subtract(Money money)
          Subtract the given money from this money.
 java.lang.String toString()
          Returns a String object representing this Money's value.
static java.lang.String toString(double value)
          Return a String representation of the given amount of money in the default currency.
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

ZERO

public static final Money ZERO
Immutable representation of zero money in the default currency.

Constructor Detail

Money

public Money(Currency currency,
             double amount)
Create a new Money instance from the given amount using the given currency.

Parameters:
currency - the currency of the money.
amount - the amount of money.

Money

public Money(double amount)
Create a new Money instance from the given amount using the default currency.

Parameters:
amount - the amount of money.

Money

public Money(Currency currency,
             java.lang.String string)
      throws MoneyFormatException
Create a new Money instance by parsing the given string.

Parameters:
string - the string to parse.
Throws:
MoneyFormatException - if there was an error parsing the string.
Method Detail

getCurrency

public Currency getCurrency()
Return the currency of this money.

Returns:
the currency

add

public Money add(Money money)
Add the given money to this money.

Parameters:
money - the money to add.
Returns:
the resultant Money.

add

public Money add(double money)
Add the given money to this money.

Parameters:
money - the money to add.
Returns:
the resultant Money.

subtract

public Money subtract(Money money)
Subtract the given money from this money.

Parameters:
money - the money to subtract.
Returns:
the resultant Money.

divide

public Money divide(int number)
Divide this money by the given number.

Parameters:
number - the number to divide by.
Returns:
the resultant Money.

multiply

public Money multiply(int number)
Multiply this money by the given number.

Parameters:
number - the number to multiply by.
Returns:
the resultant Money.

clone

public java.lang.Object clone()
Return a clone of this object. Since Money is immutable, this function simply returns this object.

Returns:
this object

isLessThan

public boolean isLessThan(Money money)
Return whether this money is less than the given money.

Parameters:
money - the money to compare with
Returns:
true if this money is less than the given money.

isLessThanEqual

public boolean isLessThanEqual(Money money)
Return whether this money is less than or equal to the given money.

Parameters:
money - the money to compare with
Returns:
true if this money is less than or equal to the given money.

isGreaterThan

public boolean isGreaterThan(Money money)
Return whether this money is greater than to the given money.

Parameters:
money - the money to compare with
Returns:
true if this money is greater than the given money.

isGreaterThanEqual

public boolean isGreaterThanEqual(Money money)
Return whether this money is greater than or equal to the given money.

Parameters:
money - the money to compare with
Returns:
true if this money is greater than or equal to the given money.

compareTo

public int compareTo(java.lang.Object object)
Compare this money to the specified money.

Specified by:
compareTo in interface java.lang.Comparable
Parameters:
object - the money to compare
Returns:
the value 0 if the monies are equal; 1 if this money is more than the specified money or -1 if this money is less than the specified money.

equals

public boolean equals(java.lang.Object object)
Compare this money for equality with the specified money.

Parameters:
object - the money to compare
Returns:
true iff the monies are equal.

hashCode

public int hashCode()
Return a hash code representation of this money.

Returns:
hash code representation.

exchange

public Money exchange(Currency currency,
                      double exchangeRate)
Exchange this money for another currency at the given exchange rate. This function returns a new money object which contains the exchanged money in the given currency.

Parameters:
currency - the currency to exchange this money for
exchangeRate - the exchange rate
Returns:
the new money in the given currency

toString

public java.lang.String toString()
Returns a String object representing this Money's value.

Returns:
the string representation.

toString

public static java.lang.String toString(double value)
Return a String representation of the given amount of money in the default currency.

Parameters:
value - the value of the money
Returns:
the string representation.

doubleValue

public double doubleValue()
Return the value of this Money as a double.

Returns:
the numeric value represented by this object after conversion to type double.

export

public java.lang.String export()
Returns a String object representing this Money's value without the currency sign.

Returns:
the string representation without the currency.

Venice 0.751beta