com.ail.util
Class Rate

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

public class Rate
extends Type

This class represents fractions or rates - with some special handling for common fractions like percentages and permils.

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

Field Summary
 
Fields inherited from class com.ail.core.Type
jXPathContext, NOT_PERSISTED
 
Constructor Summary
Rate()
          Constructor.
Rate(java.lang.String rate)
          Constructor.
 
Method Summary
 java.math.BigDecimal applyTo(java.math.BigDecimal value, int scale, int roundingMode)
          Apply this rate to a BigDecimal value.
 double applyTo(double value, int scale, int roundingMode)
          Apply this rate to a double value.
 java.lang.String applyTo(java.lang.String value, int scale, int roundingMode)
          Apply this rate to a String value.
 java.lang.Object clone()
          Clone this object.
 java.lang.String getRate()
          Get the rate as a string.
 void setRate(java.lang.String rate)
          Rates can be expressed in the following formats: "n%" where 0 <= n <= 100.
 
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
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Rate

public Rate()
Constructor. This constructor is provided for use by frameworks (such as castor) which require default constructors. Its use is discouraged in code as the rate created is unusable until the setRate method has been called.


Rate

public Rate(java.lang.String rate)
Constructor. This constructor accepts rates in a number of formats see the setRate method for details.

Parameters:
rate -
Method Detail

setRate

public void setRate(java.lang.String rate)
             throws java.lang.IllegalArgumentException,
                    java.lang.NumberFormatException
Rates can be expressed in the following formats:

Parameters:
rate - The representation of a rate.
Throws:
java.lang.IllegalArgumentException - if a value is out of range, e.g. "102%".
java.lang.NumberFormatException - if the rate strings format does not match one of those described above.

getRate

public java.lang.String getRate()
Get the rate as a string. The string returned is the same as that used to inialise the Rate object (or passed to setRate).

Returns:
The Rate string, or null if not defined.

applyTo

public java.math.BigDecimal applyTo(java.math.BigDecimal value,
                                    int scale,
                                    int roundingMode)
                             throws java.lang.IllegalStateException
Apply this rate to a BigDecimal value. The resulting value will be rounded using the rounding mode specified.

Parameters:
value - The value to apply the rate to
scale - Scale (decimal places) for the calculation.
roundingMode - The rounding mode to use
Returns:
The result of appying this rate to value.
Throws:
java.lang.IllegalStateException - If this rate has not be initialised.
See Also:
For details of rounding modes.

applyTo

public java.lang.String applyTo(java.lang.String value,
                                int scale,
                                int roundingMode)
                         throws java.lang.IllegalStateException,
                                java.lang.NumberFormatException
Apply this rate to a String value. The string is assumed to represent a BigDecimal value. The rounding mode specified will be used during the calculation.

Parameters:
value - String in BigDecimal format
scale - Scale (decimal places) for the calculation.
roundingMode - Rounding to mode to apply
Returns:
The result of applying the rate, as a string.
Throws:
java.lang.IllegalStateException - If this rate has not been initialised.
java.lang.NumberFormatException - If "value" does not represent a valid BigDecimal.
See Also:
For details of rounding modes.

applyTo

public double applyTo(double value,
                      int scale,
                      int roundingMode)
               throws java.lang.IllegalStateException
Apply this rate to a double value. The result will be rounded using the specificed rounding mode.

Parameters:
value - Value to apply the rate to.
scale - Scale (decimal places) for the calculation.
roundingMode - Rounding mode to use.
Returns:
The result of applying the rate.
Throws:
java.lang.IllegalStateException - If this rate has not beeb initialised.

clone

public java.lang.Object clone()
Description copied from class: Type
Clone this object. This clone method is used by all Type subclasses to handle deep cloning. For the factory to operate correctly it is essential that Types can be deep cloned, as it hangs onto prototyped instances by name and simply clones them when a request is made for an instance of a named type.

Overrides:
clone in class Type