com.ail.financial
Class CurrencyAmount

java.lang.Object
  extended by com.ail.core.Type
      extended by com.ail.financial.CurrencyAmount
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable

public class CurrencyAmount
extends Type

Instances of this class represent amounts of money. todo This currently assumes a BigDecimal scale of 2 for all currencies.

Version:
$Revision: 1.5 $
See Also:
Serialized Form

Field Summary
 
Fields inherited from class com.ail.core.Type
jXPathContext, NOT_PERSISTED
 
Constructor Summary
CurrencyAmount()
          Default constructor.
CurrencyAmount(java.math.BigDecimal amount, Currency currency)
          Constructor
CurrencyAmount(double amount, Currency currency)
          Constructor
CurrencyAmount(java.lang.String amount, Currency currency)
          Constructor
 
Method Summary
 void add(CurrencyAmount that)
          Add another currency amount to this one, saving the result in this one.
 void apply(Rate rate)
          Apply a percentage to this currency amount.
 java.lang.Object clone()
          Clone this.
 boolean equals(java.lang.Object obj)
          Equality checker
 java.math.BigDecimal getAmount()
          Get the amount represented
 java.lang.String getAmountAsString()
           
 Currency getCurrency()
          Get the amount's currency
 java.lang.String getCurrencyAsString()
          Get the currency as a string.
 boolean greaterThan(CurrencyAmount that)
          Compare the value of 'that' currency amount with this.
 boolean greaterThan(double amount, Currency currency)
          Compare the value of amount in currency with the amount represented by this.
 boolean lessThan(CurrencyAmount that)
          Compare the value of 'that' currency amount with this.
 boolean lessThan(double amount, Currency currency)
          Compare the value of amount in currency with the amount represented by this.
 void setAmount(java.math.BigDecimal amount)
          Set the amount property.
 void setAmountAsString(java.lang.String amount)
          Set the amount property from a string.
 void setCurrencyAsString(java.lang.String currency)
          Set the currency based on a String representation of a currency.
 void subtract(CurrencyAmount that)
          Subtract another currency amount from this one, saving the result in this one.
 java.lang.String toString()
          Get the String representation of this amount.
 
Methods inherited from class com.ail.core.Type
addAttribute, deleteLock, deleteSerialVersion, fetchJXPathContext, getAttribute, getAttribute, getAttributeCount, getForeignSystemId, getLock, getSerialVersion, getSystemId, hasLock, hasSerialVersion, hasSystemId, isPersisted, markAsNotPersisted, mergeWithDataFrom, removeAttribute, removeAttribute, setAttribute, setForeignSystemId, setLock, setSerialVersion, setSystemId, xpathGet, xpathGet, xpathIterate, xpathSet
 
Methods inherited from class java.lang.Object
getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

CurrencyAmount

public CurrencyAmount()
Default constructor. This constructor is provided to use by frameworks (like castor) that demand a default constructor. Its use from code is discounraged as it creates an instance that is unusable until the setters for amount and currency have been invoked.


CurrencyAmount

public CurrencyAmount(java.math.BigDecimal amount,
                      Currency currency)
Constructor

Parameters:
amount - The amount
currency - The currency

CurrencyAmount

public CurrencyAmount(java.lang.String amount,
                      Currency currency)
Constructor

Parameters:
amount - The amount
currency - The currency

CurrencyAmount

public CurrencyAmount(double amount,
                      Currency currency)
Constructor

Parameters:
amount - The amount
currency - The currency.
Method Detail

getAmount

public java.math.BigDecimal getAmount()
Get the amount represented

Returns:
the amount.

setAmount

public void setAmount(java.math.BigDecimal amount)
Set the amount property.

Parameters:
amount -

setAmountAsString

public void setAmountAsString(java.lang.String amount)
                       throws java.lang.NumberFormatException
Set the amount property from a string.

Parameters:
amount -
Throws:
java.lang.NumberFormatException - If string does not represent a valid decimal amount.

getAmountAsString

public java.lang.String getAmountAsString()

subtract

public void subtract(CurrencyAmount that)
              throws java.lang.IllegalArgumentException
Subtract another currency amount from this one, saving the result in this one.

Parameters:
that - Currency amount to subtract
Throws:
java.lang.IllegalArgumentException - If the currency types does not match.

add

public void add(CurrencyAmount that)
         throws java.lang.IllegalArgumentException
Add another currency amount to this one, saving the result in this one.

Parameters:
that - Currenct amount to add
Throws:
java.lang.IllegalArgumentException - If the curerncies do not match.

lessThan

public boolean lessThan(CurrencyAmount that)
Compare the value of 'that' currency amount with this. The two amounts must represent the same currency.

Parameters:
that - Currency amount to compare with.
Returns:
true if 'that' is less than this.
Throws:
java.lang.IllegalArgumentException - if the currencies do not match.

greaterThan

public boolean greaterThan(CurrencyAmount that)
Compare the value of 'that' currency amount with this. The two amounts must represent the same currency.

Parameters:
that - Currency amount to compare with.
Returns:
true if 'that' is greater than than this.
Throws:
java.lang.IllegalArgumentException - if the currencies do not match.

lessThan

public boolean lessThan(double amount,
                        Currency currency)
Compare the value of amount in currency with the amount represented by this. The two currencies must be the same.

Parameters:
amount - Amount to compare against.
currency - The currency amount is in.
Returns:
true if the amount represented by amount and currency is less than than this.
Throws:
java.lang.IllegalArgumentException - if the currencies do not match.

greaterThan

public boolean greaterThan(double amount,
                           Currency currency)
Compare the value of amount in currency with the amount represented by this. The two currencies must be the same.

Parameters:
amount - Amount to compare against.
currency - The currency amount is in.
Returns:
true if the amount represented by amount and currency is greater than than this.
Throws:
java.lang.IllegalArgumentException - if the currencies do not match.

setCurrencyAsString

public void setCurrencyAsString(java.lang.String currency)
                         throws java.lang.IndexOutOfBoundsException
Set the currency based on a String representation of a currency.

Parameters:
currency -
Throws:
java.lang.IndexOutOfBoundsException

getCurrency

public Currency getCurrency()
Get the amount's currency

Returns:
currency

getCurrencyAsString

public java.lang.String getCurrencyAsString()
Get the currency as a string.

Returns:
Currency as a string

toString

public java.lang.String toString()
Get the String representation of this amount.

Overrides:
toString in class java.lang.Object
Returns:

apply

public void apply(Rate rate)
Apply a percentage to this currency amount.

Parameters:
rate - Percentage to apply.

clone

public java.lang.Object clone()
                       throws java.lang.CloneNotSupportedException
Clone this. Most types can rely on Type's clone method to handle this, but because we want to keep setCurrency() private, we have to handle the method here.

Overrides:
clone in class Type
Returns:
Exact copy of this.
Throws:
java.lang.CloneNotSupportedException - If the clone fails

equals

public boolean equals(java.lang.Object obj)
Equality checker

Overrides:
equals in class java.lang.Object
Parameters:
that - Amount to check equality with
Returns:
true if this has the same currency and value as that.