]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_4_2-src/main/classes/core/src/com/ibm/icu/text/CurrencyMetaInfo.java
go
[Dictionary.git] / jars / icu4j-4_4_2-src / main / classes / core / src / com / ibm / icu / text / CurrencyMetaInfo.java
1 /*\r
2  *******************************************************************************\r
3  * Copyright (C) 2009-2010, International Business Machines Corporation and    *\r
4  * others. All Rights Reserved.                                                *\r
5  *******************************************************************************\r
6  */\r
7 package com.ibm.icu.text;\r
8 \r
9 import java.lang.reflect.Field;\r
10 import java.util.Collections;\r
11 import java.util.Date;\r
12 import java.util.List;\r
13 \r
14 import com.ibm.icu.util.Calendar;\r
15 import com.ibm.icu.util.GregorianCalendar;\r
16 \r
17 /**\r
18  * Provides information about currencies that is not specific to a locale.\r
19  * @draft ICU 4.4\r
20  * @provisional This API might change or be removed in a future release.\r
21  */\r
22 public class CurrencyMetaInfo {\r
23     private static final CurrencyMetaInfo impl;\r
24     private static final boolean hasData;\r
25 \r
26     /**\r
27      * Returns the unique instance of the currency meta info.\r
28      * @return the meta info\r
29      * @draft ICU 4.4\r
30      * @provisional This API might change or be removed in a future release.\r
31      */\r
32     public static CurrencyMetaInfo getInstance() {\r
33         return impl;\r
34     }\r
35 \r
36     /**\r
37      * Returns true if there is data for the currency meta info.\r
38      * @return true if there is actual data\r
39      * @draft ICU 4.4\r
40      * @provisional This API might change or be removed in a future release.\r
41      */\r
42     public static boolean hasData() {\r
43         return hasData;\r
44     }\r
45 \r
46     /**\r
47      * Subclass constructor.\r
48      * @internal\r
49      * @deprecated This API is ICU internal only.\r
50      */\r
51     protected CurrencyMetaInfo() {\r
52     }\r
53 \r
54     /**\r
55      * A filter used to select which currency info is returned.\r
56      * @draft ICU 4.4\r
57      * @provisional This API might change or be removed in a future release.\r
58      */\r
59     public static final class CurrencyFilter {\r
60         /**\r
61          * The region to filter on.  If null, accepts any region.\r
62          * @draft ICU 4.4\r
63          * @provisional This API might change or be removed in a future release.\r
64          */\r
65         public final String region;\r
66 \r
67         /**\r
68          * The currency to filter on.  If null, accepts any currency.\r
69          * @draft ICU 4.4\r
70          * @provisional This API might change or be removed in a future release.\r
71          */\r
72         public final String currency;\r
73 \r
74         /**\r
75          * The from date to filter on (milliseconds).  Accepts any currency on or after this date.\r
76          * @draft ICU 4.4\r
77          * @provisional This API might change or be removed in a future release.\r
78          */\r
79         public final long from;\r
80 \r
81         /**\r
82          * The to date to filter on (milliseconds).  Accepts any currency on or before this date.\r
83          * @draft ICU 4.4\r
84          * @provisional This API might change or be removed in a future release.\r
85          */\r
86         public final long to;\r
87 \r
88         private CurrencyFilter(String region, String currency, long from, long to) {\r
89             this.region = region;\r
90             this.currency = currency;\r
91             this.from = from;\r
92             this.to = to;\r
93         }\r
94 \r
95         private CurrencyFilter(String region, String currency, Date dateFrom, Date dateTo) {\r
96             this.region = region;\r
97             this.currency = currency;\r
98             this.from = dateFrom == null ? Long.MIN_VALUE : dateFrom.getTime();\r
99             this.to = dateTo == null ? Long.MAX_VALUE : dateTo.getTime();\r
100         }\r
101 \r
102         private static final CurrencyFilter ALL = new CurrencyFilter(null, null, null, null);\r
103 \r
104         /**\r
105          * Returns a filter that accepts all currency data.\r
106          * @return a filter\r
107          * @draft ICU 4.4\r
108          * @provisional This API might change or be removed in a future release.\r
109          */\r
110         public static CurrencyFilter all() {\r
111             return ALL;\r
112         }\r
113 \r
114         /**\r
115          * Returns a filter that accepts all currencies in use as of the current date.\r
116          * @return a filter\r
117          * @see #withDate(Date)\r
118          * @draft ICU 4.4\r
119          * @provisional This API might change or be removed in a future release.\r
120          */\r
121         public static CurrencyFilter now() {\r
122             return ALL.withDate(new Date());\r
123         }\r
124 \r
125         /**\r
126          * Returns a filter that accepts all currencies ever used in the given region.\r
127          * @param region the region code\r
128          * @return a filter\r
129          * @see #withRegion(String)\r
130          * @draft ICU 4.4\r
131          * @provisional This API might change or be removed in a future release.\r
132          */\r
133         public static CurrencyFilter onRegion(String region) {\r
134             return ALL.withRegion(region);\r
135         }\r
136 \r
137         /**\r
138          * Returns a filter that accepts the given currency.\r
139          * @param currency the currency code\r
140          * @return a filter\r
141          * @see #withCurrency(String)\r
142          * @draft ICU 4.4\r
143          * @provisional This API might change or be removed in a future release.\r
144          */\r
145         public static CurrencyFilter onCurrency(String currency) {\r
146             return ALL.withCurrency(currency);\r
147         }\r
148 \r
149         /**\r
150          * Returns a filter that accepts all currencies in use on the given date.\r
151          * @param date the date\r
152          * @return a filter\r
153          * @see #withDate(Date)\r
154          * @draft ICU 4.4\r
155          * @provisional This API might change or be removed in a future release.\r
156          */\r
157         public static CurrencyFilter onDate(Date date) {\r
158             return ALL.withDate(date);\r
159         }\r
160 \r
161         /**\r
162          * Returns a filter that accepts all currencies that were in use at some point between\r
163          * the given dates, or if dates are equal, currencies in use on that date.\r
164          * @param from date on or after a currency must have been in use\r
165          * @param to date before which a currency must have been in use, or if equal to from,\r
166          * the date on which a currency must have been in use\r
167          * @return a filter\r
168          * @see #withRange(Date, Date)\r
169          * @draft ICU 4.4\r
170          * @provisional This API might change or be removed in a future release.\r
171          */\r
172         public static CurrencyFilter onRange(Date from, Date to) {\r
173             return ALL.withRange(from, to);\r
174         }\r
175 \r
176         /**\r
177          * Returns a copy of this filter, with the specified region.  Region can be null to\r
178          * indicate no filter on region.\r
179          * @param region the region code\r
180          * @return the filter\r
181          * @see #onRegion(String)\r
182          * @draft ICU 4.4\r
183          * @provisional This API might change or be removed in a future release.\r
184          */\r
185         public CurrencyFilter withRegion(String region) {\r
186             return new CurrencyFilter(region, this.currency, this.from, this.to);\r
187         }\r
188 \r
189         /**\r
190          * Returns a copy of this filter, with the specified currency.  Currency can be null to\r
191          * indicate no filter on currency.\r
192          * @param currency the currency code\r
193          * @return the filter\r
194          * @see #onCurrency(String)\r
195          * @draft ICU 4.4\r
196          * @provisional This API might change or be removed in a future release.\r
197          */\r
198         public CurrencyFilter withCurrency(String currency) {\r
199             return new CurrencyFilter(this.region, currency, this.from, this.to);\r
200         }\r
201 \r
202         /**\r
203          * Returns a copy of this filter, with from and to set to the given date.\r
204          * @param date the date on which the currency must have been in use\r
205          * @return the filter\r
206          * @see #onDate(Date)\r
207          * @draft ICU 4.4\r
208          * @provisional This API might change or be removed in a future release.\r
209          */\r
210         public CurrencyFilter withDate(Date date) {\r
211             return new CurrencyFilter(this.region, this.currency, date, date);\r
212         }\r
213 \r
214         /**\r
215          * Returns a copy of this filter, with from and to set to the given dates.\r
216          * @param from date on or after which the currency must have been in use\r
217          * @param to date before which the currency must have been in use\r
218          * @return the filter\r
219          * @see #onRange(Date, Date)\r
220          * @draft ICU 4.4\r
221          * @provisional This API might change or be removed in a future release.\r
222          */\r
223         public CurrencyFilter withRange(Date from, Date to) {\r
224             return new CurrencyFilter(this.region, this.currency, from, to);\r
225         }\r
226 \r
227         /**\r
228          * Overrides equals.\r
229          * @draft ICU 4.4\r
230          * @provisional This API might change or be removed in a future release.\r
231          */\r
232         @Override\r
233         public boolean equals(Object rhs) {\r
234             return rhs instanceof CurrencyFilter &&\r
235                 equals((CurrencyFilter) rhs);\r
236         }\r
237 \r
238         /**\r
239          * Type-safe override of {@link #equals(Object)}.\r
240          * @param rhs the currency filter to compare to\r
241          * @return true if the filters are equal\r
242          * @draft ICU 4.4\r
243          * @provisional This API might change or be removed in a future release.\r
244          */\r
245         public boolean equals(CurrencyFilter rhs) {\r
246             return this == rhs || (rhs != null &&\r
247                     equals(this.region, rhs.region) &&\r
248                     equals(this.currency, rhs.currency) &&\r
249                     this.from == rhs.from &&\r
250                     this.to == rhs.to);\r
251         }\r
252 \r
253         /**\r
254          * Overrides hashCode.\r
255          * @draft ICU 4.4\r
256          * @provisional This API might change or be removed in a future release.\r
257          */\r
258         @Override\r
259         public int hashCode() {\r
260             int hc = 0;\r
261             if (region != null) {\r
262                 hc = region.hashCode();\r
263             }\r
264             if (currency != null) {\r
265                 hc = hc * 31 + currency.hashCode();\r
266             }\r
267             hc = hc * 31 + (int) from;\r
268             hc = hc * 31 + (int) (from >>> 32);\r
269             hc = hc * 31 + (int) to;\r
270             hc = hc * 31 + (int) (to >>> 32);\r
271             return hc;\r
272         }\r
273 \r
274         /**\r
275          * Returns a string representing the filter, for debugging.\r
276          * @draft ICU 4.4\r
277          * @provisional This API might change or be removed in a future release.\r
278          */\r
279         @Override\r
280         public String toString() {\r
281             return debugString(this);\r
282         }\r
283 \r
284         private static boolean equals(String lhs, String rhs) {\r
285             return lhs == rhs || (lhs != null && lhs.equals(rhs));\r
286         }\r
287     }\r
288 \r
289     /**\r
290      * Represents the raw information about fraction digits and rounding increment.\r
291      * @draft ICU 4.4\r
292      * @provisional This API might change or be removed in a future release.\r
293      */\r
294     public static final class CurrencyDigits {\r
295         /**\r
296          * Number of fraction digits used to display this currency.\r
297          * @draft ICU 4.4\r
298          * @provisional This API might change or be removed in a future release.\r
299          */\r
300         public final byte fractionDigits;\r
301         /**\r
302          * Rounding increment used when displaying this currency.\r
303          * @draft ICU 4.4\r
304          * @provisional This API might change or be removed in a future release.\r
305          */\r
306         public final byte roundingIncrement;\r
307 \r
308         /**\r
309          * Constructor for CurrencyDigits.\r
310          * @param fractionDigits the fraction digits\r
311          * @param roundingIncrement the rounding increment\r
312          * @draft ICU 4.4\r
313          * @provisional This API might change or be removed in a future release.\r
314          */\r
315         public CurrencyDigits(int fractionDigits, int roundingIncrement) {\r
316             this.fractionDigits = (byte) fractionDigits;\r
317             this.roundingIncrement = (byte) roundingIncrement;\r
318         }\r
319 \r
320         /**\r
321          * Returns a string representing the currency digits, for debugging.\r
322          * @draft ICU 4.4\r
323          * @provisional This API might change or be removed in a future release.\r
324          */\r
325         @Override\r
326         public String toString() {\r
327             return debugString(this);\r
328         }\r
329     }\r
330 \r
331     /**\r
332      * Represents a complete currency info record listing the region, currency, from and to dates,\r
333      * and priority.\r
334      * @draft ICU 4.4\r
335      * @provisional This API might change or be removed in a future release.\r
336      */\r
337     public static final class CurrencyInfo {\r
338         /**\r
339          * Region code where currency is used.\r
340          * @draft ICU 4.4\r
341          * @provisional This API might change or be removed in a future release.\r
342          */\r
343         public final String region;\r
344 \r
345         /**\r
346          * The three-letter ISO currency code.\r
347          * @draft ICU 4.4\r
348          * @provisional This API might change or be removed in a future release.\r
349          */\r
350         public final String code;\r
351 \r
352         /**\r
353          * Date on which the currency was first officially used in the region.  If there is no\r
354          * date, this is Long.MIN_VALUE;\r
355          * @draft ICU 4.4\r
356          * @provisional This API might change or be removed in a future release.\r
357          */\r
358         public final long from;\r
359 \r
360         /**\r
361          * Date at which the currency stopped being officially used in the region.  If there is\r
362          * no date, this is Long.MAX_VALUE;\r
363          * @draft ICU 4.4\r
364          * @provisional This API might change or be removed in a future release.\r
365          */\r
366         public final long to;\r
367 \r
368         /**\r
369          * Preference order of currencies being used at the same time in the region.  Lower\r
370          * values are preferred (generally, this is a transition from an older to a newer\r
371          * currency).  Priorities within a single country are unique.\r
372          * @draft ICU 4.4\r
373          * @provisional This API might change or be removed in a future release.\r
374          */\r
375         public final short priority;\r
376 \r
377         /**\r
378          * Constructs a currency info.\r
379          * @param region region code\r
380          * @param code currency code\r
381          * @param from start date in milliseconds\r
382          * @param to end date in milliseconds\r
383          * @param priority priority value, 0 is highest priority, increasing values are lower\r
384          * @draft ICU 4.4\r
385          * @provisional This API might change or be removed in a future release.\r
386          */\r
387         public CurrencyInfo(String region, String code, long from, long to, int priority) {\r
388             this.region = region;\r
389             this.code = code;\r
390             this.from = from;\r
391             this.to = to;\r
392             this.priority = (short) priority;\r
393         }\r
394 \r
395         /**\r
396          * Returns a string useful for debugging.\r
397          * @draft ICU 4.4\r
398          * @provisional This API might change or be removed in a future release.\r
399          */\r
400         @Override\r
401         public String toString() {\r
402             return debugString(this);\r
403         }\r
404     }\r
405 \r
406     /**\r
407      * Returns the list of CurrencyInfos matching the provided filter.  Results\r
408      * are ordered by country code,  then by highest to lowest priority (0 is highest).\r
409      * The returned list is unmodifiable.\r
410      * @param filter the filter to control which currency info to return\r
411      * @return the matching information\r
412      * @draft ICU 4.4\r
413      * @provisional This API might change or be removed in a future release.\r
414      */\r
415     public List<CurrencyInfo> currencyInfo(CurrencyFilter filter) {\r
416         return Collections.emptyList();\r
417     }\r
418 \r
419     /**\r
420      * Returns the list of currency codes matching the provided filter.\r
421      * Results are ordered as in {@link #currencyInfo(CurrencyFilter)}.\r
422      * The returned list is unmodifiable.\r
423      * @param filter the filter to control which currencies to return.  If filter is null,\r
424      * returns all currencies for which information is available.\r
425      * @return the matching currency codes\r
426      * @draft ICU 4.4\r
427      * @provisional This API might change or be removed in a future release.\r
428      */\r
429     public List<String> currencies(CurrencyFilter filter) {\r
430         return Collections.emptyList();\r
431    }\r
432 \r
433     /**\r
434      * Returns the list of region codes matching the provided filter.\r
435      * Results are ordered as in {@link #currencyInfo(CurrencyFilter)}.\r
436      * The returned list is unmodifiable.\r
437      * @param filter the filter to control which regions to return.  If filter is null,\r
438      * returns all regions for which information is available.\r
439      * @return the matching region codes\r
440      * @draft ICU 4.4\r
441      * @provisional This API might change or be removed in a future release.\r
442      */\r
443     public List<String> regions(CurrencyFilter filter) {\r
444         return Collections.emptyList();\r
445     }\r
446 \r
447     /**\r
448      * Returns the CurrencyDigits for the currency code.\r
449      * @param isoCode the currency code\r
450      * @return the CurrencyDigits\r
451      * @draft ICU 4.4\r
452      * @provisional This API might change or be removed in a future release.\r
453      */\r
454     public CurrencyDigits currencyDigits(String isoCode) {\r
455         return defaultDigits;\r
456     }\r
457 \r
458     /**\r
459      * @internal\r
460      * @deprecated This API is ICU internal only.\r
461      */\r
462     protected static final CurrencyDigits defaultDigits = new CurrencyDigits(2, 0);\r
463 \r
464     static {\r
465         CurrencyMetaInfo temp = null;\r
466         boolean tempHasData = false;\r
467         try {\r
468             Class<?> clzz = Class.forName("com.ibm.icu.impl.ICUCurrencyMetaInfo");\r
469             temp = (CurrencyMetaInfo) clzz.newInstance();\r
470             tempHasData = true;\r
471         } catch (Throwable t) {\r
472             temp = new CurrencyMetaInfo();\r
473         }\r
474         impl = temp;\r
475         hasData = tempHasData;\r
476     }\r
477 \r
478     private static String dateString(long date) {\r
479         if (date == Long.MAX_VALUE || date == Long.MIN_VALUE) {\r
480             return null;\r
481         }\r
482         GregorianCalendar gc = new GregorianCalendar();\r
483         gc.setTimeInMillis(date);\r
484         return "" + gc.get(Calendar.YEAR) + '-' + (gc.get(Calendar.MONTH) + 1) + '-' +\r
485                 gc.get(Calendar.DAY_OF_MONTH);\r
486     }\r
487 \r
488     private static String debugString(Object o) {\r
489         StringBuilder sb = new StringBuilder();\r
490         try {\r
491             for (Field f : o.getClass().getFields()) {\r
492                 Object v = f.get(o);\r
493                 if (v != null) {\r
494                     String s;\r
495                     if (v instanceof Date) {\r
496                         s = dateString(((Date)v).getTime());\r
497                     } else if (v instanceof Long) {\r
498                         s = dateString(((Long)v).longValue());\r
499                     } else {\r
500                         s = String.valueOf(v);\r
501                     }\r
502                     if (s == null) {\r
503                         continue;\r
504                     }\r
505                     if (sb.length() > 0) {\r
506                         sb.append(",");\r
507                     }\r
508                     sb.append(f.getName())\r
509                         .append("='")\r
510                         .append(s)\r
511                         .append("'");\r
512                 }\r
513             }\r
514         } catch (Throwable t) {\r
515         }\r
516         sb.insert(0, o.getClass().getSimpleName() + "(");\r
517         sb.append(")");\r
518         return sb.toString();\r
519     }\r
520 }\r