Class Money

java.lang.Object
de.calamanari.pk.money.Money
All Implemented Interfaces:
java.io.Serializable, java.lang.Comparable<Money>

public class Money
extends java.lang.Object
implements java.io.Serializable, java.lang.Comparable<Money>
Money - a MONEY demo implementation
Please be aware that this implementation has absolutely nothing to do with high performance or optimized memory consumption. :-)
You'll find full fledged and well tested Money libraries in the web.
Author:
Karl Eilebrecht
See Also:
Serialized Form
  • Field Summary

    Fields
    Modifier and Type Field Description
    static java.util.Currency CURRENCY_EUR
    Euro
    static java.util.Currency CURRENCY_USD
    USA-Dollar
  • Constructor Summary

    Constructors
    Constructor Description
    Money​(double value, java.lang.String currencyCode)
    Creates a Money instance from a double value with the given currency
    Money​(double value, java.util.Currency currency)
    Creates a Money instance from a double value with the given currency
    Money​(long value, java.lang.String currencyCode)
    Creates a Money instance from a long value with the given currency
    Money​(long value, java.util.Currency currency)
    Creates a Money instance from a long value with the given currency
    Money​(java.lang.String value)
    This constructor accepts the output of the toString()-method
    Money​(java.lang.String value, java.lang.String currencyCode)
    Creates a Money instance from a String with the given currency
    Money​(java.lang.String value, java.util.Currency currency)
    Creates a Money instance from a String with the given currency
    Money​(java.math.BigDecimal value, java.lang.String currencyCode)
    Creates a Money instance from a BigDecimal with the given currency
    Money​(java.math.BigDecimal value, java.util.Currency currency)
    Creates a Money instance from a BigDecimal with the given currency
  • Method Summary

    Modifier and Type Method Description
    Money add​(Money value)
    Creates a new money instance as the sum of this one plus the given one
    int compareTo​(Money o)  
    Money[] distribute​(double... weights)
    Distributes this value according to the given weights, means [1.0,1.0,1.0] will have the same effect as [10.0,10.0,10.0].
    A potential remainder will be distributed to the items from the highest weight to the lowest, ignoring 0.
    Money[] distribute​(java.math.BigDecimal[] weights)
    Distributes this value according to the given weights, means [1,1,1] will have the same effect as [10,10,10].
    A potential remainder will be distributed to the items from the highest weight to the lowest, ignoring 0.
    Money divide​(double divisor)
    Divides this instance by the given divisor and returns a new Money instance for the result.
    Money divide​(long divisor)
    Divides this instance by the given divisor and returns a new Money instance for the result.
    Money divide​(java.math.BigDecimal divisor)
    Divides this instance by the given divisor and returns a new Money instance for the result.
    Money divide​(java.math.BigInteger divisor)
    Divides this instance by the given divisor and returns a new Money instance for the result.
    boolean equals​(java.lang.Object obj)  
    java.math.BigDecimal getBigDecimalValue()
    Returns the corresponding BigDecimal value
    double getDoubleValue()
    Returns the corresponding double value
    int getSignum()
    Determines the signum of this money instance
    java.math.BigInteger getSmallestUnitValue()
    Returns the value represented using the smallest unit corresponding to the currency, i.e. 100 Cents for 1 Dollar
    int hashCode()  
    Money multiply​(double factor)
    Multiplies this instance with the given factor and returns a new Money instance for the result.
    Money multiply​(long factor)
    Multiplies this instance with the given factor and returns a new Money instance for the result.
    Money multiply​(java.math.BigDecimal factor)
    Multiplies this instance with the given factor and returns a new Money instance for the result.
    Money multiply​(java.math.BigInteger factor)
    Multiplies this instance with the given factor and returns a new Money instance for the result.
    Money negate()
    Returns a new money instance as the negative value of this one
    Money substract​(Money value)
    Creates a new money instance as this one minus the given one
    java.lang.String toString()
    Returns the string representation of this money instance of the form 'value currencyCode', the returned string may be used with the corresponding constructor to recreate the money instance

    Methods inherited from class java.lang.Object

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

    • CURRENCY_USD

      public static final java.util.Currency CURRENCY_USD
      USA-Dollar
    • CURRENCY_EUR

      public static final java.util.Currency CURRENCY_EUR
      Euro
  • Constructor Details

    • Money

      public Money​(java.math.BigDecimal value, java.util.Currency currency)
      Creates a Money instance from a BigDecimal with the given currency
      Parameters:
      value - instance's value
      currency - instance's currency
      Throws:
      java.lang.IllegalArgumentException - if the currency is not supported
    • Money

      public Money​(java.math.BigDecimal value, java.lang.String currencyCode)
      Creates a Money instance from a BigDecimal with the given currency
      Parameters:
      value - instance's value
      currencyCode - identifier for currency
      Throws:
      java.lang.IllegalArgumentException - if the currency is not supported
    • Money

      public Money​(double value, java.util.Currency currency)
      Creates a Money instance from a double value with the given currency
      Parameters:
      value - instance's value
      currency - the value's currency
      Throws:
      java.lang.IllegalArgumentException - if the currency is not supported
    • Money

      public Money​(double value, java.lang.String currencyCode)
      Creates a Money instance from a double value with the given currency
      Parameters:
      value - instance's value
      currencyCode - identifier for currency
      Throws:
      java.lang.IllegalArgumentException - if the currency is not supported
    • Money

      public Money​(long value, java.util.Currency currency)
      Creates a Money instance from a long value with the given currency
      Parameters:
      value - instance's value
      currency - instance's currency
      Throws:
      java.lang.IllegalArgumentException - if the currency is not supported
    • Money

      public Money​(long value, java.lang.String currencyCode)
      Creates a Money instance from a long value with the given currency
      Parameters:
      value - instance's value
      currencyCode - identifier for currency
      Throws:
      java.lang.IllegalArgumentException - if the currency is not supported
    • Money

      public Money​(java.lang.String value, java.util.Currency currency)
      Creates a Money instance from a String with the given currency
      Parameters:
      value - stringified value (double or long)
      currency - instance's currency
      Throws:
      java.lang.IllegalArgumentException - if the currency is not supported
    • Money

      public Money​(java.lang.String value, java.lang.String currencyCode)
      Creates a Money instance from a String with the given currency
      Parameters:
      value - stringified value (double or long)
      currencyCode - identifier for currency
      Throws:
      java.lang.IllegalArgumentException - if the currency is not supported
    • Money

      public Money​(java.lang.String value)
      This constructor accepts the output of the toString()-method
      Parameters:
      value - stringified Money instance
      Throws:
      java.lang.IllegalArgumentException - if the currency is not supported
  • Method Details

    • getBigDecimalValue

      public java.math.BigDecimal getBigDecimalValue()
      Returns the corresponding BigDecimal value
      Returns:
      big decimal value, precision limited related to currencies smallest unit
    • getDoubleValue

      public double getDoubleValue()
      Returns the corresponding double value
      Returns:
      double value, precision limited related to currencies smallest unit
    • getSmallestUnitValue

      public java.math.BigInteger getSmallestUnitValue()
      Returns the value represented using the smallest unit corresponding to the currency, i.e. 100 Cents for 1 Dollar
      Returns:
      value using smallest unit
    • getSignum

      public int getSignum()
      Determines the signum of this money instance
      Returns:
      -1, 0 or 1 for negative, zero or positive values
    • negate

      public Money negate()
      Returns a new money instance as the negative value of this one
      Returns:
      negative money instance
    • add

      public Money add​(Money value)
      Creates a new money instance as the sum of this one plus the given one
      Parameters:
      value - money to add (must have the same currency)
      Returns:
      new money instance
    • substract

      public Money substract​(Money value)
      Creates a new money instance as this one minus the given one
      Parameters:
      value - money to subtract (must have the same currency)
      Returns:
      new money instance
    • multiply

      public Money multiply​(java.math.BigDecimal factor)
      Multiplies this instance with the given factor and returns a new Money instance for the result.
      Parameters:
      factor - value to be multiplied
      Returns:
      result
    • multiply

      public Money multiply​(double factor)
      Multiplies this instance with the given factor and returns a new Money instance for the result.
      Parameters:
      factor - value to be multiplied
      Returns:
      result
    • multiply

      public Money multiply​(java.math.BigInteger factor)
      Multiplies this instance with the given factor and returns a new Money instance for the result.
      Parameters:
      factor - value to be multiplied
      Returns:
      result
    • multiply

      public Money multiply​(long factor)
      Multiplies this instance with the given factor and returns a new Money instance for the result.
      Parameters:
      factor - value to be multiplied
      Returns:
      result
    • divide

      public Money divide​(java.math.BigDecimal divisor)
      Divides this instance by the given divisor and returns a new Money instance for the result.
      Parameters:
      divisor - value to be used for division
      Returns:
      result
    • divide

      public Money divide​(double divisor)
      Divides this instance by the given divisor and returns a new Money instance for the result.
      Parameters:
      divisor - value to be used for division
      Returns:
      result
    • divide

      public Money divide​(java.math.BigInteger divisor)
      Divides this instance by the given divisor and returns a new Money instance for the result.
      Parameters:
      divisor - value to be used for division
      Returns:
      result
    • divide

      public Money divide​(long divisor)
      Divides this instance by the given divisor and returns a new Money instance for the result.
      Parameters:
      divisor - value to be used for division
      Returns:
      result
    • distribute

      public Money[] distribute​(java.math.BigDecimal[] weights)
      Distributes this value according to the given weights, means [1,1,1] will have the same effect as [10,10,10].
      A potential remainder will be distributed to the items from the highest weight to the lowest, ignoring 0.
      Parameters:
      weights - used to distribute this value
      Returns:
      array of money instances (distributed value of this instance)
    • distribute

      public Money[] distribute​(double... weights)
      Distributes this value according to the given weights, means [1.0,1.0,1.0] will have the same effect as [10.0,10.0,10.0].
      A potential remainder will be distributed to the items from the highest weight to the lowest, ignoring 0.
      Parameters:
      weights - used to distribute the value
      Returns:
      array of money instances (distributed value of this instance)
    • compareTo

      public int compareTo​(Money o)
      Specified by:
      compareTo in interface java.lang.Comparable<Money>
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class java.lang.Object
    • equals

      public boolean equals​(java.lang.Object obj)
      Overrides:
      equals in class java.lang.Object
    • toString

      public java.lang.String toString()
      Returns the string representation of this money instance of the form 'value currencyCode', the returned string may be used with the corresponding constructor to recreate the money instance
      Overrides:
      toString in class java.lang.Object
      Returns:
      debug string for value