Package com.broadleafcommerce.money.util
Class MonetaryUtils
java.lang.Object
com.broadleafcommerce.money.util.MonetaryUtils
Convenience methods for dealing with the Monetary API
- Author:
- Phillip Verheyden (phillipuniverse)
-
Method Summary
Modifier and TypeMethodDescriptionstatic javax.money.CurrencyUnit
Obtains the default system currencystatic javax.money.CurrencyUnit
getCurrency
(String currencyCode) Takes a currency code and returns aCurrencyUnit
.static javax.money.CurrencyUnit
getCurrency
(Currency currency) Converts aCurrency
object into aCurrencyUnit
.static javax.money.CurrencyUnit
getCurrency
(Locale locale) Get theCurrencyUnit
for theLocale
providedstatic boolean
isEqualByComparison
(javax.money.MonetaryAmount amount1, Number amount2) Tests if a MonetaryAmount is equal to the BigDecimal being passed in by looking at each objects doubleValuestatic boolean
isEqualByComparison
(javax.money.MonetaryAmount amount1, String amount2) Tests if a MonetaryAmount is equal to the String being passed in by first converting the String to a BigDecimal and then calling theisEqualByComparison(MonetaryAmount, Number)
method.static boolean
isEqualByComparison
(javax.money.MonetaryAmount amount1, javax.money.MonetaryAmount amount2) Tests whether twoMonetaryAmount
objects are equal by comparison, after rounding is applied based on currency.static boolean
Tests whether theMonetaryAmount
value is equal to theNumber
provided, rounded according to the currency.static boolean
isEqualToZero
(javax.money.MonetaryAmount amount) Tests whether the providedMonetaryAmount
is equal to zero after rounding based on the currency.static boolean
isGreaterThan
(javax.money.MonetaryAmount amount, Number value) Tests whether theMonetaryAmount
value is greater than theNumber
provided, rounded according to the currency.static boolean
isGreaterThanZero
(javax.money.MonetaryAmount amount) Tests whether the providedMonetaryAmount
is greater than zero after rounding based on the currency.static boolean
isLessThan
(javax.money.MonetaryAmount amount, Number value) Tests whether theMonetaryAmount
value is less than theNumber
provided, rounded according to the currency.static boolean
isLessThanZero
(javax.money.MonetaryAmount amount) Tests whether the providedMonetaryAmount
is less than zero after rounding based on the currency.static javax.money.MonetaryAmount
round
(javax.money.MonetaryAmount amount) Rounds the passed in amount using the scale of its currency and a default rounding mode.static javax.money.MonetaryAmount
round
(javax.money.MonetaryAmount amount, int scale) Performs rounding to the specified scale andRoundingMode.HALF_EVEN
used for the rounding model.static javax.money.MonetaryAmount
round
(javax.money.MonetaryAmount amount, int scale, RoundingMode roundingMode) Performs rounding on the amount with a specified scale and roundingMode.static javax.money.MonetaryAmount
round
(javax.money.MonetaryAmount amount, RoundingMode roundingMode) Rounds the passed in amount using the scale of its currency and the passed in RoundingModeRoundingMode.HALF_EVEN
is considered "Banker's Rounding" and this is what is used.static javax.money.MonetaryAmount
Converts the givenBigDecimal
to aMonetaryAmount
with the default system currencystatic javax.money.MonetaryAmount
toAmount
(Number value, CurrencyContext ctx) static javax.money.MonetaryAmount
Converts the givenBigDecimal
to aMonetaryAmount
with the currency for the given currency code.static javax.money.MonetaryAmount
static <T extends javax.money.MonetaryAmount>
javax.money.MonetaryAmountReturns an instance ofMonetaryAmount
.static javax.money.MonetaryAmount
Converts the given passed in String to avalue
and then builds anmoney
using the value and then converts the passed in currencyCode (e.g.static javax.money.MonetaryAmount
Converts the given passed in String to avalue
and then builds anmoney
using the value and specifiedcurrency
.static BigDecimal
toValue
(javax.money.MonetaryAmount monetaryAmount) Converts aMonetaryAmount
to aBigDecimal
that represents its underlying value.static BigDecimal
toValueOrNull
(javax.money.MonetaryAmount monetaryAmount) Converts aMonetaryAmount
to aBigDecimal
that represents its underlying value.static javax.money.MonetaryAmount
zero()
Returns aMonetaryAmount
with a defaultCurrencyUnit
and a 0 (zero) value.static javax.money.MonetaryAmount
Returns aMonetaryAmount
with theCurrencyUnit
for the provided currency code and a 0 (zero) value.static javax.money.MonetaryAmount
zero
(javax.money.CurrencyUnit currencyUnit) Returns aMonetaryAmount
with the providedCurrencyUnit
and a 0 (zero) value.
-
Method Details
-
toAmount
Converts the givenBigDecimal
to aMonetaryAmount
with the default system currency- Parameters:
value
- the value to convert- Returns:
- the converted
MonetaryAmount
or null if the value was null - See Also:
-
toAmount
@Nullable public static javax.money.MonetaryAmount toAmount(@Nullable Number value, @Nullable String currencyCode) Converts the givenBigDecimal
to aMonetaryAmount
with the currency for the given currency code.- Parameters:
value
- the value to convertcurrencyCode
- the currency code of the currency to use- Returns:
- the converted
MonetaryAmount
or null if the value was null - See Also:
-
toAmount
@Nullable public static javax.money.MonetaryAmount toAmount(@Nullable Number value, @Nullable CurrencyContext ctx) - Parameters:
value
- the value to convert, can be nullctx
- the currency the result should be in or null to use the default system currency- Returns:
- the converted value in the given currency. Null if value is null.
-
toAmount
@Nullable public static javax.money.MonetaryAmount toAmount(@Nullable Number value, @Nullable javax.money.CurrencyUnit currency) - Parameters:
value
- the value to convert, can be nullcurrency
- the currency the result should be in or null to use the default system currency- Returns:
- the converted value in the given currency. Null if value is null
-
toAmount
@Nullable public static javax.money.MonetaryAmount toAmount(@NonNull String value, javax.money.CurrencyUnit currency) Converts the given passed in String to avalue
and then builds anmoney
using the value and specifiedcurrency
.- Parameters:
value
- the String value to convertcurrency
- the currency the result should be in or null to use the default system currency- Returns:
- the converted value in the given currency. Null if value is null
-
toAmount
@Nullable public static javax.money.MonetaryAmount toAmount(@NonNull String value, String currencyCode) Converts the given passed in String to avalue
and then builds anmoney
using the value and then converts the passed in currencyCode (e.g. "USD") to aCurrencyUnit
to build a MonetaryAmount.- Parameters:
value
- the String value to convertcurrencyCode
- the String currency code to use to build this MonetaryAmount- Returns:
- the converted value in the given currency. Null if value is null
-
toAmount
@Nullable public static <T extends javax.money.MonetaryAmount> javax.money.MonetaryAmount toAmount(@Nullable Number value, @Nullable javax.money.CurrencyUnit currency, @Nullable Class<T> type) Returns an instance ofMonetaryAmount
. If the value argument is null, null will be returned. If the Currency argument is null, the system default will be used. If the type argument is null, the system default will be used.- Type Parameters:
T
- a type ofMonetaryAmount
- Parameters:
value
- the value to convert into a monetary amountcurrency
- the currency to usetype
- the type ofMonetaryAmount
to use- Returns:
- a new monetary amount with the given value, currency, and type
-
toValue
Converts aMonetaryAmount
to aBigDecimal
that represents its underlying value.- Parameters:
monetaryAmount
- the amount to convert into its value- Returns:
- the BigDecimal amount value
-
toValueOrNull
@Nullable public static BigDecimal toValueOrNull(@Nullable javax.money.MonetaryAmount monetaryAmount) Converts aMonetaryAmount
to aBigDecimal
that represents its underlying value. Returns null if the amount is null.- Parameters:
monetaryAmount
- the amount to convert into its value- Returns:
- the BigDecimal amount value
-
zero
public static javax.money.MonetaryAmount zero()Returns aMonetaryAmount
with a defaultCurrencyUnit
and a 0 (zero) value.- Returns:
- zero monetary amount with the default currency
-
zero
public static javax.money.MonetaryAmount zero(@Nullable javax.money.CurrencyUnit currencyUnit) Returns aMonetaryAmount
with the providedCurrencyUnit
and a 0 (zero) value.- Parameters:
currencyUnit
- the currency to use for the zero amount- Returns:
- zero monetary amount for the currency
-
zero
Returns aMonetaryAmount
with theCurrencyUnit
for the provided currency code and a 0 (zero) value.- Parameters:
currencyCode
- the currency code to use for the zero amount- Returns:
- zero monetary amount for the currency
-
getCurrency
Takes a currency code and returns aCurrencyUnit
. If the currencyCode is null, returns the default currency.- Parameters:
currencyCode
- the currency code to get the currency unit for- Returns:
- the currency unit for the currency code, or the default currency
-
getCurrency
Converts aCurrency
object into aCurrencyUnit
. If currency is null, this returns default currency.- Parameters:
currency
- the currency object to convert- Returns:
- the currency unit of the currency, or the default currency
-
defaultCurrency
public static javax.money.CurrencyUnit defaultCurrency()Obtains the default system currency- Returns:
- the system default currency
- See Also:
-
round
public static javax.money.MonetaryAmount round(javax.money.MonetaryAmount amount, int scale, RoundingMode roundingMode) Performs rounding on the amount with a specified scale and roundingMode.- Parameters:
amount
- the amount to roundscale
- the scale to round toroundingMode
- the method used for rounding- Returns:
- the rounded amount
-
round
public static javax.money.MonetaryAmount round(javax.money.MonetaryAmount amount, int scale) Performs rounding to the specified scale andRoundingMode.HALF_EVEN
used for the rounding model.- Parameters:
amount
- the amount to roundscale
- the scale to round to- Returns:
- the rounded amount
-
round
public static javax.money.MonetaryAmount round(javax.money.MonetaryAmount amount) Rounds the passed in amount using the scale of its currency and a default rounding mode.RoundingMode.HALF_EVEN
is considered "Banker's Rounding" and this is what is used.- Parameters:
amount
- the amount to round- Returns:
- the rounded amount
-
round
public static javax.money.MonetaryAmount round(javax.money.MonetaryAmount amount, RoundingMode roundingMode) Rounds the passed in amount using the scale of its currency and the passed in RoundingModeRoundingMode.HALF_EVEN
is considered "Banker's Rounding" and this is what is used.- Parameters:
amount
- the amount to roundroundingMode
- The roundingMode to use for rounding.- Returns:
- the rounded amount
-
isEqualByComparison
public static boolean isEqualByComparison(javax.money.MonetaryAmount amount1, javax.money.MonetaryAmount amount2) Tests whether twoMonetaryAmount
objects are equal by comparison, after rounding is applied based on currency. The currencies must be equal in order for the objects to be equal. The numeric value must also be equal, but rounding will be applied prior to comparison. So, consider that: USD 1.00 == USD 1.00
USD 1.00 == USD 1.009
USD 1.00 != GBP 1.00
- Parameters:
amount1
- the first amount to compareamount2
- the second amount to compare- Returns:
- whether the amounts are equal by value comparision
-
isEqualByComparison
Tests if a MonetaryAmount is equal to the BigDecimal being passed in by looking at each objects doubleValue- Parameters:
amount1
- the first amount to compareamount2
- the second amount to compare- Returns:
- whether the amounts are equal by value comparision
-
isEqualByComparison
Tests if a MonetaryAmount is equal to the String being passed in by first converting the String to a BigDecimal and then calling theisEqualByComparison(MonetaryAmount, Number)
method.- Parameters:
amount1
- A MonetaryAmountamount2
- String representation of a Money (e.g. "1.25")- Returns:
- whether the amounts are equal by value comparision
-
isGreaterThanZero
public static boolean isGreaterThanZero(javax.money.MonetaryAmount amount) Tests whether the providedMonetaryAmount
is greater than zero after rounding based on the currency.- Parameters:
amount
- the amount to compare- Returns:
- whether the amount is greater than zero
-
isLessThanZero
public static boolean isLessThanZero(javax.money.MonetaryAmount amount) Tests whether the providedMonetaryAmount
is less than zero after rounding based on the currency.- Parameters:
amount
- the amount to compare- Returns:
- whether the amount is less than zero
-
isEqualToZero
public static boolean isEqualToZero(javax.money.MonetaryAmount amount) Tests whether the providedMonetaryAmount
is equal to zero after rounding based on the currency.- Parameters:
amount
- the amount to compare- Returns:
- whether the amount is equal to zero
-
isGreaterThan
Tests whether theMonetaryAmount
value is greater than theNumber
provided, rounded according to the currency. So, a call to this method with a USDMonetaryAmount
with an initial value of 1.01 would return true if the provided value is greater than 1.01, rounded according to the currency. In other words, the provided value is scaled to that of the providedMonetaryAmount
and then compared.- Parameters:
amount
- the amount to comparevalue
- the number value to compare- Returns:
- whether the amount is greater than the value
-
isLessThan
Tests whether theMonetaryAmount
value is less than theNumber
provided, rounded according to the currency. So, a call to this method with a USDMonetaryAmount
with an initial value of 1.01 would return true if the provided value is less than 1.01, rounded according to the currency. In other words, the provided value is scaled to that of the providedMonetaryAmount
and then compared for equality.- Parameters:
amount
- the amount to comparevalue
- the number value to compare- Returns:
- whether the amount is less than the value
-
isEqualTo
Tests whether theMonetaryAmount
value is equal to theNumber
provided, rounded according to the currency. So, a call to this method with a USDMonetaryAmount
with an initial value of 1.01 would return true if the provided value is equal to 1.01, rounded according to the currency. In other words, the provided value is scaled to that of the providedMonetaryAmount
and then compared for equality. Consider that: USD 1.00 == 1
USD 1.00 == 1.0
USD 1.00 == 1.00
USD 1.00 == 1.001
USD 1.00 != 1.009
USD 1.00 != 3
- Parameters:
amount
- the amount to comparevalue
- the number value to compare- Returns:
- whether the amount is equal to the value
-
getCurrency
Get theCurrencyUnit
for theLocale
provided- Parameters:
locale
- the locale to determine currency against- Returns:
- the
CurrencyUnit
for theLocale
provided
-