org.davesag.generica.core
Class Money

java.lang.Object
  extended byorg.davesag.generica.core.Money
All Implemented Interfaces:
Comparable, Serializable

public class Money
extends Object
implements Serializable, Comparable

This Money bean represents an amount in a currency. This is a whole number of 'cents' in that currency. Note throughout this documentation I shall refer to cents as being the indivisible component of a currency, even though obviously many currencies don't actually have Cents. Use the valueOf method to get the actual 'dollar' value.

Since:
Generica 1.0
Version:
CVS Revision $Id: Money.java,v 1.12 2004/04/08 13:19:08 davesag Exp $
Author:
Dave Sag http://www.davesag.com
See Also:
Serialized Form

Field Summary
protected  BigDecimal theAmount
          the amount
protected  Currency theCurrency
          the Currency
 
Constructor Summary
Money()
          null constructor required by hibernate.
Money(Currency currency, BigDecimal amount)
          Constructor that takes a currency object and number of 'cents' as a big decimal.
Money(Currency currency, int n)
          Constructor that takes a currency object and number of 'cents'.
Money(Currency currency, Integer n)
          Constructor that takes a currency object and number of 'cents'.
Money(Currency currency, String amount)
          Constructor that takes a currency object and number of 'cents' as a string.
Money(String currencycode, BigDecimal amount)
          Constructor that takes a currency code and number of 'cents' as a big decimal.
Money(String currencycode, int n)
          Constructor that takes a iso currency code and number of 'cents'.
Money(String currencycode, Integer amount)
          Constructor that takes a currency code and number of 'cents' as a string.
Money(String currencycode, String amount)
          Constructor that takes a currency code and number of 'cents' as a string.
 
Method Summary
 Money add(Money addto)
          Adds the supplied money to this money, and converts the currency as needed.
 int compareTo(Object o)
          Compares this money with the supplied money object, performing currency conversion if needed.
 boolean equals(Object o)
          compare the two moneys for equality.
 String format(Locale loc)
          Constructs a correcty formatted string representation of this money, according to a format in org.davesag.generica.resource.application currecy.format.
 String format(Locale loc, String formatpattern)
          Constructs a correcty formatted string representation of this money, according to a format pattern supplied.
 BigDecimal getAmount()
          get the amount in a whole number of 'cents'.
 Currency getCurrency()
          get the currency
 String getCurrencyCode()
          get the ISO 4217 currency code.
 Integer getIntegerAmount()
          get the amount in a whole number of 'cents'.
 int hashCode()
          require to override hashcode when we override the equals method.
 Money multiply(int timesby)
          Multiplies the supplied number by this money's amount.
 Money multiply(Long timesby)
          Multiplies the supplied number by this money's amount.
 void setAmount(BigDecimal amnt)
          Set the amount in 'cents'.
 void setCurrency(Currency curr)
          Set the currency
 void setCurrencyCode(String curcode)
          Set the ISO 4217 currency code.
 void setIntegerAmount(Integer amnt)
          Set the amount in 'cents'.
 Money subtract(Money subfrom)
          Subtracts the supplied money from this money, and converts the currency as needed.
 String toString()
          a simple string version of this money.
static BigDecimal valueOf(Money m)
          Money is stored as a whole number of 'cents' The actual value depends on the number of decimal places in the currency, but 'cents' is a useful generic term.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

theCurrency

protected Currency theCurrency
the Currency


theAmount

protected BigDecimal theAmount
the amount

Constructor Detail

Money

public Money()
null constructor required by hibernate.


Money

public Money(String currencycode,
             int n)
Constructor that takes a iso currency code and number of 'cents'.

Parameters:
currencycode - The currency code to use.
n - The amount in cents

Money

public Money(Currency currency,
             int n)
Constructor that takes a currency object and number of 'cents'.

Parameters:
currency - The currency to use.
n - The amount in cents

Money

public Money(Currency currency,
             Integer n)
Constructor that takes a currency object and number of 'cents'.

Parameters:
currency - The currency to use.
n - The amount in cents as an Integer

Money

public Money(String currencycode,
             String amount)
Constructor that takes a currency code and number of 'cents' as a string.

Parameters:
currencycode - The currency code to use.
amount - The amount in cents as a string

Money

public Money(String currencycode,
             Integer amount)
