Judging from the RI the contract of MonetaryAmount#compareTo seems to be to first compare by currency code (not by currency, even though CurrencyUnit is Comparable) before comparing by numeric value. However all the convenience comparison methods #isGreaterThan, #isGreaterThanOrEqualTo, #isLessThan, #isLessThanOrEqualTo, #isEqualTo are specified to throw MonetaryException if the currency code (not the currency) is different.
This leads to the following behavior:
FastMoney money = FastMoney.of(BigDecimal.valueOf(2L), CHF);
assertTrue(money.compareTo(FastMoney.of(BigDecimal.valueOf(1L), EUR)) < 0); // passes
assertTrue(money.isLessThan(FastMoney.of(BigDecimal.valueOf(1L), EUR))); // throws MonetaryException
This may be an issue with the RI and not the API, as the API gives no guidance on how #compareTo should be implemented. However the API requires that #equals considers the currency unit and that best practices are that a.equals(b) implies a.compareTo(b) == 0 .
Judging from the RI the contract of
MonetaryAmount#compareToseems to be to first compare by currency code (not by currency, even thoughCurrencyUnitisComparable) before comparing by numeric value. However all the convenience comparison methods#isGreaterThan,#isGreaterThanOrEqualTo,#isLessThan,#isLessThanOrEqualTo,#isEqualToare specified to throwMonetaryExceptionif the currency code (not the currency) is different.This leads to the following behavior:
This may be an issue with the RI and not the API, as the API gives no guidance on how
#compareToshould be implemented. However the API requires that#equalsconsiders the currency unit and that best practices are thata.equals(b)impliesa.compareTo(b) == 0.