Class Money
java.lang.Object
de.hybris.order.calculation.money.AbstractAmount
de.hybris.order.calculation.money.Money
Represents a monetary amount by combining a
This class provides common operations like
BigDecimal and a Currency. Please know that the scale of
the amount is always equal to the amount of currency digits.Therefore the following rules apply:
| used values | result | |
|---|---|---|
| BigDecimal | Currency.getDigits() | Money.getAmount() as BigDecimal |
| new BigDecimal(1.000) | 0 | 1 |
| new BigDecimal(1.000) | 3 | 1.000 |
| BigDecimal.ZERO | 2 | 0.00 |
| BigDecimal.valueOf(-1.34) | 3 | -1.340 |
| BigDecimal.valueOf(1.234) | 1 | 1.2 since we simply cut off the extra digits |
add(Money), subtract(Money) and split(List).
A Money in the smallest amount (example 0.01 EUR) can not be split in smaller amounts. Splitting this amount
in two will return {0.01 EUR, 0.00 EUR}.
Last but not least there are a number of tool methods to ease working with this class: zero(Currency),
valueOf(Currency, String...), sum(Collection), sortAscending(List) and
getPercentages(Money...) to name a few.-
Nested Class Summary
Nested Classes -
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionCreates a new money from a given amount of smallest pieces and a currency.Creates zero money of the given Currency.Creates a new money from String and currency.Money(BigDecimal amount, Currency currency) Creates a Money object. -
Method Summary
Modifier and TypeMethodDescriptionvoidChecks if the current Currency is equal to given one.voidassertCurreniesAreEqual(Money other) Checks if the current Currency is equal to given one.protected BigDecimalcheckPercentages(List<Percentage> percentages) booleanReturns true if the given parameter is also an instanceof money and the current currency is equal to the currency of the given money and the current amount is comparedTo == 0 to the given money amount.The monetary amount.Returns the currency of this money.static List<Percentage>getPercentages(int scale, Money... moneys) Calculates a list ofPercentageobjects reflecting the distribution of money within the given money list.static List<Percentage>getPercentages(Money... moneys) Calculates a list ofPercentageobjects reflecting the distribution of money within the given money list.static List<Percentage>getPercentages(List<Money> moneys, int scale) Calculates a list ofPercentageobjects reflecting the distribution of money within the given money list.protected static int[]getSortedPositionTable(List<Money> moneyList) inthashCode()static voidsortAscending(List<Money> elements) protected static <T> List<T>sortByMoney(Map<T, Money> moneyMap, Comparator<Map.Entry<?, Money>> comp) static <T> List<T>sortByMoneyAscending(Map<T, Money> moneyMap) static <T> List<T>sortByMoneyDescending(Map<T, Money> moneyMap) static voidsortDescending(List<Money> elements) split(Percentage... percentages) Same assplit(List)but because of the variable parameter list the sum must not be exactly 100%.split(List<Percentage> percentages) Splits the current Money into a list of new Money objects, based on the given list ofPercentageobjects.static final Moneystatic final Moneysum(Collection<Money> elements) static final <T> Moneysum(Collection<T> elements, Money.MoneyExtractor<T> extractor) protected static final longsumUnscaled(Collection<Money> elements) protected static final <T> MoneysumUnscaled(Collection<T> elements, Money.MoneyExtractor<T> extractor) toString()Returns the amount of the value formatted with theCurrency.getDigits()and the currency isocode.Creates a list of money objects for a specific currency and any number of long values.Creates a list of money objects for a specific currency and any number of String values.valueOf(Currency curr, BigDecimal... amounts) Creates a list of money objects for a specific currency and any number of BigDecimal values.static MoneyShortcut to obtain commonly used zero money instances.
-
Field Details
-
CURRENCY_CANNOT_BE_NULL
- See Also:
-
CANNOT_SUM_NOTHING
- See Also:
-
-
Constructor Details
-
Money
Creates a Money object. A new BigDecimal object is used internally, based onCurrency.getDigits().amountandthis.getAmount()are maybe not equal (because of the different scale of the BigDecimal) BUTnew Money(BigDecimal.ONE, curr)andnew Money(new BigDecimal("1.000"), curr)are equal with each other!- Parameters:
amount- the value or amount of the moneycurrency- the amount of the money in the hybrisCurrencypojo- Throws:
ArithmeticException- if the amount must be rounded because theCurrency.getDigits()does not provide enough digits.
-
Money
Creates zero money of the given Currency.- Parameters:
currency-
-
Money
Creates a new money from a given amount of smallest pieces and a currency. -
Money
Creates a new money from String and currency.
-
-
Method Details
-
valueOf
Creates a list of money objects for a specific currency and any number of String values. -
valueOf
Creates a list of money objects for a specific currency and any number of long values. -
valueOf
Creates a list of money objects for a specific currency and any number of BigDecimal values. -
zero
Shortcut to obtain commonly used zero money instances. Since they are cached this methods should be preferred toMoney(Currency). -
getCurrency
Returns the currency of this money. -
getAmount
The monetary amount. The amount scale is equal to the currency digits! -
assertCurreniesAreEqual
Checks if the current Currency is equal to given one.- Throws:
CurrenciesAreNotEqualException- if the currencies are not equal.
-
assertCurreniesAreEqual
Checks if the current Currency is equal to given one.- Throws:
CurrenciesAreNotEqualException- if the currencies are not equal.
-
add
Add to the currentMoneyamount the givenMoneyamount. The result is a new Money object with the sum of both amounts and in the same currency.- Parameters:
money- this will be added to the current money amount- Returns:
- a new object with the sum
- Throws:
CurrenciesAreNotEqualException- ifgetCurrency()is different in the given objects
-
subtract
Subtracts from currentMoneyamount the givenMoneyamount. The result is a new Money object with the difference of both amounts and in the same currency.- Parameters:
money- this will be subtracted to the current money amount- Returns:
- a new object with the difference
- Throws:
CurrenciesAreNotEqualException- ifgetCurrency()is different in the given objects
-
hashCode
public int hashCode() -
equals
Returns true if the given parameter is also an instanceof money and the current currency is equal to the currency of the given money and the current amount is comparedTo == 0 to the given money amount. (this is not BigDecimal.equals(BigDecimal), the check is BigDecimal.compareTo(BigDecimal)) -
toString
Returns the amount of the value formatted with theCurrency.getDigits()and the currency isocode. Example: "1.20 EUR"- Overrides:
toStringin classAbstractAmount- Returns:
- {0.xx} {currency.toString};
xx.lenght == currency.digits
-
split
Same assplit(List)but because of the variable parameter list the sum must not be exactly 100%. If less the missingPercentagevalue will be added. This means, the size of the resulting list is equal to the given percentage list if the sum of them is exactly 100% or the size of the resulting list is one more if the given percentage list is less than 100%. Anyway aCalculationExceptionis thrown if the sum is greater than 100%. -
split
Splits the current Money into a list of new Money objects, based on the given list ofPercentageobjects. The sum of the Percentages must be equal to 100% or aCalculationExceptionis thrown. The size of the returned money list is equal to the size of the given percentage list. The sum of the returned money list is equal to the given amount of money. A money can only be split into the smallest possible amount of this money which depends on the currency. If a small amount of money can not be split anymore this amount is added to the last entry in the returned list. Example: splitting 0.01 EURO into half results in [0.00EUR , 0.01EUR]- Parameters:
percentages- the list ofPercentagein which the current money should be split into.- Returns:
- a
ListofMoneyobjects which are summed up the same amount of this current Money object. - Throws:
AmountException- if the sum of the percentages is not 100%IllegalArgumentException- if the percent list is null or empty
-
checkPercentages
-
sortByMoneyAscending
-
sortByMoney
-
sortByMoneyDescending
-
sortAscending
-
sortDescending
-
sum
-
sum
-
sum
-
sumUnscaled
protected static final <T> Money sumUnscaled(Collection<T> elements, Money.MoneyExtractor<T> extractor) -
sumUnscaled
-
getPercentages
Calculates a list ofPercentageobjects reflecting the distribution of money within the given money list. Please note that the scale of these percentages is zero.- See Also:
-
getPercentages
Calculates a list ofPercentageobjects reflecting the distribution of money within the given money list. The specified scale is defining the precision of these percentages.- See Also:
-
getPercentages
Calculates a list ofPercentageobjects reflecting the distribution of money within the given money list. The specified scale is defining the precision of these percentages.- See Also:
-
getSortedPositionTable
-