Constructor that takes a currency code and number of 'cents' as a string.

Parameters:
currencycode - The currency code to use.
amount - The amount in cents as an Integer

Money

public Money(Currency currency,
             String amount)
Constructor that takes a currency object and number of 'cents' as a string.

Parameters:
currency - The currency to use.
amount - The amount in cents as a string

Money

public Money(String currencycode,
             BigDecimal amount)
Constructor that takes a currency code and number of 'cents' as a big decimal.

Parameters:
currencycode - The currency code to use.
amount - The amount in cents as a BigDecimal

Money

public Money(Currency currency,
             BigDecimal amount)
Constructor that takes a currency object and number of 'cents' as a big decimal.

Parameters:
currency - The currency to use.
amount - The amount in cents as a BigDecimal
Method Detail

getCurrency

public Currency getCurrency()
get the currency

Returns:
The currency

setCurrency

public void setCurrency(Currency curr)
Set the currency

Parameters:
curr - the first name.

getCurrencyCode

public String getCurrencyCode()
get the ISO 4217 currency code.

Returns:
The ISO 4217 currency code.
Hibernate Property:
length = "3"

setCurrencyCode

public void setCurrencyCode(String curcode)
Set the ISO 4217 currency code.

Parameters:
curcode - the first name.

getAmount

public BigDecimal getAmount()
get the amount in a whole number of 'cents'.

Returns:
The amount in 'cents'.

getIntegerAmount

public Integer getIntegerAmount()
get the amount in a whole number of 'cents'.

Returns:
The amount in 'cents'.
Hibernate Property:

setIntegerAmount

public void setIntegerAmount(Integer amnt)
Set the amount in 'cents'.

Parameters:
amnt - The amount

setAmount

public void setAmount(BigDecimal amnt)
Set the amount in 'cents'.

Parameters:
amnt - The amount

add

public Money add(Money addto)
Adds the supplied money to this money, and converts the currency as needed.

Parameters:
addto - The money to add to this money.
Returns:
The money that represents the sum of the monies.

subtract

public Money subtract(Money subfrom)
Subtracts the supplied money from this money, and converts the currency as needed.

Parameters:
subfrom - The money to subtract from this money.
Returns:
The money that represents the new value.

multiply

public Money multiply(int timesby)
Multiplies the supplied number by this money's amount.

Parameters:
timesby - The integer to multiply this money by.
Returns:
The money that represents the multiplied money.

multiply

public Money multiply(Long timesby)
Multiplies the supplied number by this money's amount.

Parameters:
timesby - The integer to multiply this money by.
Returns:
The money that represents the multiplied money.

compareTo

public int compareTo(Object o)
Compares this money with the supplied money object, performing currency conversion if needed.

Specified by:
compareTo in interface Comparable
Parameters:
o - The object to compare.
Returns:
-1 if this < 0, +1 if this > o and 0 if this == o.
Throws:
NullPointerException - if the object incoming was null.
ClassCastException - if the incoming object was not a Money.

equals

public boolean equals(Object o)
compare the two moneys for equality.

Parameters:
o - The other object (should be a money).
Returns:
true if the object is a Money of the same currency and value. Use compareTo to test equivalence of monies with different currencies.

hashCode

public int hashCode()
require to override hashcode when we override the equals method.

Returns:
the hashcode for this Money.

valueOf

public static BigDecimal valueOf(Money m)
Money is stored as a whole number of 'cents' The actual value depends on the number of decimal places in the currency, but 'cents' is a useful generic term.

Parameters:
m - the money.
Returns:
the value of this money as a properly constructed number to the correct decimal place.

format

public String format(Locale loc,
                     String formatpattern)
Constructs a correcty formatted string representation of this money, according to a format pattern supplied.

Parameters:
loc - The local to display the money in.
formatpattern - the fomatting pattern.
Returns:
A string representation of this money.

format

public String format(Locale loc)
Constructs a correcty formatted string representation of this money, according to a format in org.davesag.generica.resource.application currecy.format.

Parameters:
loc - The local to display the money in.
Returns:
A string representation of this money.

toString

public String toString()
a simple string version of this money.

Returns:
a formatted string with the currency symbol and value according to the default locale.


Dave Sag Generica API version 0.5.9 - (prerelease) Copyright 2003 - 2004 Dave Sag.