author | chegar |
Sun, 17 Aug 2014 15:54:13 +0100 | |
changeset 25859 | 3317bb8137f4 |
parent 14342 | jdk/src/share/classes/java/util/spi/CurrencyNameProvider.java@8435a30053c1 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
14342
8435a30053c1
7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents:
13583
diff
changeset
|
2 |
* Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package java.util.spi; |
|
27 |
||
7255
88d0a8413bb8
6807534: CurrencyNameProvider.getDisplayName(String, Locale) doesn't throw IllegalArgumentException
naoto
parents:
5506
diff
changeset
|
28 |
import java.util.Arrays; |
2 | 29 |
import java.util.Currency; |
7255
88d0a8413bb8
6807534: CurrencyNameProvider.getDisplayName(String, Locale) doesn't throw IllegalArgumentException
naoto
parents:
5506
diff
changeset
|
30 |
import java.util.List; |
2 | 31 |
import java.util.Locale; |
13583 | 32 |
import java.util.ResourceBundle.Control; |
2 | 33 |
|
34 |
/** |
|
35 |
* An abstract class for service providers that |
|
36 |
* provide localized currency symbols and display names for the |
|
37 |
* {@link java.util.Currency Currency} class. |
|
38 |
* Note that currency symbols are considered names when determining |
|
39 |
* behaviors described in the |
|
40 |
* {@link java.util.spi.LocaleServiceProvider LocaleServiceProvider} |
|
41 |
* specification. |
|
42 |
* |
|
43 |
* @since 1.6 |
|
44 |
*/ |
|
45 |
public abstract class CurrencyNameProvider extends LocaleServiceProvider { |
|
46 |
||
47 |
/** |
|
48 |
* Sole constructor. (For invocation by subclass constructors, typically |
|
49 |
* implicit.) |
|
50 |
*/ |
|
51 |
protected CurrencyNameProvider() { |
|
52 |
} |
|
53 |
||
54 |
/** |
|
55 |
* Gets the symbol of the given currency code for the specified locale. |
|
56 |
* For example, for "USD" (US Dollar), the symbol is "$" if the specified |
|
57 |
* locale is the US, while for other locales it may be "US$". If no |
|
58 |
* symbol can be determined, null should be returned. |
|
59 |
* |
|
60 |
* @param currencyCode the ISO 4217 currency code, which |
|
61 |
* consists of three upper-case letters between 'A' (U+0041) and |
|
62 |
* 'Z' (U+005A) |
|
63 |
* @param locale the desired locale |
|
64 |
* @return the symbol of the given currency code for the specified locale, or null if |
|
65 |
* the symbol is not available for the locale |
|
66 |
* @exception NullPointerException if <code>currencyCode</code> or |
|
67 |
* <code>locale</code> is null |
|
68 |
* @exception IllegalArgumentException if <code>currencyCode</code> is not in |
|
69 |
* the form of three upper-case letters, or <code>locale</code> isn't |
|
70 |
* one of the locales returned from |
|
71 |
* {@link java.util.spi.LocaleServiceProvider#getAvailableLocales() |
|
72 |
* getAvailableLocales()}. |
|
73 |
* @see java.util.Currency#getSymbol(java.util.Locale) |
|
74 |
*/ |
|
75 |
public abstract String getSymbol(String currencyCode, Locale locale); |
|
76 |
||
77 |
/** |
|
78 |
* Returns a name for the currency that is appropriate for display to the |
|
79 |
* user. The default implementation returns null. |
|
80 |
* |
|
81 |
* @param currencyCode the ISO 4217 currency code, which |
|
82 |
* consists of three upper-case letters between 'A' (U+0041) and |
|
83 |
* 'Z' (U+005A) |
|
84 |
* @param locale the desired locale |
|
85 |
* @return the name for the currency that is appropriate for display to the |
|
86 |
* user, or null if the name is not available for the locale |
|
87 |
* @exception IllegalArgumentException if <code>currencyCode</code> is not in |
|
88 |
* the form of three upper-case letters, or <code>locale</code> isn't |
|
89 |
* one of the locales returned from |
|
90 |
* {@link java.util.spi.LocaleServiceProvider#getAvailableLocales() |
|
91 |
* getAvailableLocales()}. |
|
92 |
* @exception NullPointerException if <code>currencyCode</code> or |
|
93 |
* <code>locale</code> is <code>null</code> |
|
94 |
* @since 1.7 |
|
95 |
*/ |
|
96 |
public String getDisplayName(String currencyCode, Locale locale) { |
|
97 |
if (currencyCode == null || locale == null) { |
|
98 |
throw new NullPointerException(); |
|
99 |
} |
|
100 |
||
7255
88d0a8413bb8
6807534: CurrencyNameProvider.getDisplayName(String, Locale) doesn't throw IllegalArgumentException
naoto
parents:
5506
diff
changeset
|
101 |
// Check whether the currencyCode is valid |
88d0a8413bb8
6807534: CurrencyNameProvider.getDisplayName(String, Locale) doesn't throw IllegalArgumentException
naoto
parents:
5506
diff
changeset
|
102 |
char[] charray = currencyCode.toCharArray(); |
88d0a8413bb8
6807534: CurrencyNameProvider.getDisplayName(String, Locale) doesn't throw IllegalArgumentException
naoto
parents:
5506
diff
changeset
|
103 |
if (charray.length != 3) { |
88d0a8413bb8
6807534: CurrencyNameProvider.getDisplayName(String, Locale) doesn't throw IllegalArgumentException
naoto
parents:
5506
diff
changeset
|
104 |
throw new IllegalArgumentException("The currencyCode is not in the form of three upper-case letters."); |
88d0a8413bb8
6807534: CurrencyNameProvider.getDisplayName(String, Locale) doesn't throw IllegalArgumentException
naoto
parents:
5506
diff
changeset
|
105 |
} |
88d0a8413bb8
6807534: CurrencyNameProvider.getDisplayName(String, Locale) doesn't throw IllegalArgumentException
naoto
parents:
5506
diff
changeset
|
106 |
for (char c : charray) { |
88d0a8413bb8
6807534: CurrencyNameProvider.getDisplayName(String, Locale) doesn't throw IllegalArgumentException
naoto
parents:
5506
diff
changeset
|
107 |
if (c < 'A' || c > 'Z') { |
88d0a8413bb8
6807534: CurrencyNameProvider.getDisplayName(String, Locale) doesn't throw IllegalArgumentException
naoto
parents:
5506
diff
changeset
|
108 |
throw new IllegalArgumentException("The currencyCode is not in the form of three upper-case letters."); |
88d0a8413bb8
6807534: CurrencyNameProvider.getDisplayName(String, Locale) doesn't throw IllegalArgumentException
naoto
parents:
5506
diff
changeset
|
109 |
} |
88d0a8413bb8
6807534: CurrencyNameProvider.getDisplayName(String, Locale) doesn't throw IllegalArgumentException
naoto
parents:
5506
diff
changeset
|
110 |
} |
88d0a8413bb8
6807534: CurrencyNameProvider.getDisplayName(String, Locale) doesn't throw IllegalArgumentException
naoto
parents:
5506
diff
changeset
|
111 |
|
88d0a8413bb8
6807534: CurrencyNameProvider.getDisplayName(String, Locale) doesn't throw IllegalArgumentException
naoto
parents:
5506
diff
changeset
|
112 |
// Check whether the locale is valid |
13583 | 113 |
Control c = Control.getNoFallbackControl(Control.FORMAT_DEFAULT); |
114 |
for (Locale l : getAvailableLocales()) { |
|
115 |
if (c.getCandidateLocales("", l).contains(locale)) { |
|
116 |
return null; |
|
117 |
} |
|
7255
88d0a8413bb8
6807534: CurrencyNameProvider.getDisplayName(String, Locale) doesn't throw IllegalArgumentException
naoto
parents:
5506
diff
changeset
|
118 |
} |
88d0a8413bb8
6807534: CurrencyNameProvider.getDisplayName(String, Locale) doesn't throw IllegalArgumentException
naoto
parents:
5506
diff
changeset
|
119 |
|
13583 | 120 |
throw new IllegalArgumentException("The locale is not available"); |
2 | 121 |
} |
122 |
} |