KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibm > icu > text > NumberFormatServiceShim


1 /*
2  *******************************************************************************
3  * Copyright (C) 2003-2006, International Business Machines Corporation and *
4  * others. All Rights Reserved. *
5  *******************************************************************************
6  */

7
8 package com.ibm.icu.text;
9
10 import java.util.Locale JavaDoc;
11 import java.util.Set JavaDoc;
12 import java.util.MissingResourceException JavaDoc;
13
14 //import com.ibm.icu.impl.ICULocaleData;
15
import com.ibm.icu.impl.ICUResourceBundle;
16 import com.ibm.icu.impl.ICUService;
17 import com.ibm.icu.impl.ICUService.Factory;
18 import com.ibm.icu.impl.ICUService.Key;
19 import com.ibm.icu.impl.ICULocaleService;
20 import com.ibm.icu.impl.ICULocaleService.LocaleKey;
21 import com.ibm.icu.impl.ICULocaleService.LocaleKeyFactory;
22 import com.ibm.icu.text.NumberFormat;
23 import com.ibm.icu.text.NumberFormat.NumberFormatFactory;
24 import com.ibm.icu.util.ULocale;
25 import com.ibm.icu.util.UResourceBundle;
26
27 class NumberFormatServiceShim extends NumberFormat.NumberFormatShim {
28
29     Locale JavaDoc[] getAvailableLocales() {
30         if (service.isDefault()) {
31             return ICUResourceBundle.getAvailableLocales(ICUResourceBundle.ICU_BASE_NAME);
32         }
33         return service.getAvailableLocales();
34     }
35
36     ULocale[] getAvailableULocales() {
37         if (service.isDefault()) {
38             return ICUResourceBundle.getAvailableULocales(ICUResourceBundle.ICU_BASE_NAME);
39         }
40         return service.getAvailableULocales();
41     }
42
43     private static final class NFFactory extends LocaleKeyFactory {
44         private NumberFormatFactory delegate;
45
46         NFFactory(NumberFormatFactory delegate) {
47             super(delegate.visible() ? VISIBLE : INVISIBLE);
48
49             this.delegate = delegate;
50         }
51
52         public Object JavaDoc create(Key key, ICUService service) {
53             if (handlesKey(key)) {
54                 LocaleKey lkey = (LocaleKey)key;
55                 ULocale loc = lkey.canonicalLocale();
56                 int kind = lkey.kind();
57
58                 Object JavaDoc result = delegate.createFormat(loc, kind);
59                 if (result == null) {
60                     result = service.getKey(key, null, this);
61                 }
62                 return result;
63             }
64             return null;
65         }
66
67         protected Set JavaDoc getSupportedIDs() {
68             return delegate.getSupportedLocaleNames();
69         }
70     }
71
72     Object JavaDoc registerFactory(NumberFormatFactory factory) {
73         return service.registerFactory(new NFFactory(factory));
74     }
75
76     boolean unregister(Object JavaDoc registryKey) {
77         return service.unregisterFactory((Factory)registryKey);
78     }
79
80     NumberFormat createInstance(ULocale desiredLocale, int choice) {
81
82     // use service cache
83
// if (service.isDefault()) {
84
// return NumberFormat.createInstance(desiredLocale, choice);
85
// }
86

87         ULocale[] actualLoc = new ULocale[1];
88         if (desiredLocale.equals(ULocale.ROOT)) {
89             desiredLocale = ULocale.ROOT;
90         }
91         NumberFormat fmt = (NumberFormat)service.get(desiredLocale, choice,
92                                                      actualLoc);
93         if (fmt == null) {
94             throw new MissingResourceException JavaDoc("Unable to construct NumberFormat", "", "");
95         }
96         fmt = (NumberFormat)fmt.clone();
97
98         ULocale uloc = actualLoc[0];
99         fmt.setLocale(uloc, uloc); // services make no distinction between actual & valid
100
return fmt;
101     }
102
103     private static class NFService extends ICULocaleService {
104         NFService() {
105             super("NumberFormat");
106
107             class RBNumberFormatFactory extends ICUResourceBundleFactory {
108                 protected Object JavaDoc handleCreate(ULocale loc, int kind, ICUService service) {
109                     return NumberFormat.createInstance(loc, kind);
110                 }
111             }
112                 
113             this.registerFactory(new RBNumberFormatFactory());
114             markDefault();
115         }
116     }
117     private static ICULocaleService service = new NFService();
118 }
119
Popular Tags