Java 9中新的貨幣API

Java譯站發表於2015-01-10

JSR 354定義了一套新的Java貨幣API,計劃會在Java 9中正式引入。本文中我們將來看一下它的參考實現:JavaMoney的當前進展。

正如我在之前那篇Java 8新的日期時間API一文中那樣,本文主要也是通過一些程式碼來演示下新的API的用法 。

在開始之前,我想先用一段話來簡短地總結一下規範定義的這套新的API的用意何在:

對許多應用而言貨幣價值都是一個關鍵的特性,但JDK對此卻幾乎沒有任何支援。嚴格來講,現有的java.util.Currency類只是代表了當前ISO
4217貨幣的一個資料結構,但並沒有關聯的值或者自定義貨幣。JDK對貨幣的運算及轉換也沒有內建的支援,更別說有一個能夠代表貨幣值的標準型別了。

如果你用的是Maven的話,只需把下面的引用新增到工裡面便能夠體驗下該參考實現的當前功能了:

<dependency>
  <groupId>org.javamoney</groupId>
  <artifactId>moneta</artifactId>
  <version>0.9</version>
</dependency>

規範中提到的類及介面都在javax.money.*包下面。

我們先從核心的兩個介面CurrencyUnit與MonetaryAmount開始講起。

CurrencyUnit及MonetaryAmount

CurrencyUnit代表的是貨幣。它有點類似於現在的java.util.Currency類,不同之處在於它支援自定義的實現。從規範的定義來看,java.util.Currency也是可以實現該介面的。CurrencyUnit的例項可以通過MonetaryCurrencies工廠來獲取:

// 根據貨幣程式碼來獲取貨幣單位 CurrencyUnit euro = MonetaryCurrencies.getCurrency("EUR");
    CurrencyUnit usDollar = MonetaryCurrencies.getCurrency("USD"); // 根據國家及地區來獲取貨幣單位
    CurrencyUnit yen = MonetaryCurrencies.getCurrency(Locale.JAPAN); CurrencyUnit
    canadianDollar = MonetaryCurrencies.getCurrency(Locale.CANADA);

MontetaryAmount代表的是某種貨幣的具體金額。通常它都會與某個CurrencyUnit繫結。MontetaryAmount和CurrencyUnit一樣,也是一個能支援多種實現的介面。CurrencyUnit與MontetaryAmount的實現必須是不可變,執行緒安全且可比較的。

/ get MonetaryAmount from CurrencyUnit
CurrencyUnit euro = MonetaryCurrencies.getCurrency("EUR");
MonetaryAmount fiveEuro = Money.of(5, euro);

// get MonetaryAmount from currency code
MonetaryAmount tenUsDollar = Money.of(10, "USD");

// FastMoney is an alternative MonetaryAmount factory that focuses on performance
MonetaryAmount sevenEuro = FastMoney.of(7, euro);

Money與FastMoney是JavaMoney庫中MonetaryAmount的兩種實現。Money是預設實現,它使用BigDecimal來儲存金額。FastMoney是可選的另一個實現,它用long型別來儲存金額。根據文件來看,FastMoney上的操作要比Money的快10到15倍左右。然而,FastMoney的金額大小與精度都受限於long型別。

注意了,這裡的Money和FastMoney都是具體的實現類(它們在org.javamoney.moneta.*包下面,而不是javax.money.*)。如果你不希望指定具體型別的話,可以通過MonetaryAmountFactory來生成一個MonetaryAmount的例項:

MonetaryAmount specAmount = MonetaryAmounts.getDefaultAmountFactory()
                .setNumber(123.45) .setCurrency("USD") .create();

當且僅當實現類,貨幣單位,以及數值全部相等時才認為這兩個MontetaryAmount例項是相等的。

MonetaryAmount oneEuro = Money.of(1, MonetaryCurrencies.getCurrency("EUR"));
boolean isEqual = oneEuro.equals(Money.of(1, "EUR")); // true
boolean isEqualFast = oneEuro.equals(FastMoney.of(1, "EUR")); // false

MonetaryAmount內包含豐富的方法,可以用來獲取具體的貨幣,金額,精度等等:

MonetaryAmount monetaryAmount = Money.of(123.45, euro);
CurrencyUnit currency = monetaryAmount.getCurrency();
NumberValue numberValue = monetaryAmount.getNumber();

int intValue = numberValue.intValue(); // 123
double doubleValue = numberValue.doubleValue(); // 123.45
long fractionDenominator = numberValue.getAmountFractionDenominator(); // 100
long fractionNumerator = numberValue.getAmountFractionNumerator(); // 45
int precision = numberValue.getPrecision(); // 5

// NumberValue extends java.lang.Number. 
// So we assign numberValue to a variable of type Number
Number number = numberValue;

MonetaryAmount的使用

可以在MonetaryAmount上進行算術運算:

MonetaryAmount twelveEuro = fiveEuro.add(sevenEuro); // "EUR 12"
MonetaryAmount twoEuro = sevenEuro.subtract(fiveEuro); // "EUR 2"
MonetaryAmount sevenPointFiveEuro = fiveEuro.multiply(1.5); // "EUR 7.5"

