The reference implementation of the JSR 354, money-api, release a new version. Moneta, the JSR 354 reference implementation, and Moneta-bp, its backport compatible with Java prior to SE8, run very stable since quite some time. Nevertheless we fixed a few issues.
New factory methods
We also added a few construct methods to all MonetaryAmount T implementations:
- T zero(CurrencyUnit currency) – Obtains an instance representing zero.
- T ofMinor(CurrencyUnit currency, long amountMinor) – Obtains an instance from an amount in minor units, in other words, the result of ofMinor(USD, 1234) will be USD 12,34.
- T ofMinor(CurrencyUnit currency, long amountMinor, int factionDigits) - Obtains an instance from an amount in minor units, in other words, the result of ofMinor(USD, 1234, 2) will be USD 12,34. The difference between with the last method is this method ignore the fraction digits from the CurrencyUnit instance and it uses the fraction digits informed from parameters.
The following snippets shows a few example what you can do now:
MonetaryAmount money = Money.zero(dollar); MonetaryAmount oneDollar = Money.ofMinor(dollar, 12_34); MonetaryAmount money = FastMoney.zero(dollar); MonetaryAmount oneDollar = FastMoney.ofMinor(dollar, 12_34); MonetaryAmount money = RoundedMoney.zero(dollar); MonetaryAmount oneDollar = RoundedMoney.ofMinor(dollar, 12_34);
We also deprecated the MonetaryUtil class in favour of two new classes:
- MonetaryOperator which provides useful implementations for commonly used operators
MonetaryAmount amount = ////USD 12.23 amount.with(MonetaryOperators.majorPart());//USD 12 amount.with(MonetaryOperators.minorPart());//USD 0.23 amount.with(MonetaryOperators.percent(10));//USD 1.223
- MonetaryQueries provides useful implementations of queries to extract information from a monetary amount.
MonetaryAmount amount = //USD 12.34 12.32 amount.query(MonetaryQueries.convertMinorPart());//1232 amount.query(MonetaryQueries.extractMajorPart());//12 amount.query(MonetaryQueries.extractMinorPart());//32
This release also contains a new exchange rate provider version to figure out historic exchange rates from the IMF.
CurrencyUnit dollar = Monetary.getCurrency("USD"); CurrencyUnit real = Monetary.getCurrency("BRL"); MonetaryAmount money = FastMoney.of(10, dollar); MonetaryAmount money2 = FastMoney.of(10, real); LocalDate localDate = Year.of(2009).atMonth(Month.JANUARY).atDay(9); ExchangeRateProvider provider = MonetaryConversions.getExchangeRateProvider(ExchangeRateType.IMF_HIST); ConversionQuery query = ConversionQueryBuilder.of().setTermCurrency(dollar).set(localDate).build(); CurrencyConversion currencyConversion = provider.getCurrencyConversion(query); MonetaryAmount result = currencyConversion.apply(money2); MonetaryAmount monetaryAmount = money.add(result);
Changes in MonetaryAmountFormat
We also added new ways to create MonetaryAmountFormat instances so the can be exported with toString() to and be instantiated with this output in a round trip:
MonetaryAmountFormat defaultFormat = MonetaryAmountDecimalFormatBuilder.newInstance().build(); MonetaryAmountFormat patternFormat = MonetaryAmountDecimalFormatBuilder.of("¤ ###,###.00").build(); MonetaryAmountFormat localeFormat = MonetaryAmountDecimalFormatBuilder.of(Locale.US).build(); CurrencyUnit currency = Monetary.getCurrency("BRL"); MonetaryAmount money = Money.of(12, currency); String format = defaultFormat.format(money);//$12.00 MonetaryAmount moneyParsed = Money.parse(format, defaultFormat);//or using defafult.parse(format);
This is a maintenance release. We checked backward compatibility as part of the CI integrated tooling, so there should be no issues regarding compatibility. The code provided is usage, compile and binary compatible. Explicitly excluded is the usage of code artifacts contained in packages named internal. If you have such usages (we hope: no), please review and test your code before deploying it into production.
The new version 1.1 of the JSR 354 reference implementation fixes several bugs (most of them minor ones) and comes with a few new features improving developer’s efficiency.