Holger's Java API

## com.antelmann.math
Class Money

    java.lang.Object
      java.lang.Number
          com.antelmann.math.Money

All Implemented Interfaces:
  Addable<Money>, Measurable, HtmlSupported, Serializable, Comparable<Money>

    public final class Money

extends Number
implements Comparable<Money>, Addable<Money>, Measurable, HtmlSupported

represents an immutable monetary value that allows exact calculations on currencies. Money instances are immutable and always exact values rounded to the scale of the currency. The value is internally represented as a rounded BigDecimal with a scale corresponding to the given currency.

Since:
  20. April 2007, 15:33
Author:
  Holger Antelmann
See Also:
  `MoneyConverter`, `MoneyFormatFactory`, Serialized Form

Nested Class Summary

Nested classes/interfaces inherited from interface com.antelmann.math.Measurable
`Measurable.Adapter`

Field Summary
`static Money` `NO_MONEY` a convenient instance that equals new Money(BigDecimal.ZERO).

Constructor Summary
`Money()` Deprecated. use `NO_MONEY` instead
`Money(BigDecimal value)` uses the Currency of the default Locale
`Money(BigDecimal value, Currency currency)`
`Money(BigDecimal value, CurrencyUnit currency)` constructor that is called by all others
`Money(BigDecimal value, String currencyCode)`
`Money(double d)` convenience constructor using the default currency
`Money(Number value)`
`Money(Number number, Currency currency)`
`Money(Number value, CurrencyUnit currency)`

Method Summary
`Money` `abs()` returns Money with an absolute (non-negative) value
`Money` `add(BigDecimal bd)`
`Money` `add(double d)`
`Money` `add(Money m)` adds this instance to the other given one and returns the result; this entry itself remains untouched
`Money` `add(Number n)`

`static

`<P,T> HashMap<P,Money>`

`

`aggregate(Map<T,Money> data, PatternExtractor<T,P> pattern)`
utility function

`int` `compareTo(Money m)` only compares value of the same currency
`Money` `convertTo(Currency c, BigDecimal exchangeRate)` the given exchangeRage is multiplied with the value
`Money` `convertTo(Currency c, double exchangeRate)` the given exchangeRage is multiplied with the value
`Money` `divide(double divisor)`
`Money` `divide(Money divisor)`
`Money` `divide(Number divisor)`
`double` `doubleValue()`
`boolean` `equals(Object obj)` note that if the value is zero, it's considered the same even if the currency is different.
`float` `floatValue()`
`BigDecimal` `getAmount()` represents the quantity of this measurable entity
`Currency` `getCurrency()`
`static Money` `getSmallestPositiveAmountFor(Currency cur)`
`CurrencyUnit` `getUnit()` represents the unit type of this measurable entity
`BigDecimal` `getValue()` sames as `getAmount()`
`int` `hashCode()`
`int` `intValue()`
`boolean` `isCompatibleTo(Money money)` returns true only if the currency is the same or either money value is zero.
`boolean` `isNegative()`
`boolean` `isPositive()`
`boolean` `isZero()`
`long` `longValue()`
`Money` `multiply(double factor)`
`Money` `multiply(long factor)`
`Money` `multiply(Money m)`
`Money` `multiply(Number factor)`
`Money` `negate()`
`static BigDecimal` `round(Number n, Currency c)`
`Money` `roundToScale(Number scale)`
`int` `signum()`
`Money` `subtract(double d)`
`Money` `subtract(Money m)`
`Money` `subtract(Number b)`
`String` `toHtml(Locale l)` provides an HTML encoded String that uses `Strings.HTML_SPACE` to ensure that the amount is always displayed in a single line
`String` `toHtml(Object context)` returns a String that contains valid HTML to be embedded in other HTML content
`Measure<CurrencyUnit>` `toMeasure()`
`String` `toString()` uses the currency instance of the default Locale from NumberFormat and applies the applicable currency.
`String` `toString(Locale l)`

Methods inherited from class java.lang.Number
`byteValue, shortValue`

Methods inherited from class java.lang.Object
`clone, finalize, getClass, notify, notifyAll, wait, wait, wait`

Field Detail

### NO_MONEY

    public static final Money NO_MONEY

  a convenient instance that equals new Money(BigDecimal.ZERO).

  See Also:
    `equals(Object)`

Constructor Detail

### Money

    @Deprecated
    public Money()

  Deprecated. use `NO_MONEY` instead

  convenience constructor w/ zero money in the default currency

### Money

    public Money(double d)

  convenience constructor using the default currency

### Money

    public Money(Number value)

### Money

    public Money(BigDecimal value)

  uses the Currency of the default Locale

### Money

    public Money(Number number,
                 Currency currency)

### Money

    public Money(BigDecimal value,
                 String currencyCode)
          throws NullPointerException,
                 IllegalArgumentException

Parameters:
  `currencyCode` - the ISO 4217 code of the currency
Throws:
  `NullPointerException`
  `IllegalArgumentException`