// MonetaryAmount can have a negative NumberValue
MonetaryAmount minusTwoEuro = fiveEuro.subtract(sevenEuro); // "EUR -2"

// some useful utility methods
boolean greaterThan = sevenEuro.isGreaterThan(fiveEuro); // true
boolean positive = sevenEuro.isPositive(); // true
boolean zero = sevenEuro.isZero(); // false

// Note that MonetaryAmounts need to have the same CurrencyUnit to do mathematical operations
// this fails with: javax.money.MonetaryException: Currency mismatch: EUR/USD
fiveEuro.add(tenUsDollar);

舍入操作是金額換算裡面非常重要的一部分。MonetaryAmount可以使用舍入操作符來進行四捨五入:

CurrencyUnit usd = MonetaryCurrencies.getCurrency("USD");
MonetaryAmount dollars = Money.of(12.34567, usd);
MonetaryOperator roundingOperator = MonetaryRoundings.getRounding(usd);
MonetaryAmount roundedDollars = dollars.with(roundingOperator); // USD 12.35

這裡12.3456美金就會按當前貨幣預設的舍入規則來進行換算。

在操作MonetaryAmount集合時,有許多實用的工具方法可以用來進行過濾,排序以及分組。這些方法還可以與Java 8的流API一起配套使用。

看一下下面這個集合:

List<MonetaryAmount> amounts = new ArrayList<>();
amounts.add(Money.of(2, "EUR"));
amounts.add(Money.of(42, "USD"));
amounts.add(Money.of(7, "USD"));
amounts.add(Money.of(13.37, "JPY"));
amounts.add(Money.of(18, "USD"));

我們可以根據CurrencyUnit來進行金額過濾:

CurrencyUnit yen = MonetaryCurrencies.getCurrency("JPY");
CurrencyUnit dollar = MonetaryCurrencies.getCurrency("USD");
// 根據貨幣過濾,只返回美金
// result is [USD 18, USD 7, USD 42]
List<MonetaryAmount> onlyDollar = amounts.stream()
    .filter(MonetaryFunctions.isCurrency(dollar))
    .collect(Collectors.toList());

// 根據貨幣過濾,只返回美金和日元
// [USD 18, USD 7, JPY 13.37, USD 42]
List<MonetaryAmount> onlyDollarAndYen = amounts.stream()
    .filter(MonetaryFunctions.isCurrency(dollar, yen))
    .collect(Collectors.toList());

我們還可以過濾出大於或小於某個閾值的金額:

MonetaryAmount tenDollar = Money.of(10, dollar);

// [USD 42, USD 18]
List<MonetaryAmount> greaterThanTenDollar = amounts.stream()
    .filter(MonetaryFunctions.isCurrency(dollar))
    .filter(MonetaryFunctions.isGreaterThan(tenDollar))
    .collect(Collectors.toList());

排序也是類似的:

// Sorting dollar values by number value
// [USD 7, USD 18, USD 42]
List<MonetaryAmount> sortedByAmount = onlyDollar.stream()
    .sorted(MonetaryFunctions.sortNumber())
    .collect(Collectors.toList());

// Sorting by CurrencyUnit
// [EUR 2, JPY 13.37, USD 42, USD 7, USD 18]
List<MonetaryAmount> sortedByCurrencyUnit = amounts.stream()
    .sorted(MonetaryFunctions.sortCurrencyUnit())
    .collect(Collectors.toList());

還有分組操作:

// 按貨幣單位進行分組
// {USD=[USD 42, USD 7, USD 18], EUR=[EUR 2], JPY=[JPY 13.37]}
Map<CurrencyUnit, List<MonetaryAmount>> groupedByCurrency = amounts.stream()
    .collect(MonetaryFunctions.groupByCurrencyUnit());

// 分組並進行彙總
Map<CurrencyUnit, MonetarySummaryStatistics> summary = amounts.stream()
    .collect(MonetaryFunctions.groupBySummarizingMonetary()).get();

// get summary for CurrencyUnit USD
MonetarySummaryStatistics dollarSummary = summary.get(dollar);
MonetaryAmount average = dollarSummary.getAverage(); // "USD 22.333333333333333333.."
MonetaryAmount min = dollarSummary.getMin(); // "USD 7"
MonetaryAmount max = dollarSummary.getMax(); // "USD 42"
MonetaryAmount sum = dollarSummary.getSum(); // "USD 67"
long count = dollarSummary.getCount(); // 3

MonetaryFunctions還提供了歸約函式,可以用來獲取最大值,最小值,以及求和:

List<MonetaryAmount> amounts = new ArrayList<>();
amounts.add(Money.of(10, "EUR"));
amounts.add(Money.of(7.5, "EUR"));
amounts.add(Money.of(12, "EUR"));

Optional<MonetaryAmount> max = amounts.stream().reduce(MonetaryFunctions.max()); // "EUR 7.5"
Optional<MonetaryAmount> min = amounts.stream().reduce(MonetaryFunctions.min()); // "EUR 12"
Optional<MonetaryAmount> sum = amounts.stream().reduce(MonetaryFunctions.sum()); //

