Class Money

java.lang.Object
com.highdeal.currency.Money
All Implemented Interfaces:
Serializable

public class Money extends Object implements Serializable
Money is an immutable representing an amount of money with an arbitrary precision.
See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
    Rounds toward nearest neighbor.
    static final int
    Increments the digit prior to a non-zero discarded fraction.
    static final int
    Truncate the money amount.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a new money amount by parsing the string s that must be like EUR 0.00 or USD 0.00 for instance.
    Money(BigDecimal anAmount, Currency aCurrency)
    Create a new money amount for a currency and with an internal precision sets to the currency default precision.
    Money(BigDecimal anAmount, Currency aCurrency, int internalPrecision)
    Create a new money amount for a currency and with an internal precision.
    Money(BigDecimal anAmount, Currency aCurrency, int internalPrecision, int roundingMode)
    Create a new money amount for a currency and with an internal precision.
    Money(BigDecimal anAmount, String currencyCode)
    Create a new money amount for a currency and with an internal precision sets to the currency default precision.
    Money(BigDecimal anAmount, String currencyCode, int internalPrecision, int roundingMode)
    Create a new money amount for a currency and with an internal precision.
  • Method Summary

    Modifier and Type
    Method
    Description
    add(Money val)
    Returns a money amount whose value is the sum of this and val and whose internal precision is the maximum of the internal precisions of this and val.
    add(Money val, int precision, int roundingMode)
    Returns a money amount whose value is the sum of this and val with a specified precision and rouding mode.
    int
    Returns 0 if amounts are equals (same currency and same value), -1, or 1.
    static int
    convertRounding(int rounding)
    Converts a money rounding into a big decimal rounding.
    divide(BigDecimal val, int roundingMode)
    Returns a money amount whose value is the quotient of this by a value, rounded through a given rounding mode.
    divide(BigDecimal val, int internalPrecision, int roundingMode)
    Returns a money amount whose value is the quotient of this by a value, rounded through a given rounding mode and with a given limited precision.
    Creates a duplicate of this money amount.
    boolean
    equals(Object moneyAmount)
    Returns true if amounts are equals (same currency and same value), false otherwise.
    Returns the amount for this money amount.
    Returns the currency for this money amount.
    static String
    getRounding(int rounding)
    Returns the string representation of the rounding mode corresponding to a money rounding mode.
    static int
    getRounding(String rounding)
    Returns the money rounding from a string representation of the rounding.
    int
     
    static boolean
    Tests if the String s is in money format.
    boolean
    Returns true if amount is zero, whatever the currency, false otherwise.
    max(Money val)
    Returns the maximum of this and val.
    min(Money val)
    Returns the minimum of this and val.
    multiply(BigDecimal val, int roundingMode)
    Returns a money amount whose value is the multiplication of this by a value, rounded through a given rounding mode.
    multiply(BigDecimal val, int internalPrecision, int roundingMode)
    Returns a money amount whose value is the multiplication of this by a value, rounded through a given rounding mode and with a given limited precision.
    Returns a money amount whose value is the opposite of this amount.
    round(int roundingMode)
    Returns a new money amount whose value is rounded to the precision of its currency using the specified rounding mode.
    round(int precision, int roundingMode)
    Returns a new money amount whose value is rounded to the specified precision and rounding mode.
    Returns a money amount whose value is the difference of this and val and whose internal precision is the maximum of the internal precisions of this and val.
    subtract(Money val, int precision, int roundingMode)
    Returns a money amount whose value is the difference of this and val with a specified precision and rouding mode.
    toEMUCurrency(Currency toCurrency)
    Converts the money amount to a EMU currency.
    Converts the money amount to Euro.
    Gives a string representation of the amount.

    Methods inherited from class java.lang.Object

    getClass, notify, notifyAll, wait, wait, wait
  • Field Details

    • NEAREST

      public static final int NEAREST
      Rounds toward nearest neighbor. If the discarded fraction is >= .5, then raise, otherwise truncate.
      See Also:
    • TRUNCATE

      public static final int TRUNCATE
      Truncate the money amount. The fraction is discarded.
      See Also:
    • RAISE

      public static final int RAISE
      Increments the digit prior to a non-zero discarded fraction.
      See Also:
  • Constructor Details

    • Money

      public Money(String s) throws IllegalArgumentException
      Creates a new money amount by parsing the string s that must be like EUR 0.00 or USD 0.00 for instance.
      Parameters:
      s - the string to be parsed.
      Throws:
      IllegalArgumentException - a parsing error occurs
    • Money

      public Money(BigDecimal anAmount, Currency aCurrency, int internalPrecision)
      Create a new money amount for a currency and with an internal precision. If the amount has a scale which is greater than internal precision, then it is rounded to the nearest amount with internal precision.
      Parameters:
      anAmount - the initial money amount.
      aCurrency - the currency for this money amount.
      internalPrecision - the internal precision used for computations.
    • Money

      public Money(BigDecimal anAmount, Currency aCurrency, int internalPrecision, int roundingMode)
      Create a new money amount for a currency and with an internal precision. If the amount has a scale which is greater than internal precision, then it is rounded accorded to the with internal precision.
      Parameters:
      anAmount - the initial money amount.
      aCurrency - the currency for this money amount.
      internalPrecision - the internal precision used for computations.
      roundingMode - the mode of rounding NEAREST, TRUNCATE or RAISE
    • Money

      public Money(BigDecimal anAmount, String currencyCode)
      Create a new money amount for a currency and with an internal precision sets to the currency default precision.
      Parameters:
      anAmount - the initial amount.
      currencyCode - the currency code for this amount.
      See Also:
    • Money

      public Money(BigDecimal anAmount, String currencyCode, int internalPrecision, int roundingMode)
      Create a new money amount for a currency and with an internal precision. If the amount has a scale which is greater than internal precision, then it is rounded accorded to the with internal precision.
      Parameters:
      anAmount - the initial money amount.
      currencyCode - the currency code for this money amount.
      internalPrecision - the internal precision used for computations.
      roundingMode - the mode of rounding NEAREST, TRUNCATE or RAISE
    • Money

      public Money(BigDecimal anAmount, Currency aCurrency)
      Create a new money amount for a currency and with an internal precision sets to the currency default precision.
      Parameters:
      anAmount - the initial amount.
      aCurrency - the currency for this amount.
      See Also:
  • Method Details

    • isZero

      public boolean isZero()
      Returns true if amount is zero, whatever the currency, false otherwise.
      Returns:
      true if amount is zero, false otherwise.
    • duplicate

      public Money duplicate()
      Creates a duplicate of this money amount.
      Returns:
      a duplicate of this money amount.
    • getCurrency

      public Currency getCurrency()
      Returns the currency for this money amount.
      Returns:
      the currency for this money amount.
    • getAmount

      public BigDecimal getAmount()
      Returns the amount for this money amount.
      Returns:
      the amount for this money amount.
    • negate

      public Money negate()
      Returns a money amount whose value is the opposite of this amount.
      Returns:
      the opposite amount for this money amount.
    • add

      public Money add(Money val) throws IncompatibleCurrenciesException
      Returns a money amount whose value is the sum of this and val and whose internal precision is the maximum of the internal precisions of this and val. Both money amounts must have the same currency.
      Parameters:
      val - The money to add.
      Returns:
      The sum of the money amounts.
      Throws:
      IncompatibleCurrenciesException - if the currencies of this and val are not equals.
    • add

      public Money add(Money val, int precision, int roundingMode) throws IncompatibleCurrenciesException
      Returns a money amount whose value is the sum of this and val with a specified precision and rouding mode. Both money amounts must have the same currency.
      Parameters:
      val - The money to add.
      precision - The number of digits for the precision.
      roundingMode - The rounding mode.
      Returns:
      The sum of the money amounts.
      Throws:
      IncompatibleCurrenciesException - if the currencies of this and val are not equals.
    • subtract

      public Money subtract(Money val) throws IncompatibleCurrenciesException
      Returns a money amount whose value is the difference of this and val and whose internal precision is the maximum of the internal precisions of this and val. Both money amounts must have the same currency.
      Parameters:
      val - The value to subtract.
      Returns:
      The sum of the money amounts.
      Throws:
      IncompatibleCurrenciesException - if the currencies of this and val are not equals.
    • subtract

      public Money subtract(Money val, int precision, int roundingMode) throws IncompatibleCurrenciesException
      Returns a money amount whose value is the difference of this and val with a specified precision and rouding mode. Both money amounts must have the same currency.
      Parameters:
      val - The value to subtract.
      precision - The number of digits for the precision.
      roundingMode - The rounding mode.
      Returns:
      The difference of the money amounts.
      Throws:
      IncompatibleCurrenciesException - if the currencies of this and val are not equals.
    • multiply

      public Money multiply(BigDecimal val, int roundingMode)
      Returns a money amount whose value is the multiplication of this by a value, rounded through a given rounding mode.
      Parameters:
      val - The value to multiply by.
      roundingMode - The rounding mode.
      Returns:
      the multiplication of an amount by a constant.
    • multiply

      public Money multiply(BigDecimal val, int internalPrecision, int roundingMode)
      Returns a money amount whose value is the multiplication of this by a value, rounded through a given rounding mode and with a given limited precision.
      Parameters:
      val - The value to multiply by.
      internalPrecision - The precision to use for the multiplication.
      roundingMode - The rounding mode.
      Returns:
      the multiplication of an amount by a constant.
    • divide

      public Money divide(BigDecimal val, int roundingMode) throws ArithmeticException
      Returns a money amount whose value is the quotient of this by a value, rounded through a given rounding mode.
      Parameters:
      val - The value to divide by.
      roundingMode - The rounding mode.
      Returns:
      the quotient of an amount by a constant.
      Throws:
      ArithmeticException - if val is zero.
    • divide

      public Money divide(BigDecimal val, int internalPrecision, int roundingMode) throws ArithmeticException
      Returns a money amount whose value is the quotient of this by a value, rounded through a given rounding mode and with a given limited precision.
      Parameters:
      val - The value to divide by.
      internalPrecision - The precision of the result.
      roundingMode - The rounding mode.
      Returns:
      the quotient of an amount by a constant.
      Throws:
      ArithmeticException - if val is zero.
    • max

      public Money max(Money val) throws IncompatibleCurrenciesException
      Returns the maximum of this and val.
      Parameters:
      val - The value from which the maximum must be chosen.
      Returns:
      the quotient of an amount by a constant.
      Throws:
      IncompatibleCurrenciesException - if the currencies of this and val are not the same.
    • min

      public Money min(Money val) throws IncompatibleCurrenciesException
      Returns the minimum of this and val.
      Parameters:
      val - The value from which the minimum must be chosen.
      Returns:
      the quotient of an amount by a constant.
      Throws:
      IncompatibleCurrenciesException - if the currencies of this and val are not the same.
    • equals

      public boolean equals(Object moneyAmount)
      Returns true if amounts are equals (same currency and same value), false otherwise.
      Overrides:
      equals in class Object
      Parameters:
      moneyAmount - The amount to compare with.
      Returns:
      true if amounts are equals, false otherwise.
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • compareTo

      public int compareTo(Money val) throws IncompatibleCurrenciesException
      Returns 0 if amounts are equals (same currency and same value), -1, or 1.
      Parameters:
      val - The money to compare to.
      Returns:
      0, 1, or -1.
      Throws:
      IncompatibleCurrenciesException - if the currencies of this and val are not the same.
    • round

      public Money round(int roundingMode)
      Returns a new money amount whose value is rounded to the precision of its currency using the specified rounding mode.
      Parameters:
      roundingMode - The rounding mode.
      Returns:
      a rounded amount.
    • round

      public Money round(int precision, int roundingMode)
      Returns a new money amount whose value is rounded to the specified precision and rounding mode.
      Parameters:
      precision - the precision.
      roundingMode - the rounding mode.
      Returns:
      a rounded amount.
    • toString

      public String toString()
      Gives a string representation of the amount.
      Overrides:
      toString in class Object
      Returns:
      a string representation of the amount.
    • toEuro

      public Money toEuro() throws IncompatibleCurrenciesException
      Converts the money amount to Euro.
      Returns:
      the amount in euro.
      Throws:
      IncompatibleCurrenciesException - if currency of the money is not a EMU currency.
    • toEMUCurrency

      public Money toEMUCurrency(Currency toCurrency) throws IncompatibleCurrenciesException
      Converts the money amount to a EMU currency.
      Parameters:
      toCurrency - The target EMU currency.
      Returns:
      the converted amount.
      Throws:
      IncompatibleCurrenciesException - if the destination currency is not a AMU currency.
    • isMoneyFormat

      public static boolean isMoneyFormat(String s)
      Tests if the String s is in money format.
      Parameters:
      s - The string to check.
      Returns:
      true or false.
    • convertRounding

      public static int convertRounding(int rounding)
      Converts a money rounding into a big decimal rounding.
      Parameters:
      rounding - The money rounding.
      Returns:
      The corresponding money rounding.
    • getRounding

      public static int getRounding(String rounding)
      Returns the money rounding from a string representation of the rounding.
      Parameters:
      rounding - A string representation of the rounding.
      Returns:
      The money rounding mode, or -1 if there is no corresponding rounding mode.
    • getRounding

      public static String getRounding(int rounding)
      Returns the string representation of the rounding mode corresponding to a money rounding mode.
      Parameters:
      rounding - The string representation of the rounding mode.
      Returns:
      The string representation of the rounding mode, or null if the string does not match any rounding mode.