### Money

    public Money(BigDecimal value,
                 Currency currency)

### Money

    public Money(Number value,
                 CurrencyUnit currency)

### Money

    public Money(BigDecimal value,
                 CurrencyUnit currency)

  constructor that is called by all others

Method Detail

### round

    public static BigDecimal round(Number n,
                                   Currency c)

### getAmount

    public final BigDecimal getAmount()

  Description copied from interface: `Measurable`
  represents the quantity of this measurable entity

  Specified by:
    `getAmount` in interface `Measurable`

### getUnit

    public final CurrencyUnit getUnit()

  Description copied from interface: `Measurable`
  represents the unit type of this measurable entity

  Specified by:
    `getUnit` in interface `Measurable`

### getValue

    public final BigDecimal getValue()

  sames as `getAmount()`

### doubleValue

    public final double doubleValue()

  Specified by:
    `doubleValue` in class `Number`

### getCurrency

    public final Currency getCurrency()

### isZero

    public final boolean isZero()

### isNegative

    public final boolean isNegative()

### isPositive

    public final boolean isPositive()

### toMeasure

    public Measure<CurrencyUnit> toMeasure()

### isCompatibleTo

    public boolean isCompatibleTo(Money money)

  returns true only if the currency is the same or either money value is zero.

### compareTo

    public int compareTo(Money m)
                  throws WrongCurrencyException

  only compares value of the same currency

  Specified by:
    `compareTo` in interface `Comparable<Money>`
  Throws:
    `WrongCurrencyException` - if the currency is not the same
  See Also:
    `MoneyConverter`

### equals

    public boolean equals(Object obj)

  note that if the value is zero, it's considered the same even if the currency is different. That is no Dollars are the same as no Euros - it's both no money at all either way.

  Overrides:
    `equals` in class `Object`

### hashCode

    public int hashCode()

  Overrides:
    `hashCode` in class `Object`

### toString

    public String toString()

  uses the currency instance of the default Locale from NumberFormat and applies the applicable currency.

  Overrides:
    `toString` in class `Object`

### toString

    public String toString(Locale l)

### toHtml

    public String toHtml(Object context)

  Description copied from interface: `HtmlSupported`
  returns a String that contains valid HTML to be embedded in other HTML content

  Specified by:
    `toHtml` in interface `HtmlSupported`
  Parameters:
    `context` - depending on the caller, this value may denote the servlet request, JSF context or a null value

### toHtml

    public String toHtml(Locale l)

  provides an HTML encoded String that uses `Strings.HTML_SPACE` to ensure that the amount is always displayed in a single line

  See Also:
    `Strings.encodeXML(String)`

### abs

    public final Money abs()

  returns Money with an absolute (non-negative) value

### signum

    public int signum()

### add

    public Money add(Money m)
              throws WrongCurrencyException

  Description copied from interface: `Addable`
  adds this instance to the other given one and returns the result; this entry itself remains untouched

  Specified by:
    `add` in interface `Addable<Money>`
  Throws:
    `WrongCurrencyException`

### add

    public Money add(Number n)

### add

    public final Money add(double d)

### add

    public final Money add(BigDecimal bd)

### subtract

    public Money subtract(Money m)
                   throws WrongCurrencyException

  Throws:
    `WrongCurrencyException`

### subtract

    public final Money subtract(Number b)

### subtract

    public final Money subtract(double d)

### multiply

    public Money multiply(double factor)

### multiply

    public Money multiply(long factor)

### multiply

    public Money multiply(Number factor)

### multiply

    public Money multiply(Money m)
                   throws WrongCurrencyException

  Throws:
    `WrongCurrencyException`

### divide

    public Money divide(double divisor)

### divide

    public Money divide(Number divisor)

### divide

    public Money divide(Money divisor)
                 throws WrongCurrencyException

  Throws:
    `WrongCurrencyException`

### negate

    public Money negate()

### roundToScale

    public Money roundToScale(Number scale)

### convertTo

    public Money convertTo(Currency c,
                           double exchangeRate)

  the given exchangeRage is multiplied with the value

### convertTo

    public Money convertTo(Currency c,
                           BigDecimal exchangeRate)

  the given exchangeRage is multiplied with the value

### floatValue

    public float floatValue()

  Specified by:
    `floatValue` in class `Number`

### intValue

    public int intValue()

  Specified by:
    `intValue` in class `Number`

### longValue

    public long longValue()

  Specified by:
    `longValue` in class `Number`

### aggregate

    public static <P,T> HashMap<P,Money> aggregate(Map<T,Money> data,
                                                   PatternExtractor<T,P> pattern)
                                      throws WrongCurrencyException

  utility function

  Throws:
    `WrongCurrencyException`

### getSmallestPositiveAmountFor

    public static Money getSmallestPositiveAmountFor(Currency cur)

(c) Holger Antelmann since 2001- all rights reserved (contact: info@antelmann.com)
see www.antelmann.com/developer for further details and available downloads