Class MonetaryValue

java.lang.Object
rs.baselib.type.MonetaryValue
All Implemented Interfaces:
Serializable, Comparable<MonetaryValue>

public class MonetaryValue extends Object implements Serializable, Comparable<MonetaryValue>
A BigDecimal configured for monetary operations.

The BigDecimal is configured with a precision scale of 2, RoundingMode.HALF_UP.

Since:
1.2.9
Author:
ralph
See Also:
  • Field Details

  • Constructor Details

    • MonetaryValue

      @Deprecated public MonetaryValue(BigDecimal value)
      Deprecated.
      use a specific currency MonetaryValue(BigDecimal, Currency).
      Constructor.
      Parameters:
      value - the monetary value
    • MonetaryValue

      public MonetaryValue(BigDecimal value, Currency currency)
      Constructor.
      Parameters:
      value - the monetary value
      currency - the currency (can be null, default is currency of default locale)
    • MonetaryValue

      public MonetaryValue(MonetaryValue value)
      Constructor.
      Parameters:
      value - the monetary value
    • MonetaryValue

      @Deprecated public MonetaryValue(BigDecimal value, RoundingMode roundingMode)
      Deprecated.
      Constructor.
      Parameters:
      value - the monetary value
      roundingMode - the rounding mode to be used
    • MonetaryValue

      public MonetaryValue(BigDecimal value, Currency currency, RoundingMode roundingMode)
      Constructor.
      Parameters:
      value - the monetary value
      roundingMode - the rounding mode to be used
      currency - the currency (can be null, default is currency of default locale)
    • MonetaryValue

      @Deprecated public MonetaryValue(BigDecimal value, RoundingMode roundingMode, MathContext mathContext, int scale)
      Constructor.
      Parameters:
      value - the monetary value
      roundingMode - the rounding mode to be used
      mathContext - the MathContext to be used
      scale - the scale to be used
    • MonetaryValue

      public MonetaryValue(BigDecimal value, Currency currency, RoundingMode roundingMode, MathContext mathContext, int scale)
      Constructor.
      Parameters:
      value - the monetary value
      currency - the currency (can be null, default is currency of default locale)
      roundingMode - the rounding mode to be used
      mathContext - the MathContext to be used
      scale - the scale to be used
    • MonetaryValue

      @Deprecated public MonetaryValue(CharSequence value)
      Deprecated.
      use a specific currency MonetaryValue(CharSequence, Currency).
      Constructor.
      Parameters:
      value - the monetary value
    • MonetaryValue

      public MonetaryValue(CharSequence value, Currency currency)
      Constructor.
      Parameters:
      value - the monetary value
      currency - the currency (can be null, default is currency of default locale)
    • MonetaryValue

      public MonetaryValue(double value)
      Deprecated.
      use a specific currency MonetaryValue(double, Currency).
      Constructor.
      Parameters:
      value - the monetary value
    • MonetaryValue

      public MonetaryValue(double value, Currency currency)
      Constructor.
      Parameters:
      value - the monetary value
      currency - the currency (can be null, default is currency of default locale)
    • MonetaryValue

      public MonetaryValue(BigInteger value)
      Deprecated.
      use a specific currency MonetaryValue(BigInteger, Currency).
      Constructor.
      Parameters:
      value - the monetary value
    • MonetaryValue

      public MonetaryValue(BigInteger value, Currency currency)
      Constructor.
      Parameters:
      value - the monetary value
      currency - the currency (can be null, default is currency of default locale)
    • MonetaryValue

      public MonetaryValue(int value)
      Deprecated.
      use a specific currency MonetaryValue(int, Currency).
      Constructor.
      Parameters:
      value - the monetary value
    • MonetaryValue

      public MonetaryValue(int value, Currency currency)
      Constructor.
      Parameters:
      value - the monetary value
      currency - the currency (can be null, default is currency of default locale)
    • MonetaryValue

      public MonetaryValue(long value)
      Deprecated.
      use a specific currency MonetaryValue(long, Currency).
      Constructor.
      Parameters:
      value - the monetary value
    • MonetaryValue

      public MonetaryValue(long value, Currency currency)
      Constructor.
      Parameters:
      value - the monetary value
      currency - the currency (can be null, default is currency of default locale)
  • Method Details

    • getAmount

      public BigDecimal getAmount()
      Returns the amount.
      Returns:
      the amount
    • setAmount

      public void setAmount(BigDecimal amount)
      Sets the amount.
      Parameters:
      amount - the amount to set
    • getCurrency

      public Currency getCurrency()
      Returns the currency.
      Returns:
      the currency
    • setCurrency

      public void setCurrency(Currency currency)
      Sets the currency.
      Parameters:
      currency - the currency (can be null, default is currency of default locale)
    • getRoundingMode

      public RoundingMode getRoundingMode()
      Returns the rounding mode.
      Returns:
      the rounding mode
    • setRoundingMode

      public void setRoundingMode(RoundingMode roundingMode)
      Sets the rounding mode.
      Parameters:
      roundingMode - the rounding mode to set
    • getScale

      public int getScale()
      Returns the scale.
      Returns:
      the scale
    • setScale

      public void setScale(int scale)
      Sets the scale.
      Parameters:
      scale - the scale to set
    • getMathContext

      public MathContext getMathContext()
      Returns the MathContext.
      Returns:
      the mathContext
    • setMathContext

      public void setMathContext(MathContext mathContext)
      Sets the MathContext.
      Parameters:
      mathContext - the mathContext to set
    • isPositive

      public boolean isPositive()
      Is value positive?
      Returns:
      true when value is a positive number
    • isNegative

      public boolean isNegative()
      Is value negative?
      Returns:
      true when value is a negative number
    • isZero

      public boolean isZero()
      Is value positive?
      Returns:
      true when value is a positive number
    • add

      public MonetaryValue add(MonetaryValue value)
      Adds another value to this object
      Parameters:
      value - value to be added
      Returns:
      new monetary value holding the result
    • add

      public MonetaryValue add(BigDecimal value)
      Adds another value to this object
      Parameters:
      value - value to be added
      Returns:
      new monetary value holding the result
    • add

      public MonetaryValue add(int value)
      Adds another value to this object
      Parameters:
      value - value to be added
      Returns:
      new monetary value holding the result
    • add

      public MonetaryValue add(long value)
      Adds another value to this object
      Parameters:
      value - value to be added
      Returns:
      new monetary value holding the result
    • add

      public MonetaryValue add(double value)
      Adds another value to this object
      Parameters:
      value - value to be added
      Returns:
      new monetary value holding the result
    • add

      public MonetaryValue add(BigInteger value)
      Adds another value to this object
      Parameters:
      value - value to be added
      Returns:
      new monetary value holding the result
    • add

      public MonetaryValue add(CharSequence value)
      Adds another value to this object
      Parameters:
      value - value to be added
      Returns:
      new monetary value holding the result
    • subtract

      public MonetaryValue subtract(MonetaryValue value)
      Subtracts another value from this object.
      Parameters:
      value - value to be subtracted
      Returns:
      new monetary value holding the result
    • subtract

      public MonetaryValue subtract(BigDecimal value)
      Subtracts another value from this object.
      Parameters:
      value - value to be subtracted
      Returns:
      new monetary value holding the result
    • subtract

      public MonetaryValue subtract(int value)
      Subtracts another value from this object.
      Parameters:
      value - value to be subtracted
      Returns:
      new monetary value holding the result
    • subtract

      public MonetaryValue subtract(long value)
      Subtracts another value from this object.
      Parameters:
      value - value to be subtracted
      Returns:
      new monetary value holding the result
    • subtract

      public MonetaryValue subtract(double value)
      Subtracts another value from this object.
      Parameters:
      value - value to be subtracted
      Returns:
      new monetary value holding the result
    • subtract

      public MonetaryValue subtract(BigInteger value)
      Subtracts another value from this object.
      Parameters:
      value - value to be subtracted
      Returns:
      new monetary value holding the result
    • subtract

      public MonetaryValue subtract(CharSequence value)
      Subtracts another value from this object.
      Parameters:
      value - value to be subtracted
      Returns:
      new monetary value holding the result
    • multiply

      public MonetaryValue multiply(BigDecimal value)
      Multiplies another value with this object.
      Parameters:
      value - value to be multiplied with
      Returns:
      new monetary value holding the result
    • multiply

      public MonetaryValue multiply(int value)
      Multiplies another value with this object.
      Parameters:
      value - value to be multiplied with
      Returns:
      new monetary value holding the result
    • multiply

      public MonetaryValue multiply(long value)
      Multiplies another value with this object.
      Parameters:
      value - value to be multiplied with
      Returns:
      new monetary value holding the result
    • multiply

      public MonetaryValue multiply(double value)
      Multiplies another value with this object.
      Parameters:
      value - value to be multiplied with
      Returns:
      new monetary value holding the result
    • multiply

      public MonetaryValue multiply(BigInteger value)
      Multiplies another value with this object.
      Parameters:
      value - value to be multiplied with
      Returns:
      new monetary value holding the result
    • multiply

      public MonetaryValue multiply(CharSequence value)
      Multiplies another value with this object.
      Parameters:
      value - value to be multiplied with
      Returns:
      new monetary value holding the result
    • divide

      public MonetaryValue divide(BigDecimal value)
      Divides this object by another value.
      Parameters:
      value - value to be divided by
      Returns:
      new monetary value holding the result
    • divide

      public MonetaryValue divide(int value)
      Divides this object by another value.
      Parameters:
      value - value to be divided by
      Returns:
      new monetary value holding the result
    • divide

      public MonetaryValue divide(long value)
      Divides this object by another value.
      Parameters:
      value - value to be divided by
      Returns:
      new monetary value holding the result
    • divide

      public MonetaryValue divide(double value)
      Divides this object by another value.
      Parameters:
      value - value to be divided by
      Returns:
      new monetary value holding the result
    • divide

      public MonetaryValue divide(BigInteger value)
      Divides this object by another value.
      Parameters:
      value - value to be divided by
      Returns:
      new monetary value holding the result
    • divide

      public MonetaryValue divide(CharSequence value)
      Divides this object by another value.
      Parameters:
      value - value to be divided by
      Returns:
      new monetary value holding the result
    • divide

      public BigDecimal divide(MonetaryValue value)
      Divides this object by another value.
      Parameters:
      value - value to be divided by
      Returns:
      the result of the operation
    • negate

      public MonetaryValue negate()
      Negates this value.
      Returns:
      new monetary value holding the result
    • getDouble

      public double getDouble()
      Returns this value as a double.
      Returns:
      value as double
      See Also:
    • getFloat

      public float getFloat()
      Returns this value as a float.
      Returns:
      value as float
      See Also:
    • getInt

      public int getInt()
      Returns this value as int.
      Returns:
      value as int
      See Also:
    • getBigInteger

      public BigInteger getBigInteger()
      Returns this value as BigInteger.
      Returns:
      value as BigInteger
      See Also:
    • getLong

      public long getLong()
      Returns this value as a long.
      Returns:
      value as long
      See Also:
    • compareTo

      public int compareTo(MonetaryValue o)
      Specified by:
      compareTo in interface Comparable<MonetaryValue>
    • hashCode

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

      public boolean equals(Object other)
      Overrides:
      equals in class Object
    • toString

      public String toString()
      The representation of the value as string
      Overrides:
      toString in class Object
      Returns:
      e.g. "12.00" or "0.00" or "-2345.34"
    • zero

      public static MonetaryValue zero(Currency currency)
      Creates an amount of zero in the given locale.
      Parameters:
      currency - the currency (can be null, default is currency of default locale)
      Returns:
      the zero amount.