Class MonetaryUtils

java.lang.Object
com.broadleafcommerce.money.util.MonetaryUtils

public final class MonetaryUtils extends Object
Convenience methods for dealing with the Monetary API
Author:
Phillip Verheyden (phillipuniverse)
  • Method Summary

    Modifier and Type
    Method
    Description
    static javax.money.CurrencyUnit
    Obtains the default system currency
    static javax.money.CurrencyUnit
    getCurrency(String currencyCode)
    Takes a currency code and returns a CurrencyUnit.
    static javax.money.CurrencyUnit
    Converts a Currency object into a CurrencyUnit.
    static javax.money.CurrencyUnit
    Get the CurrencyUnit for the Locale provided
    static 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 doubleValue
    static 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 the isEqualByComparison(MonetaryAmount, Number) method.
    static boolean
    isEqualByComparison(javax.money.MonetaryAmount amount1, javax.money.MonetaryAmount amount2)
    Tests whether two MonetaryAmount objects are equal by comparison, after rounding is applied based on currency.
    static boolean
    isEqualTo(javax.money.MonetaryAmount amount, Number value)
    Tests whether the MonetaryAmount value is equal to the Number provided, rounded according to the currency.
    static boolean
    isEqualToZero(javax.money.MonetaryAmount amount)
    Tests whether the provided MonetaryAmount is equal to zero after rounding based on the currency.
    static boolean
    isGreaterThan(javax.money.MonetaryAmount amount, Number value)
    Tests whether the MonetaryAmount value is greater than the Number provided, rounded according to the currency.
    static boolean
    isGreaterThanZero(javax.money.MonetaryAmount amount)
    Tests whether the provided MonetaryAmount is greater than zero after rounding based on the currency.
    static boolean
    isLessThan(javax.money.MonetaryAmount amount, Number value)
    Tests whether the MonetaryAmount value is less than the Number provided, rounded according to the currency.
    static boolean
    isLessThanZero(javax.money.MonetaryAmount amount)
    Tests whether the provided MonetaryAmount 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 and RoundingMode.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 RoundingMode RoundingMode.HALF_EVEN is considered "Banker's Rounding" and this is what is used.
    static javax.money.MonetaryAmount
    Converts the given BigDecimal to a MonetaryAmount with the default system currency
    static javax.money.MonetaryAmount
    Converts the given value to a money in the specified currency.
    static javax.money.MonetaryAmount
    toAmount(Number value, String currencyCode)
    Converts the given BigDecimal to a MonetaryAmount with the currency for the given currency code.
    static javax.money.MonetaryAmount
    toAmount(Number value, javax.money.CurrencyUnit currency)
    Converts the given value to a money in the specified currency.
    static <T extends javax.money.MonetaryAmount>
    javax.money.MonetaryAmount
    toAmount(Number value, javax.money.CurrencyUnit currency, Class<T> type)
    Returns an instance of MonetaryAmount.
    static javax.money.MonetaryAmount
    toAmount(String value, String currencyCode)
    Converts the given passed in String to a value and then builds an money using the value and then converts the passed in currencyCode (e.g.
    static javax.money.MonetaryAmount
    toAmount(String value, javax.money.CurrencyUnit currency)
    Converts the given passed in String to a value and then builds an money using the value and specified currency.
    static BigDecimal
    toValue(javax.money.MonetaryAmount monetaryAmount)
    Converts a MonetaryAmount to a BigDecimal that represents its underlying value.
    static BigDecimal
    toValueOrNull(javax.money.MonetaryAmount monetaryAmount)
    Converts a MonetaryAmount to a BigDecimal that represents its underlying value.
    static javax.money.MonetaryAmount
    Returns a MonetaryAmount with a default CurrencyUnit and a 0 (zero) value.
    static javax.money.MonetaryAmount
    zero(String currencyCode)
    Returns a MonetaryAmount with the CurrencyUnit for the provided currency code and a 0 (zero) value.
    static javax.money.MonetaryAmount
    zero(javax.money.CurrencyUnit currencyUnit)
    Returns a MonetaryAmount with the provided CurrencyUnit and a 0 (zero) value.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • toAmount

      @Nullable public static javax.money.MonetaryAmount toAmount(@Nullable Number value)
      Converts the given BigDecimal to a MonetaryAmount 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 given BigDecimal to a MonetaryAmount with the currency for the given currency code.
      Parameters:
      value - the value to convert
      currencyCode - 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)
      Converts the given value to a money in the specified currency.
      Parameters:
      value - the value to convert, can be null
      ctx - 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)
      Converts the given value to a money in the specified currency.
      Parameters:
      value - the value to convert, can be null
      currency - 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 a value and then builds an money using the value and specified currency.
      Parameters:
      value - the String value to convert
      currency - 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 a value and then builds an money using the value and then converts the passed in currencyCode (e.g. "USD") to a CurrencyUnit to build a MonetaryAmount.
      Parameters:
      value - the String value to convert
      currencyCode - 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 of MonetaryAmount. 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 of MonetaryAmount
      Parameters:
      value - the value to convert into a monetary amount
      currency - the currency to use
      type - the type of MonetaryAmount to use
      Returns:
      a new monetary amount with the given value, currency, and type
    • toValue

      public static BigDecimal toValue(javax.money.MonetaryAmount monetaryAmount)
      Converts a MonetaryAmount to a BigDecimal 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 a MonetaryAmount to a BigDecimal 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 a MonetaryAmount with a default CurrencyUnit 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 a MonetaryAmount with the provided CurrencyUnit and a 0 (zero) value.
      Parameters:
      currencyUnit - the currency to use for the zero amount
      Returns:
      zero monetary amount for the currency
    • zero

      public static javax.money.MonetaryAmount zero(@Nullable String currencyCode)
      Returns a MonetaryAmount with the CurrencyUnit 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

      public static javax.money.CurrencyUnit getCurrency(@Nullable String currencyCode)
      Takes a currency code and returns a CurrencyUnit. 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

      public static javax.money.CurrencyUnit getCurrency(@Nullable Currency currency)
      Converts a Currency object into a CurrencyUnit. 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 round
      scale - the scale to round to
      roundingMode - 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 and RoundingMode.HALF_EVEN used for the rounding model.
      Parameters:
      amount - the amount to round
      scale - 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 RoundingMode RoundingMode.HALF_EVEN is considered "Banker's Rounding" and this is what is used.
      Parameters:
      amount - the amount to round
      roundingMode - 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 two MonetaryAmount 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 compare
      amount2 - the second amount to compare
      Returns:
      whether the amounts are equal by value comparision
    • isEqualByComparison

      public static 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 doubleValue
      Parameters:
      amount1 - the first amount to compare
      amount2 - the second amount to compare
      Returns:
      whether the amounts are equal by value comparision
    • isEqualByComparison

      public static 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 the isEqualByComparison(MonetaryAmount, Number) method.
      Parameters:
      amount1 - A MonetaryAmount
      amount2 - 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 provided MonetaryAmount 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 provided MonetaryAmount 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 provided MonetaryAmount 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

      public static boolean isGreaterThan(javax.money.MonetaryAmount amount, Number value)
      Tests whether the MonetaryAmount value is greater than the Number provided, rounded according to the currency. So, a call to this method with a USD MonetaryAmount 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 provided MonetaryAmount and then compared.
      Parameters:
      amount - the amount to compare
      value - the number value to compare
      Returns:
      whether the amount is greater than the value
    • isLessThan

      public static boolean isLessThan(javax.money.MonetaryAmount amount, Number value)
      Tests whether the MonetaryAmount value is less than the Number provided, rounded according to the currency. So, a call to this method with a USD MonetaryAmount 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 provided MonetaryAmount and then compared for equality.
      Parameters:
      amount - the amount to compare
      value - the number value to compare
      Returns:
      whether the amount is less than the value
    • isEqualTo

      public static boolean isEqualTo(javax.money.MonetaryAmount amount, Number value)
      Tests whether the MonetaryAmount value is equal to the Number provided, rounded according to the currency. So, a call to this method with a USD MonetaryAmount 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 provided MonetaryAmount 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 compare
      value - the number value to compare
      Returns:
      whether the amount is equal to the value
    • getCurrency

      public static javax.money.CurrencyUnit getCurrency(Locale locale)
      Get the CurrencyUnit for the Locale provided
      Parameters:
      locale - the locale to determine currency against
      Returns:
      the CurrencyUnit for the Locale provided