Package com.storedobject.core
Class Money
java.lang.Object
com.storedobject.core.Money
- All Implemented Interfaces:
BigValue
,Storable
,Comparable<Money>
Class that represents a monetary value. This class is immutable.
- Author:
- Syam
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic class
A class representing a list of currency values. -
Constructor Summary
ConstructorsConstructorDescriptionMoney()
Create a monetary value of zero in the default currency.Create a monetary value from the amount specified in a particular currency.Create a monetary value from the amount specified in a particular currency.Create a monetary value from the amount specified in the default currency.Create a monetary value from the amount specified in a particular currency.Create a monetary value from the amount specified in a particular currency.Create a monetary value of zero in a particular currency.Create a monetary value from the amount specified in a particular currency.Create a monetary value from the amount specified in a particular currency.Money
(BigDecimal value) Create a monetary value from the amount specified in the default currency.Money
(BigDecimal amount, String currencyCode) Create a monetary value from the amount specified in a particular currency.Money
(BigDecimal value, Currency currency) Create a monetary value from the amount specified in a particular currency.Create a monetary value of zero in a particular currency. -
Method Summary
Modifier and TypeMethodDescriptionabsolute()
Return the monetary value containing the absolute value.Add amountAdd amount valueadd
(BigDecimal amount) Add amountConvert this monetary value to another currency by applying the buying rate.int
Divide the amount with an "exchange rate" to convert it into another currency value.Convert this monetary value to another currency (by applying the average rate).static Money
Construct a monetary value from a string representation.currencies
(boolean includeMetals) divide
(double divisor) Divide the amount with a valueDivide the amount with a quantity (To find the unit price)Divide the amount with a ratedivide
(BigDecimal divisor) Divide the amount with a valueboolean
getBuyingRate
(Currency currency) Gets the selling rate.getBuyingRate
(Currency currency, SystemEntity systemEntity) Gets the buying rate.static Rate
getBuyingRate
(Currency from, Currency to) Gets the selling rate.static Rate
getBuyingRate
(Currency from, Currency to, SystemEntity systemEntity) Gets the buying rate.Get the currency of this monetary value.static Currency
getCurrency
(String code) Get currency for the given code.static Currency
getCurrency
(String code, boolean includeMetals) Get currency for the given code.int
Gets the number of decimals.static Currency
Get the default currency.static Currency
Gets the default currency for the library user when SO Platform is not active.getExchangeRate
(Currency currency) Gets the exchange rate (average of selling rate and buying rate).getExchangeRate
(Currency currency, SystemEntity systemEntity) Gets the exchange rate (average of selling rate and buying rate).static Rate
getExchangeRate
(Currency from, Currency to) Gets the exchange rate (average of selling rate and buying rate).static Rate
getExchangeRate
(Currency from, Currency to, SystemEntity systemEntity) Gets the exchange rate (average of selling rate and buying rate).int
static int
getFractionDigits
(Currency currency) getSellingRate
(Currency currency) Gets the selling rate.getSellingRate
(Currency currency, SystemEntity systemEntity) Gets the selling rate.static Rate
getSellingRate
(Currency from, Currency to) Gets the selling rate.static Rate
getSellingRate
(Currency from, Currency to, SystemEntity systemEntity) Gets the selling rate.Get the storable value.static String
Get the symbol for the given currency.getValue()
Get the monetary value as BigDecimal.int
hashCode()
boolean
isCredit()
Checks if this is a credit amount or not.boolean
isDebit()
Checks if this is a debit amount or not.boolean
isGreaterThan
(long value) Check if this is greater than 'value'.boolean
isGreaterThan
(Money another) Check if this is greater than 'another'.boolean
isGreaterThanOrEqual
(long value) Check if this is greater than or equal to 'value'.boolean
isGreaterThanOrEqual
(Money another) Check if this is greater than or equal to 'another'.boolean
isLessThan
(long value) Check if this is less than 'value'.boolean
isLessThan
(Money another) Check if this is less than 'another'.boolean
isLessThanOrEqual
(long value) Check if this is less than or equal to 'value'.boolean
isLessThanOrEqual
(Money another) Check if this is less than or equal to 'another'.static boolean
static boolean
Is the platform running in a multi-currency (and multi-tenancy) environment?boolean
boolean
Checks if this is a positive amount or not.boolean
isZero()
Checks if this is zero or not.multiply
(double multiplicand) Multiply the amount with a valuemultiply
(DecimalNumber multiplicand) Multiply the amount with a rateMultiply the amount with a quantity (Given the unit price, find out the total amount).Multiply the amount with a ratemultiply
(BigDecimal multiplicand) Multiply the amount with a valuenegate()
Reverses the sign of this money.percentage
(double percentage) Calculate the percentage.percentage
(DecimalNumber percentage) Calculate the percentage.percentage
(Rate percentage) Calculate the percentage.percentage
(BigDecimal percentage) Calculate the percentage.round()
Round the amount to the lower/upper value.Round the amount to the lower value.roundUp()
Round the amount to the upper value.Convert this monetary value to another currency by applying the selling rate.static void
setDefaultUserCurrency
(Currency defaultUserCurrency) Sets the default currency for the library user when SO Platform is not active.Subtract amountSubtract amountsubtract
(BigDecimal amount) Subtract amounttoLocal
(Rate exchangeRate, SystemEntity systemEntity) Convert this monetary value to the equivalent in local currency by applying the given "exchange rate".toLocal
(Rate exchangeRate, TransactionManager tm) Convert this monetary value to the equivalent in local currency by applying the given "exchange rate".toLocal
(SystemEntity systemEntity) Convert this monetary value to the equivalent in local currency by applying the current "exchange rate".Convert this monetary value to the equivalent in local currency by applying the current "exchange rate".toLocal
(Date date, SystemEntity systemEntity) Convert this monetary value to the equivalent in local currency by applying the current "exchange rate".toLocal
(Date date, TransactionManager tm) Convert this monetary value to the equivalent in local currency by applying the current "exchange rate".toString()
Create the formatted representation of the amount value with appropriate decimals places.toString
(boolean showSymbol) Create the formatted representation of the amount value with appropriate decimals places.toString
(boolean showSymbol, boolean tagDebit) Create the formatted representation of the amount value with appropriate decimals places.toString
(boolean showSymbol, boolean tagDebit, SystemUser forUser) Create the formatted representation of the amount value with appropriate decimals places.toString
(boolean showSymbol, SystemUser forUser) Create the formatted representation of the amount value with appropriate decimals places.toString
(SystemUser forUser) Create the formatted representation of the amount value with appropriate decimals places.boolean
Whether a rounding took place when this was created or not.words()
Get the currency amount in words.words
(boolean camelCase) Get the currency amount in words.words
(SystemUser forUser) Get the currency amount in words.words
(SystemUser forUser, boolean camelCase) Get the currency amount in words.zero()
Set the monetary value to zero.
-
Constructor Details
-
Money
public Money()Create a monetary value of zero in the default currency. -
Money
Create a monetary value of zero in a particular currency.- Parameters:
currencyCode
- The currency code (ISO standard)
-
Money
-
Money
Create a monetary value from the amount specified in a particular currency.- Parameters:
amount
- The amountcurrencyCode
- The currency code (ISO standard)
-
Money
Create a monetary value of zero in a particular currency.- Parameters:
currency
- Currency
-
Money
-
Money
Create a monetary value from the amount specified in a particular currency.- Parameters:
value
- The BigDecimal value.currency
- Currency
-
Money
Create a monetary value from the amount specified in a particular currency.- Parameters:
value
- The money value. The value will be rounded (half-way up) if required.currency
- Currency
-
Money
Create a monetary value from the amount specified in a particular currency.- Parameters:
value
- The money value. The value will be rounded (half-way up) if required.currencyCode
- The currency code (ISO standard)
-
Money
-
Money
-
Money
Create a monetary value from the amount specified in the default currency.- Parameters:
value
- The amount.
-
Money
Create a monetary value from the amount specified in the default currency.- Parameters:
value
- The amount.
-
-
Method Details
-
getDefaultCurrency
Get the default currency. If this is invoked in a multi-currency environment, a runtime exception will be thrown.- Returns:
- Default currency.
-
getDefaultUserCurrency
Gets the default currency for the library user when SO Platform is not active.- Returns:
- the default user currency
-
setDefaultUserCurrency
Sets the default currency for the library user when SO Platform is not active.- Parameters:
defaultUserCurrency
- the currency to set as default for the user
-
isMultiCurrency
public static boolean isMultiCurrency()Is the platform running in a multi-currency (and multi-tenancy) environment? A multi-currency environment is an environment where multiple system entities exist and not all of them have the same accounting currency.- Returns:
- True/false.
-
create
-
getCurrency
-
getCurrency
-
getDecimals
public int getDecimals()Gets the number of decimals.- Returns:
- No. of decimals
-
getValue
Get the monetary value as BigDecimal. -
getCurrency
-
zero
-
add
-
add
Add amount- Parameters:
amount
- The amount value to add- Returns:
- Result
-
add
-
getBuyingRate
-
getSellingRate
-
getExchangeRate
-
getBuyingRate
Gets the buying rate.Note: To convert to the target currency, you need to divide the monetary value with this rate.
- Parameters:
currency
- Currency for which rate is required.systemEntity
- System Entity.- Returns:
- Rate
-
getSellingRate
Gets the selling rate.Note: To convert to the target currency, you need to divide the monetary value with this rate.
- Parameters:
currency
- Currency for which rate is required.systemEntity
- System Entity.- Returns:
- Rate
-
getExchangeRate
Gets the exchange rate (average of selling rate and buying rate).Note: To convert to the target currency, you need to divide the monetary value with this rate.
- Parameters:
currency
- Currency for which rate is required.systemEntity
- System Entity.- Returns:
- Rate
-
getBuyingRate
-
getSellingRate
-
getExchangeRate
Gets the exchange rate (average of selling rate and buying rate).Note: To convert to the target currency, you need to divide the monetary value with this rate.
- Parameters:
from
- Currency for which rate is required.to
- Target currency to convert to.- Returns:
- Rate
-
getBuyingRate
Gets the buying rate.Note: To convert to the target currency, you need to divide the monetary value with this rate.
- Parameters:
from
- Currency for which rate is required.to
- Target currency to convert to.systemEntity
- System Entity.- Returns:
- Rate
-
getSellingRate
Gets the selling rate.Note: To convert to the target currency, you need to divide the monetary value with this rate.
- Parameters:
from
- Currency for which rate is required.to
- Target currency to convert to.systemEntity
- System Entity.- Returns:
- Rate
-
getExchangeRate
Gets the exchange rate (average of selling rate and buying rate).Note: To convert to the target currency, you need to divide the monetary value with this rate.
- Parameters:
from
- Currency for which rate is required.to
- Target currency to convert to.systemEntity
- System Entity.- Returns:
- Rate
-
convert
-
buy
-
sell
-
subtract
-
subtract
Subtract amount- Parameters:
amount
- The amount value to subtract- Returns:
- Result
-
subtract
-
multiply
-
multiply
Multiply the amount with a value- Parameters:
multiplicand
- Multiplicand- Returns:
- Result
-
multiply
Multiply the amount with a value- Parameters:
multiplicand
- Multiplicand- Returns:
- Result
-
multiply
Multiply the amount with a rate- Parameters:
multiplicand
- Multiplicand- Returns:
- Result
-
multiply
-
convert
-
toLocal
Convert this monetary value to the equivalent in local currency by applying the given "exchange rate". Unlike theconvert(Rate, Currency)
method, this method multiplies the monetary value with the exchange rate.- Parameters:
exchangeRate
- Exchange ratetm
- Transaction manager (Local currency will be determined from this)- Returns:
- Result
-
toLocal
Convert this monetary value to the equivalent in local currency by applying the given "exchange rate". Unlike theconvert(Rate, Currency)
method, this method multiplies the monetary value with the exchange rate.- Parameters:
exchangeRate
- Exchange ratesystemEntity
- System entity (Local currency will be determined from this)- Returns:
- Result
-
toLocal
Convert this monetary value to the equivalent in local currency by applying the current "exchange rate". Unlike theconvert(Rate, Currency)
method, this method multiplies the monetary value with the exchange rate.- Parameters:
tm
- Transaction manager (Local currency will be determined from this)- Returns:
- Result
-
toLocal
Convert this monetary value to the equivalent in local currency by applying the current "exchange rate". Unlike theconvert(Rate, Currency)
method, this method multiplies the monetary value with the exchange rate.- Parameters:
date
- Effective date.tm
- Transaction manager (Local currency will be determined from this)- Returns:
- Result
-
toLocal
Convert this monetary value to the equivalent in local currency by applying the current "exchange rate". Unlike theconvert(Rate, Currency)
method, this method multiplies the monetary value with the exchange rate.- Parameters:
systemEntity
- System entity (Local currency will be determined from this)- Returns:
- Result
-
toLocal
Convert this monetary value to the equivalent in local currency by applying the current "exchange rate". Unlike theconvert(Rate, Currency)
method, this method multiplies the monetary value with the exchange rate.- Parameters:
date
- Effective date.systemEntity
- System entity (Local currency will be determined from this)- Returns:
- Result
-
percentage
Calculate the percentage.- Parameters:
percentage
- Percentage- Returns:
- Result
-
percentage
Calculate the percentage.- Parameters:
percentage
- Percentage- Returns:
- Result
-
percentage
Calculate the percentage.- Parameters:
percentage
- Percentage- Returns:
- Result
-
percentage
-
divide
Divide the amount with a value- Parameters:
divisor
- Divisor- Returns:
- Result
-
divide
Divide the amount with a value- Parameters:
divisor
- Divisor- Returns:
- Result
-
divide
-
divide
-
negate
-
absolute
Return the monetary value containing the absolute value.- Returns:
- Absolute value
-
round
Round the amount to the lower/upper value. If the decimal part is greater than or equal to 0.5, it will be rounded up, otherwise, it will be rounded down.- Returns:
- Rounded value.
-
roundUp
-
roundDown
-
equals
-
isGreaterThan
Check if this is greater than 'another'.- Parameters:
another
- Value to compare.- Returns:
- True if this is greater.
-
isGreaterThan
public boolean isGreaterThan(long value) Check if this is greater than 'value'.- Parameters:
value
- Value to compare.- Returns:
- True if this is greater.
-
isLessThan
Check if this is less than 'another'.- Parameters:
another
- Value to compare.- Returns:
- True if this is less.
-
isLessThan
public boolean isLessThan(long value) Check if this is less than 'value'.- Parameters:
value
- Value to compare.- Returns:
- True if this is less.
-
isGreaterThanOrEqual
Check if this is greater than or equal to 'another'.- Parameters:
another
- Value to compare.- Returns:
- True if this is greater than or equal.
-
isGreaterThanOrEqual
public boolean isGreaterThanOrEqual(long value) Check if this is greater than or equal to 'value'.- Parameters:
value
- Value to compare.- Returns:
- True if this is greater or equal.
-
isLessThanOrEqual
Check if this is less than or equal to 'another'.- Parameters:
another
- Value to compare.- Returns:
- True if this is less than or equal.
-
isLessThanOrEqual
public boolean isLessThanOrEqual(long value) Check if this is less than or equal to 'value'.- Parameters:
value
- Value to compare.- Returns:
- True if this is less than or equal.
-
isZero
public boolean isZero()Checks if this is zero or not.- Returns:
- True if zero.
-
isDebit
public boolean isDebit()Checks if this is a debit amount or not.- Returns:
- True if negative.
-
isNegative
public boolean isNegative() -
isCredit
public boolean isCredit()Checks if this is a credit amount or not. Same asisPositive()
.- Returns:
- True if positive.
-
isPositive
public boolean isPositive()Checks if this is a positive amount or not.- Returns:
- True if positive.
-
hashCode
-
toString
-
toString
Create the formatted representation of the amount value with appropriate decimals places.- Returns:
- The formatted string representation of the amount.
-
toString
Create the formatted representation of the amount value with appropriate decimals places.- Parameters:
showSymbol
- Determines whether currency symbol should be included or not.- Returns:
- The formatted string representation of the amount.
-
toString
Create the formatted representation of the amount value with appropriate decimals places.- Parameters:
showSymbol
- Determines whether currency symbol should be included or not.- Returns:
- The formatted string representation of the amount.
-
toString
Create the formatted representation of the amount value with appropriate decimals places.- Parameters:
showSymbol
- Determines whether currency symbol should be included or not.tagDebit
- Add "DB" tag for negative (debit) amount or not.- Returns:
- The formatted string representation of the amount.
-
toString
Create the formatted representation of the amount value with appropriate decimals places.- Parameters:
showSymbol
- Determines whether currency symbol should be included or not.tagDebit
- Add "DB" tag for negative (debit) amount or not.forUser
- For the user (if non-null, user's locale country will be used for formatting).- Returns:
- The formatted string representation of the amount.
-
getSymbol
-
getStorableValue
Description copied from interface:Storable
Get the storable value. By default,Object.toString()
is returned.- Specified by:
getStorableValue
in interfaceStorable
- Returns:
- Storable value as a string.
-
compareTo
- Specified by:
compareTo
in interfaceComparable<Money>
-
words
-
words
Get the currency amount in words.- Parameters:
camelCase
- Whether to show on camel-case or not.- Returns:
- Currency amount in words.
-
words
Get the currency amount in words.- Parameters:
forUser
- For the user (if non-null, user's locale country will be used for formatting).- Returns:
- Currency amount in words.
-
words
Get the currency amount in words.- Parameters:
forUser
- For the user (if non-null, user's locale country will be used for formatting).camelCase
- Whether to show on camel-case or not.- Returns:
- Currency amount in words.
-
wasRounded
public boolean wasRounded()Whether a rounding took place when this was created or not.- Returns:
- True/false.
-
currencies
-
currencies
-
isMetal
-
getFractionDigits
public int getFractionDigits() -
getFractionDigits
-