public class Money extends AbstractAmount
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.| Modifier and Type | Class and Description |
|---|---|
static interface |
Money.MoneyExtractor<T> |
| Constructor and Description |
|---|
Money(java.math.BigDecimal amount,
Currency currency)
Creates a Money object.
|
Money(Currency currency)
Creates zero money of the given Currency.
|
Money(long amountInSmallestPieces,
Currency currency)
Creates a new money from a given amount of smallest pieces and a currency.
|
Money(java.lang.String amount,
Currency currency)
Creates a new money from String and currency.
|
| Modifier and Type | Method and Description |
|---|---|
Money |
add(Money money)
|
void |
assertCurreniesAreEqual(Currency curr)
Checks if the current Currency is equal to given one.
|
void |
assertCurreniesAreEqual(Money other)
Checks if the current Currency is equal to given one.
|
protected java.math.BigDecimal |
checkPercentages(java.util.List<Percentage> percentages) |
boolean |
equals(java.lang.Object obj)
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.
|
java.math.BigDecimal |
getAmount()
The monetary amount.
|
Currency |
getCurrency()
Returns the currency of this money.
|
static java.util.List<Percentage> |
getPercentages(int scale,
Money... moneys)
Calculates a list of
Percentage objects reflecting the distribution of money within the given money list. |
static java.util.List<Percentage> |
getPercentages(java.util.List<Money> moneys,
int scale)
Calculates a list of
Percentage objects reflecting the distribution of money within the given money list. |
static java.util.List<Percentage> |
getPercentages(Money... moneys)
Calculates a list of
Percentage objects reflecting the distribution of money within the given money list. |
protected static int[] |
getSortedPositionTable(java.util.List<Money> moneyList) |
int |
hashCode() |
static void |
sortAscending(java.util.List<Money> elements) |
protected static <T> java.util.List<T> |
sortByMoney(java.util.Map<T,Money> moneyMap,
java.util.Comparator<java.util.Map.Entry<?,Money>> comp) |
static <T> java.util.List<T> |
sortByMoneyAscending(java.util.Map<T,Money> moneyMap) |
static <T> java.util.List<T> |
sortByMoneyDescending(java.util.Map<T,Money> moneyMap) |
static void |
sortDescending(java.util.List<Money> elements) |
java.util.List<Money> |
split(java.util.List<Percentage> percentages)
Splits the current Money into a list of new Money objects, based on the given list of
Percentage objects. |
java.util.List<Money> |
split(Percentage... percentages)
Same as
split(List) but because of the variable parameter list the sum must not be exactly 100%. |
Money |
subtract(Money money)
|
static Money |
sum(java.util.Collection<Money> elements) |
static <T> Money |
sum(java.util.Collection<T> elements,
Money.MoneyExtractor<T> extractor) |
static Money |
sum(Money... money) |
protected static long |
sumUnscaled(java.util.Collection<Money> elements) |
protected static <T> Money |
sumUnscaled(java.util.Collection<T> elements,
Money.MoneyExtractor<T> extractor) |
java.lang.String |
toString()
Returns the amount of the value formatted with the
Currency.getDigits() and the currency isocode. |
static java.util.List<Money> |
valueOf(Currency curr,
java.math.BigDecimal... amounts)
Creates a list of money objects for a specific currency and any number of BigDecimal values.
|
static java.util.List<Money> |
valueOf(Currency curr,
long... amounts)
Creates a list of money objects for a specific currency and any number of long values.
|
static java.util.List<Money> |
valueOf(Currency curr,
java.lang.String... amounts)
Creates a list of money objects for a specific currency and any number of String values.
|
static Money |
zero(Currency curr)
Shortcut to obtain commonly used zero money instances.
|
public Money(java.math.BigDecimal amount,
Currency currency)
Currency.getDigits().
amount and this.getAmount() are maybe not equal (because of the different scale
of the BigDecimal) BUT new Money(BigDecimal.ONE, curr) and
new Money(new BigDecimal("1.000"), curr) are equal with each other!amount - the value or amount of the moneycurrency - the amount of the money in the hybris Currency pojojava.lang.ArithmeticException - if the amount must be rounded because the Currency.getDigits() does not provide enough digits.public Money(Currency currency)
currency - public Money(long amountInSmallestPieces,
Currency currency)
public Money(java.lang.String amount,
Currency currency)
public static java.util.List<Money> valueOf(Currency curr, java.lang.String... amounts)
public static java.util.List<Money> valueOf(Currency curr, long... amounts)
public static java.util.List<Money> valueOf(Currency curr, java.math.BigDecimal... amounts)
public static Money zero(Currency curr)
Money(Currency).public Currency getCurrency()
public java.math.BigDecimal getAmount()
public void assertCurreniesAreEqual(Currency curr)
CurrenciesAreNotEqualException - if the currencies are not equal.public void assertCurreniesAreEqual(Money other)
CurrenciesAreNotEqualException - if the currencies are not equal.public Money add(Money money)
Money amount the given Money amount. The result is a new Money object with the
sum of both amounts and in the same currency.money - this will be added to the current money amountCurrenciesAreNotEqualException - if getCurrency() is different in the given objectspublic Money subtract(Money money)
Money amount the given Money amount. The result is a new Money object with
the difference of both amounts and in the same currency.money - this will be subtracted to the current money amountCurrenciesAreNotEqualException - if getCurrency() is different in the given objectspublic int hashCode()
hashCode in class java.lang.Objectpublic boolean equals(java.lang.Object obj)
equals in class java.lang.Objectpublic java.lang.String toString()
Currency.getDigits() and the currency isocode. Example:
"1.20 EUR"toString in class AbstractAmountpublic java.util.List<Money> split(Percentage... percentages)
split(List) but because of the variable parameter list the sum must not be exactly 100%. If less
the missing Percentage value 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 a CalculationException is thrown if the sum is greater
than 100%.percentages - the variable list of percentagesList of Money objects which are summed up the same amount of this current Money object.public java.util.List<Money> split(java.util.List<Percentage> percentages)
Percentage objects.
The sum of the Percentages must be equal to 100% or a CalculationException is 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]percentages - the list of Percentage in which the current money should be split into.List of Money objects which are summed up the same amount of this current Money object.AmountException - if the sum of the percentages is not 100%java.lang.IllegalArgumentException - if the percent list is null or emptyprotected java.math.BigDecimal checkPercentages(java.util.List<Percentage> percentages)
public static <T> java.util.List<T> sortByMoneyAscending(java.util.Map<T,Money> moneyMap)
protected static <T> java.util.List<T> sortByMoney(java.util.Map<T,Money> moneyMap, java.util.Comparator<java.util.Map.Entry<?,Money>> comp)
public static <T> java.util.List<T> sortByMoneyDescending(java.util.Map<T,Money> moneyMap)
public static void sortAscending(java.util.List<Money> elements)
public static void sortDescending(java.util.List<Money> elements)
public static final <T> Money sum(java.util.Collection<T> elements, Money.MoneyExtractor<T> extractor)
protected static final <T> Money sumUnscaled(java.util.Collection<T> elements, Money.MoneyExtractor<T> extractor)
protected static final long sumUnscaled(java.util.Collection<Money> elements)
public static java.util.List<Percentage> getPercentages(Money... moneys)
Percentage objects reflecting the distribution of money within the given money list.
Please note that the scale of these percentages is zero.getPercentages(int, Money...)public static java.util.List<Percentage> getPercentages(int scale, Money... moneys)
Percentage objects reflecting the distribution of money within the given money list.
The specified scale is defining the precision of these percentages.getPercentages(Money...)public static java.util.List<Percentage> getPercentages(java.util.List<Money> moneys, int scale)
Percentage objects reflecting the distribution of money within the given money list.
The specified scale is defining the precision of these percentages.getPercentages(Money...)protected static int[] getSortedPositionTable(java.util.List<Money> moneyList)
Copyright © 2018 SAP SE. All Rights Reserved.