JSR 354: Money and Currency API - Short Overview

14
JSR 354: Money and Currency API Werner Keil | Otávio Santana @wernerkeil | @otaviojava | @jsr354 https://www.jcp.org/en/jsr/ detail?id=354 http://javamoney.org

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

Page 1: 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€

Page 2: JSR 354: Money and Currency API - Short Overview

History and Motivation

Page 3: JSR 354: Money and Currency API - Short Overview

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.

Page 4: JSR 354: Money and Currency API - Short Overview

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

Page 5: JSR 354: Money and Currency API - Short Overview

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

Page 6: JSR 354: Money and Currency API - Short Overview

/** * 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

Page 7: JSR 354: Money and Currency API - Short Overview

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??
Page 8: JSR 354: Money and Currency API - Short Overview

// 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

Page 9: JSR 354: Money and Currency API - Short Overview

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.

Page 10: JSR 354: Money and Currency API - Short Overview

AdoptersZalando

10 © 2012-2015 JavaMoney contributors Image: Zalando

Page 11: JSR 354: Money and Currency API - Short Overview

Gimme ShelterAdopt-a-JSR and similar Efforts

11 © 2012-2015 JavaMoney contributors

Page 12: JSR 354: Money and Currency API - Short Overview

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

Page 13: JSR 354: Money and Currency API - Short Overview

Demojavax.money

Page 14: JSR 354: Money and Currency API - Short Overview

Money & Currency

Adopt-A-JSR

JSR-354

Contributing