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.CurrencyUnitObtains the default system currencystatic javax.money.CurrencyUnitgetCurrency(String currencyCode) Takes a currency code and returns aCurrencyUnit.static javax.money.CurrencyUnitgetCurrency(Currency currency) Converts aCurrencyobject into aCurrencyUnit.static javax.money.CurrencyUnitgetCurrency(Locale locale) Get theCurrencyUnitfor theLocaleprovidedstatic booleanisEqualByComparison(javax.money.MonetaryAmount amount1, Number amount2) Tests if a MonetaryAmount is equal to the BigDecimal being passed in by looking at each objects doubleValuestatic booleanisEqualByComparison(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 booleanisEqualByComparison(javax.money.MonetaryAmount amount1, javax.money.MonetaryAmount amount2) Tests whether twoMonetaryAmountobjects are equal by comparison, after rounding is applied based on currency.static booleanTests whether theMonetaryAmountvalue is equal to theNumberprovided, rounded according to the currency.static booleanisEqualToZero(javax.money.MonetaryAmount amount) Tests whether the providedMonetaryAmountis equal to zero after rounding based on the currency.static booleanisGreaterThan(javax.money.MonetaryAmount amount, Number value) Tests whether theMonetaryAmountvalue is greater than theNumberprovided, rounded according to the currency.static booleanisGreaterThanZero(javax.money.MonetaryAmount amount) Tests whether the providedMonetaryAmountis greater than zero after rounding based on the currency.static booleanisLessThan(javax.money.MonetaryAmount amount, Number value) Tests whether theMonetaryAmountvalue is less than theNumberprovided, rounded according to the currency.static booleanisLessThanZero(javax.money.MonetaryAmount amount) Tests whether the providedMonetaryAmountis less than zero after rounding based on the currency.static javax.money.MonetaryAmountround(javax.money.MonetaryAmount amount) Rounds the passed in amount using the scale of its currency and a default rounding mode.static javax.money.MonetaryAmountround(javax.money.MonetaryAmount amount, int scale) Performs rounding to the specified scale andRoundingMode.HALF_EVENused for the rounding model.static javax.money.MonetaryAmountround(javax.money.MonetaryAmount amount, int scale, RoundingMode roundingMode) Performs rounding on the amount with a specified scale and roundingMode.static javax.money.MonetaryAmountround(javax.money.MonetaryAmount amount, RoundingMode roundingMode) Rounds the passed in amount using the scale of its currency and the passed in RoundingModeRoundingMode.HALF_EVENis considered "Banker's Rounding" and this is what is used.static javax.money.MonetaryAmountConverts the givenBigDecimalto aMonetaryAmountwith the default system currencystatic javax.money.MonetaryAmounttoAmount(Number value, CurrencyContext ctx) static javax.money.MonetaryAmountConverts the givenBigDecimalto aMonetaryAmountwith the currency for the given currency code.static javax.money.MonetaryAmountstatic <T extends javax.money.MonetaryAmount>
javax.money.MonetaryAmountReturns an instance ofMonetaryAmount.static javax.money.MonetaryAmountConverts the given passed in String to avalueand then builds anmoneyusing the value and then converts the passed in currencyCode (e.g.static javax.money.MonetaryAmountConverts the given passed in String to avalueand then builds anmoneyusing the value and specifiedcurrency.static BigDecimaltoValue(javax.money.MonetaryAmount monetaryAmount) Converts aMonetaryAmountto aBigDecimalthat represents its underlying value.static BigDecimaltoValueOrNull(javax.money.MonetaryAmount monetaryAmount) Converts aMonetaryAmountto aBigDecimalthat represents its underlying value.static javax.money.MonetaryAmountzero()Returns aMonetaryAmountwith a defaultCurrencyUnitand a 0 (zero) value.static javax.money.MonetaryAmountReturns aMonetaryAmountwith theCurrencyUnitfor the provided currency code and a 0 (zero) value.static javax.money.MonetaryAmountzero(javax.money.CurrencyUnit currencyUnit) Returns aMonetaryAmountwith the providedCurrencyUnitand a 0 (zero) value.
-
Method Details
-
toAmount
Converts the givenBigDecimalto aMonetaryAmountwith the default system currency- Parameters:
value- the value to convert- Returns:
- the converted
MonetaryAmountor null if the value was null - See Also:
-
toAmount
@Nullable public static javax.money.MonetaryAmount toAmount(@Nullable Number value, @Nullable String currencyCode) Converts the givenBigDecimalto aMonetaryAmountwith the currency for the given currency code.- Parameters:
value- the value to convertcurrencyCode- the currency code of the currency to use- Returns:
- the converted
MonetaryAmountor 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 avalueand then builds anmoneyusing 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 avalueand then builds anmoneyusing the value and then converts the passed in currencyCode (e.g. "USD") to aCurrencyUnitto 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 ofMonetaryAmountto use- Returns:
- a new monetary amount with the given value, currency, and type
-
toValue
Converts aMonetaryAmountto aBigDecimalthat 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 aMonetaryAmountto aBigDecimalthat 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 aMonetaryAmountwith a defaultCurrencyUnitand 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 aMonetaryAmountwith the providedCurrencyUnitand a 0 (zero) value.- Parameters:
currencyUnit- the currency to use for the zero amount- Returns:
- zero monetary amount for the currency
-
zero
Returns aMonetaryAmountwith theCurrencyUnitfor 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 aCurrencyobject 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_EVENused 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_EVENis 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_EVENis 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 twoMonetaryAmountobjects 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 providedMonetaryAmountis 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 providedMonetaryAmountis 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 providedMonetaryAmountis 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 theMonetaryAmountvalue is greater than theNumberprovided, rounded according to the currency. So, a call to this method with a USDMonetaryAmountwith 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 providedMonetaryAmountand 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 theMonetaryAmountvalue is less than theNumberprovided, rounded according to the currency. So, a call to this method with a USDMonetaryAmountwith 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 providedMonetaryAmountand 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 theMonetaryAmountvalue is equal to theNumberprovided, rounded according to the currency. So, a call to this method with a USDMonetaryAmountwith 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 providedMonetaryAmountand 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 theCurrencyUnitfor theLocaleprovided- Parameters:
locale- the locale to determine currency against- Returns:
- the
CurrencyUnitfor theLocaleprovided
-