]> granicus.if.org Git - icu/commitdiff
ICU-12690 Adding convenience overloads for java.util.Currency.
authorShane Carr <shane@unicode.org>
Tue, 11 Jul 2017 17:24:34 +0000 (17:24 +0000)
committerShane Carr <shane@unicode.org>
Tue, 11 Jul 2017 17:24:34 +0000 (17:24 +0000)
X-SVN-Rev: 40253

icu4j/main/classes/core/src/com/ibm/icu/util/Currency.java
icu4j/main/classes/core/src/com/ibm/icu/util/CurrencyAmount.java
icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/NumberFormatTest.java

index 69dbba94ccfabd950006d243451390e5df97c1f7..8b99564781afb1b16812e67f7da9e3aebc9fdce1 100644 (file)
@@ -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.
index c7e118d33d306956427c17f50578233a177ba069..7f92d8ed95a468b619e82fd139d6403608055202 100644 (file)
@@ -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
index 7fe8d46b479562528b4d34848225710afe0cd524..928bb66595d5bb09fed83ddd6bdaa69a56ee29f0 100644 (file)
@@ -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) {
         }