JSR 354: Money and Currency API - Short Overview

Post on 16-Jan-2017

197 views 6 download

Transcript of JSR 354: Money and Currency API - Short Overview

JSR 354: Money and Currency APIWerner Keil | Otávio Santana@wernerkeil | @otaviojava | @jsr354https://www.jcp.org/en/jsr/detail?id=354http://javamoney.org€

History and Motivation

Earlier ApproachesMartin Fowler: A large proportion of the computers in this world manipulate money, so it’s always puzzled me that money isn’t actually a first class data type in any mainstream programming language. The lack of a type causes problems, the most obvious surrounding currencies…see http://martinfowler.com/eaaCatalog/money.html

Eric Evans – Time and Money: On project after project, software developers have to reinvent the wheel, creating objects for simple recurring concepts such as “money” and “currency”. Although most languages have a “date” or “time” object, these are rudimentary, and do not cover many needs, such as recurring sequences of time, durations of time, or intervals of time. …To be quite frank, their code isn’t more than an academic POC, factories called dollars() or euros() are useless in real globally deployed frameworks, but he made a good point.

Motivation> Monetary values are a key feature to many applications> Existing java.util.Currency class is strictly a structure used for representing

ISO-4217 standard currencies.> No standard value type to represent a monetary amount> No support for currency arithmetic or conversion> JDK Formatting features lack flexibility

Overview of JSR 354> Core API: javax.money

CurrencyUnit, MonetaryAmount> Conversion API: javax.money.convert

ExchangeRate, ConversionContext> Formatting: javax.money.format

AmountFormatContext, MonetaryAmountFormat

> Reference Implementation: org.javamoney.moneta> TCK: org.javamoney.tck

/** * Simplest case create an amount with an ISO currency. */public void forISOCurrencies() { Money amount = Money.of(1234566.15, "USD"); // using ISO namespace by default}

/** * Create an amount using a custom currency. */public void forCustomCurrencies() { CurrencyUnit currency = Monetary.getCurrency("myCode"); Money amount = Money.of(1234566.15, currency);}

6 © 2012-2015 JavaMoney contributors

Creating Amounts

Currency Conversion> ExchangeRateType> ExchangeRate:

– ConversionContext– Base, Term currency– Conversion factor– Direct/Derived Rates

> ConversionProvider/CurrencyConverter> MonetaryConversions accessor singleton

public interface ExchangeRate { public CurrencyUnit getBaseCurrency(); public CurrencyUnit getCurrency(); public NumberValue getFactor(); public ConversionContext getContext(); public List<ExchangeRate> getExchangeRateChain(); public boolean isDerived();}

7 © 2012-2015 JavaMoney contributors

Werner Keil
Is this a financial or accounting term??

// Shows accessing exchange rates and doing conversions. MonetaryAmount amt = Money.of(2000, "EUR"); System.out.println("2000 EUR -(ECB)-> HUF = " + amt.with(MonetaryConversions.getConversion("HUF", "ECB"))); System.out.println("2000 EUR -(IMF)-> HUF = " + amt.with(MonetaryConversions.getConversion("HUF", "IMF"))); System.out.println("2000 EUR -(ECB, at 5th Jan 2015)-> HUF = " + amt.with(MonetaryConversions.getConversion(ConversionQueryBuilder.of().setTermCurrency("HUF") .set(LocalDate.of(2015, 01, 05)).build())));

8 © 2012-2015 JavaMoney contributors

Currency Conversion

Virtual Currencies> Video Game Currencies (Gold, Gil, Rupees, Credits, Gold Rings, Hearts, Zenny,

Potch, Munny, Nuyen…)> Facebook Credits are a virtual currency you can use to buy

virtual goods in any games or apps of the Facebook platform that accept payments. You can purchase Facebook Credits directly from within an app using your credit card, PayPal, mobile phone and many other local payment methods.

> Bitcoin (sign: BTC) is a decentralized digital currency based on an open-source, peer-to-peer internet protocol. It was introduced by a pseudonymous developer named Satoshi Nakamoto (allegedly Australian tech entrepreneur Craig Wright) in 2009.

AdoptersZalando

10 © 2012-2015 JavaMoney contributors Image: Zalando

Gimme ShelterAdopt-a-JSR and similar Efforts

11 © 2012-2015 JavaMoney contributors

Links• JSR 354: https://jcp.org/en/jsr/detail?id=354• JavaMoney Site: http://javamoney.org• JavaMoney Shelter: http://javamoney.github.io/shelter.html• GitHub Organization: https://github.com/JavaMoney/• Twitter: @jsr354

Demojavax.money

Money & Currency

Adopt-A-JSR

JSR-354

Contributing