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.
* @stable ICU 3.0
*/
public class CurrencyAmount extends Measure {
-
+
/**
* Constructs a new object given a number and a currency.
* @param number the number
*/
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
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 = {
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) {
}