001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package java.util;
019:
020: import java.io.Serializable;
021:
022: /**
023: * This class represents a currency as identified in the ISO 4217 currency
024: * codes.
025: */
026: public final class Currency implements Serializable {
027:
028: private static final long serialVersionUID = -158308464356906721L;
029:
030: private static Hashtable<String, Currency> codesToCurrencies = new Hashtable<String, Currency>();
031:
032: private String currencyCode;
033:
034: /**
035: * @param currencyCode
036: */
037: private Currency(String currencyCode) {
038: this .currencyCode = currencyCode;
039: }
040:
041: /**
042: * Returns the Currency instance for this currency code.
043: * <p>
044: *
045: * @param currencyCode
046: * java.lang.String
047: * @return currency java.util.Currency
048: *
049: * @throws java.lang.IllegalArgumentException
050: * if the currency code is not a supported ISO 4217 currency
051: * code
052: */
053: public static Currency getInstance(String currencyCode) {
054: Currency currency = codesToCurrencies.get(currencyCode);
055:
056: if (currency == null) {
057: currency = new Currency(currencyCode);
058: codesToCurrencies.put(currencyCode, currency);
059: }
060:
061: return currency;
062: }
063:
064: /***************************************************************************
065: * Returns the Currency instance for the given locale.
066: *
067: * @param locale
068: * java.util.Locale
069: * @return currency java.util.Currency
070: *
071: * @throws java.lang.IllegalArgumentException
072: * if the locale's country is not a supported ISO 3166 Country
073: */
074: public static Currency getInstance(Locale locale) {
075: com.ibm.icu.util.Currency currency = null;
076: try {
077: currency = com.ibm.icu.util.Currency.getInstance(locale);
078: } catch (IllegalArgumentException e) {
079: return null;
080: }
081: if (currency == null) {
082: throw new IllegalArgumentException(locale.getCountry());
083: }
084: String currencyCode = currency.getCurrencyCode();
085:
086: if (currencyCode.equals("None")) { //$NON-NLS-1$
087: return null;
088: }
089:
090: return getInstance(currencyCode);
091: }
092:
093: /**
094: * Returns this currency's ISO 4217 currency code.
095: *
096: * @return this currency's ISO 4217 currency code
097: */
098: public String getCurrencyCode() {
099: return currencyCode;
100: }
101:
102: /**
103: * Returns the currency symbol for the default locale.
104: *
105: * Equivalent to <code>getSymbol(Locale.getDefault())</code>
106: *
107: * @return the currency symbol for the default locale.
108: */
109: public String getSymbol() {
110: return getSymbol(Locale.getDefault());
111: }
112:
113: /**
114: * Returns the currency symbol for the given locale.
115: * <p>
116: *
117: * If the locale doesn't have any countries (e.g.
118: * <code>Locale.JAPANESE, new Locale("en","")</code>), currencyCode is
119: * returned.
120: * <p>
121: * First the locale bundle is checked, if the locale has the same currency,
122: * the CurrencySymbol in this locale bundle is returned.
123: * <p>
124: * Then a currency bundle for this locale is searched.
125: * <p>
126: * If a currency bundle for this locale does not exist, or there is no
127: * symbol for this currency in this bundle, than <code>currencyCode</code>
128: * is returned.
129: * <p>
130: *
131: * @param locale
132: * the locale
133: * @return symbol the representation of this Currency's symbol in this
134: * locale
135: */
136: public String getSymbol(Locale locale) {
137: if (locale.getCountry().equals("")) { //$NON-NLS-1$
138: return currencyCode;
139: }
140: return com.ibm.icu.util.Currency.getInstance(currencyCode)
141: .getSymbol(locale);
142: }
143:
144: /**
145: * Returns the default number of fraction digits for this currency (i.e. the
146: * number of digits after the decimal point). For pseudo currencies this
147: * method returns -1.
148: *
149: * @return the default number of fraction digits for this currency
150: */
151: public int getDefaultFractionDigits() {
152: return com.ibm.icu.util.Currency.getInstance(currencyCode)
153: .getDefaultFractionDigits();
154: }
155:
156: /**
157: * Returns this currency's ISO 4217 currency code.
158: *
159: * @return this currency's ISO 4217 currency code
160: */
161: @Override
162: public String toString() {
163: return currencyCode;
164: }
165:
166: private Object readResolve() {
167: return getInstance(currencyCode);
168: }
169: }
|