From fc823505ae77be8badc6cb289449a35e58215d3c Mon Sep 17 00:00:00 2001 From: Shane Carr Date: Tue, 11 Jul 2017 17:24:34 +0000 Subject: [PATCH] ICU-12690 Adding convenience overloads for java.util.Currency. X-SVN-Rev: 40253 --- .../core/src/com/ibm/icu/util/Currency.java | 21 +++++++++++++++ .../src/com/ibm/icu/util/CurrencyAmount.java | 26 ++++++++++++++++--- .../icu/dev/test/format/NumberFormatTest.java | 20 ++++++++++++-- 3 files changed, 62 insertions(+), 5 deletions(-) diff --git a/icu4j/main/classes/core/src/com/ibm/icu/util/Currency.java b/icu4j/main/classes/core/src/com/ibm/icu/util/Currency.java index 69dbba94ccf..8b99564781a 100644 --- a/icu4j/main/classes/core/src/com/ibm/icu/util/Currency.java +++ b/icu4j/main/classes/core/src/com/ibm/icu/util/Currency.java @@ -318,6 +318,27 @@ public class Currency extends MeasureUnit { return true; } + /** + * Returns a Currency object based on the currency represented by the given java.util.Currency. + * + * @param currency The Java currency object to convert. + * @return An equivalent ICU currency object. + * @draft ICU 60 + */ + public static Currency fromJavaCurrency(java.util.Currency currency) { + return getInstance(currency.getCurrencyCode()); + } + + /** + * Returns a java.util.Currency object based on the currency represented by this Currency. + * + * @return An equivalent Java currency object. + * @draft ICU 60 + */ + public java.util.Currency toJavaCurrency() { + return java.util.Currency.getInstance(getCurrencyCode()); + } + /** * Registers a new currency for the provided locale. The returned object * is a key that can be used to unregister this currency object. diff --git a/icu4j/main/classes/core/src/com/ibm/icu/util/CurrencyAmount.java b/icu4j/main/classes/core/src/com/ibm/icu/util/CurrencyAmount.java index c7e118d33d3..7f92d8ed95a 100644 --- a/icu4j/main/classes/core/src/com/ibm/icu/util/CurrencyAmount.java +++ b/icu4j/main/classes/core/src/com/ibm/icu/util/CurrencyAmount.java @@ -23,7 +23,7 @@ package com.ibm.icu.util; * @stable ICU 3.0 */ public class CurrencyAmount extends Measure { - + /** * Constructs a new object given a number and a currency. * @param number the number @@ -42,8 +42,28 @@ public class CurrencyAmount extends Measure { */ public CurrencyAmount(double number, Currency currency) { super(new Double(number), currency); - } - + } + + /** + * Constructs a new object given a number and a Java currency. + * @param number the number + * @param currency the currency + * @draft ICU 60 + */ + public CurrencyAmount(Number number, java.util.Currency currency) { + this(number, Currency.fromJavaCurrency(currency)); + } + + /** + * Constructs a new object given a double value and a Java currency. + * @param number a double value + * @param currency the currency + * @draft ICU 60 + */ + public CurrencyAmount(double number, java.util.Currency currency) { + this(number, Currency.fromJavaCurrency(currency)); + } + /** * Returns the currency of this object. * @return this object's Currency diff --git a/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/NumberFormatTest.java b/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/NumberFormatTest.java index 7fe8d46b479..928bb66595d 100644 --- a/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/NumberFormatTest.java +++ b/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/NumberFormatTest.java @@ -686,6 +686,22 @@ public class NumberFormatTest extends TestFmwk { ULocale.setDefault(save); } + @Test + public void TestJavaCurrencyConversion() { + java.util.Currency gbpJava = java.util.Currency.getInstance("GBP"); + Currency gbpIcu = Currency.getInstance("GBP"); + assertEquals("ICU should equal API value", gbpIcu, Currency.fromJavaCurrency(gbpJava)); + assertEquals("Java should equal API value", gbpJava, gbpIcu.toJavaCurrency()); + // Test CurrencyAmount constructors + CurrencyAmount ca1 = new CurrencyAmount(123.45, gbpJava); + CurrencyAmount ca2 = new CurrencyAmount(123.45, gbpIcu); + assertEquals("CurrencyAmount from both Double constructors should be equal", ca1, ca2); + // Coverage for the Number constructor + ca1 = new CurrencyAmount(new BigDecimal("543.21"), gbpJava); + ca2 = new CurrencyAmount(new BigDecimal("543.21"), gbpIcu); + assertEquals("CurrencyAmount from both Number constructors should be equal", ca1, ca2); + } + @Test public void TestCurrencyIsoPluralFormat() { String[][] DATA = { @@ -3554,12 +3570,12 @@ public class NumberFormatTest extends TestFmwk { CurrencyAmount ca, cb; try { - ca = new CurrencyAmount(null, null); + ca = new CurrencyAmount(null, (Currency) null); errln("NullPointerException should have been thrown."); } catch (NullPointerException ex) { } try { - ca = new CurrencyAmount(new Integer(0), null); + ca = new CurrencyAmount(new Integer(0), (Currency) null); errln("NullPointerException should have been thrown."); } catch (NullPointerException ex) { } -- 2.40.0