自定義的MonetaryAmount操作

MonetaryAmount還提供了一個非常友好的擴充套件點叫作MonetaryOperator。MonetaryOperator是一個函式式介面,它接收一個MonetaryAmount入參並返回一個新的MonetaryAmount物件。

// A monetary operator that returns 10% of the input MonetaryAmount
// Implemented using Java 8 Lambdas
MonetaryOperator tenPercentOperator = (MonetaryAmount amount) -> {
  BigDecimal baseAmount = amount.getNumber().numberValue(BigDecimal.class);
  BigDecimal tenPercent = baseAmount.multiply(new BigDecimal("0.1"));
  return Money.of(tenPercent, amount.getCurrency());
};

MonetaryAmount dollars = Money.of(12.34567, "USD");

// apply tenPercentOperator to MonetaryAmount
MonetaryAmount tenPercentDollars = dollars.with(tenPercentOperator); // USD 1.234567

標準的API特性都是通過MonetaryOperator的介面來實現的。比方說,前面看到的舍入操作就是以MonetaryOperator介面的形式來提供的。

匯率

貨幣兌換率可以通過ExchangeRateProvider來獲取。JavaMoney自帶了多個不同的ExchangeRateProvider的實現。其中最重要的兩個是ECBCurrentRateProvider與 IMFRateProvider。

ECBCurrentRateProvider查詢的是歐洲中央銀行(European Central Bank,ECB)的資料而IMFRateProvider查詢的是國際貨幣基金組織(International Monetary Fund,IMF)的匯率。

// get the default ExchangeRateProvider (CompoundRateProvider)
ExchangeRateProvider exchangeRateProvider = MonetaryConversions.getExchangeRateProvider();

// get the names of the default provider chain
// [IDENT, ECB, IMF, ECB-HIST]
List<String> defaultProviderChain = MonetaryConversions.getDefaultProviderChain();

// get a specific ExchangeRateProvider (here ECB)
ExchangeRateProvider ecbExchangeRateProvider = MonetaryConversions.getExchangeRateProvider("ECB");

如果沒有指定ExchangeRateProvider的話返回的就是CompoundRateProvider。CompoundRateProvider會將匯率轉換請求委派給一個ExchangeRateProvider鏈並將第一個返回準確結果的提供商的資料返回。

// get the exchange rate from euro to us dollar
ExchangeRate rate = exchangeRateProvider.getExchangeRate("EUR", "USD");

NumberValue factor = rate.getFactor(); // 1.2537 (at time writing)
CurrencyUnit baseCurrency = rate.getBaseCurrency(); // EUR
CurrencyUnit targetCurrency = rate.getCurrency(); // USD

貨幣轉換

不同貨幣間的轉換可以通過ExchangeRateProvider返回的CurrencyConversions來完成。

// get the CurrencyConversion from the default provider chain
CurrencyConversion dollarConversion = MonetaryConversions.getConversion("USD");

// get the CurrencyConversion from a specific provider
CurrencyConversion ecbDollarConversion = ecbExchangeRateProvider.getCurrencyConversion("USD");

MonetaryAmount tenEuro = Money.of(10, "EUR");

// convert 10 euro to us dollar 
MonetaryAmount inDollar = tenEuro.with(dollarConversion); // "USD 12.537" (at the time writing)

請注意CurrencyConversion也實現了MonetaryOperator介面。正如其它操作一樣,它也能通過MonetaryAmount.with()方法來呼叫。

格式化及解析

MonetaryAmount可以通過MonetaryAmountFormat來與字串進行解析/格式化。

// formatting by locale specific formats
MonetaryAmountFormat germanFormat = MonetaryFormats.getAmountFormat(Locale.GERMANY);
MonetaryAmountFormat usFormat = MonetaryFormats.getAmountFormat(Locale.CANADA);

MonetaryAmount amount = Money.of(12345.67, "USD");

String usFormatted = usFormat.format(amount); // "USD12,345.67"
String germanFormatted = germanFormat.format(amount); // 12.345,67 USD

// A MonetaryAmountFormat can also be used to parse MonetaryAmounts from strings
MonetaryAmount parsed = germanFormat.parse("12,4 USD");

可以通過AmountFormatQueryBuilder來生成自定義的格式。

// Creating a custom MonetaryAmountFormat
MonetaryAmountFormat customFormat = MonetaryFormats.getAmountFormat(
    AmountFormatQueryBuilder.of(Locale.US)
        .set(CurrencyStyle.NAME)
        .set("pattern", "00,00,00,00.00 ¤")
        .build());

// results in "00,01,23,45.67 US Dollar"
String formatted = customFormat.format(amount);

注意,這裡的¤符號在模式串中是作為貨幣的佔位符。

總結

新的貨幣API這裡已經介紹得差不多了。並且目前它的實現也已經相對穩定了(但還需要多補充些文件)。期待能在Java 9中看到這套新的介面!

上述示例可在Github中下載到。

相關文章