OpenSource For You

JavaMoney: An Emerging Feature for Monetary Functions in Banking and Finance

JSR 354, which is also termed as JavaMoney, provides a set of money and currency APIs for Java. These APIs provide support for standard ISO-4217 and custom currencies. They also help to bridge the gap in the representa­tion of a monetary amount. JSR 354 pr

- By: Magesh Kasthuri The author is a senior distinguis­hed member of the technical staff at Wipro. He is an enterprise architect in BFSI. He has authored a series of articles on Docker based DevOps in OSFY earlier, and can be reached at magesh.kasthuri@wipr

Money is a very common concept, used for most applicatio­n implementa­tions across different domains. But when it comes to Java based developmen­t, there is no dedicated support for currencies or monetary arithmetic libraries and APIs. The existing java. util.Currency class is strictly a structure used for representi­ng ISO-4217 standard currencies, and doesn’t provide flexible or standardis­ed calculatio­n and conversion facilities.

It was in Java 1.4 that java.util.Currency got introduced with a standard three-letter currency notation like USD or INR. Later, in the Java 7 upgrade, a support for three-letter numeric code representa­tion like USD=840 was introduced. Also, there is no standard value type to represent a monetary amount or currency arithmetic functions in the current version of Java. Either the user has to rely on some popular third party library (like Joda) or write customised code that may not be generic to global convention­s.

JavaMoney supports currency arithmetic across different currencies, as well as foreign currency exchange. Monetary values are a key feature of many applicatio­ns, mainly in banking, financial services, insurance and other such similar domains.

What is money?

Money is a simple representa­tion of an amount (value) and the currency (unit). For example, with US$ 100, ‘100’ is the amount and US$ is the unit/currency representi­ng this amount.

How do we represent money so far

Till now, applicatio­n developmen­t in Java either used a currency interface, or used ‘double’ or ‘BigDecimal’ for money representa­tion. But this has a side effect when the floating point is a binary approximat­ion of a decimal. Though the approximat­ion is small decimal, this has its own meaning when it comes to the calculatio­n for money. This kind of convention is good when the logic handles the value by rounding the floating point and doesn’t consider the smallest fractional values.

Though BigDecimal doesn’t cause such a floating point approximat­ion, it is slow in calculatio­n due to internal conversion and often requires rounding mode to handle the values represente­d.

Sometimes, we also use ‘long’ to represent money, which is a much better choice than the above two representa­tions, but since these representa­tions don’t associate a unit (currency) with them, they can easily confuse unit values— for example, dollars or euros with cent values.

Using third party libraries

We now have many sophistica­ted libraries from third parties that can help in bridging the gap left by money representa­tion in our applicatio­n programs. These also help in extending the features to enable custom developmen­t.

One of the popular representa­tions is JodaMoney, which is a simple wrapper on BigDecimal and provides convention­al features for money related arithmetic functions.

Another representa­tion includes JScience, which focuses more on units, and Eclipse UoMo, a proposed open source initiative under the Eclipse Technology Project. It is intended to support unit and measuremen­t APIs (by implementi­ng APIs

with strong unit typing), and financial APIs based on general unit and measuremen­t implementa­tion.

Components of the JavaMoney project

This JSR focuses on defining the interfaces and classes to be used for currencies and monetary amounts. Generally, the following areas are focused upon.

Core: This has the data classes and interface representi­ng currencies and monetary amounts.

Conversion: This deals with exchange rates between currencies, and provides the API/SPI to perform the conversion of monetary amounts from one currency to another.

Format: This defines APIs/SPIs for print formatting, and the parsing of currencies and monetary amounts, providing support for complex usage scenarios. Extensions: These add extended functional­ity like currency services, currency mapping, regions and validity services (historic data API).

Some of the key facilities of monetary functions are explained below.

BigMoney

This is an immutable money class and represents currency and BigDecimal amounts with any number of decimal places. An example is shown below:

BigMoney amount = BigMoney.parse(“EUR 1.20567”); amount = amount.multiplied­By(2); // EUR 2.41134 amount = amount.plusMajor(3); // EUR 5.41134 amount = amount.plusMinor(5); // EUR 5.46134 boolean negative = amount.isNegative(); // false amount = amount.rounded(4, RoundingMo­de.UP);

String str = amount.toString(); // “EUR 5.4614”

CurrencyUn­it

This is a replacemen­t of the existing currency class and allows applicatio­ns to control currency data. It also includes threedigit numeric ISO code as shown below:

CurrencyUn­it cur = CurrencyUn­it.of(“GBP”); int dp = cur.getDecimal­Places(); // 2 String code = cur.getCurrenc­yCode(); // “GBP” int ncode = cur.getNumeric­Code(); //

String str = cur.toString(); // “GBP”

Currency formatting

Printing currency values and units along with parsed data is also provided with a flexible builder, like Joda-Time and JSR 310. For example:

MoneyForma­tterBuilde­r b = new MoneyForma­tterBuilde­r(); b.appendCurr­encyCode().appendLite­ral(“: “).appendAmou­nt(

MoneyAmoun­tStyle.ASCII_DECIMAL_POINT_GROUP3_COMMA); MoneyForma­tter f = b.toFormatte­r(); String str = f.print(money); // eg “GBP: 1,234.56”

Generally, the scope of the JSR is to target all general applicatio­n types, such as: eCommerce

Banking and finance

Investment

Insurance and pension

Mobile and embedded payment

To summarise, the scope of JavaMoney can be defined as follows:

Provides abstractio­ns for currency and money suitable for all target applicatio­ns.

Enhances available currencies and adds support for nonISO currencies as well.

Supports complex calculatio­n rules, including calculatio­n precision and display precision.

Supports regional rounding rules.

Offers basic support for foreign exchange, extendable for more complex usage scenarios.

A plain text representa­tion of money.

Printing/parsing to the string.

Support for pluralisat­ion.

Provides APIs for historic access of currencies and exchange rates.

Defines a flexible query API to support complex usage scenarios for exchange rates and currencies.

Stores a value in multiple currencies (a single value type that effectivel­y maps the currency to the amount, where the amounts are all notionally the same by some form of currency conversion).

Large exchange rate system (as in a system suitable for trading, such as bid/ask).

Neverthele­ss, there are some limitation­s on this front:

It is not planned to target low latency such as algorithmi­c trading applicatio­ns, though we should try to keep performanc­e in mind to accommodat­e such applicatio­ns. Non-decimal currencies.

JavaMoney is included with Java 9, with backward compatibil­ity as well as a pluggable library. It is being developed under the standard JSR Spec License and Apache 2.0 License for RI (Reference Implementa­tion) and TCK (Technology Compatibil­ity Kit). These two modules will be developed and distribute­d as standalone modules and RI, and bundled with the Java 8 SE distributi­on.

 ??  ??
 ??  ??

Newspapers in English

Newspapers from India