]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-52_1/main/classes/collate/src/com/ibm/icu/text/CollatorServiceShim.java
Upgrade ICU4J.
[Dictionary.git] / jars / icu4j-52_1 / main / classes / collate / src / com / ibm / icu / text / CollatorServiceShim.java
1 /**
2 *******************************************************************************
3 * Copyright (C) 2003-2010, 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;
11 import java.util.MissingResourceException;
12 import java.util.Set;
13
14 import com.ibm.icu.impl.ICULocaleService;
15 import com.ibm.icu.impl.ICULocaleService.LocaleKeyFactory;
16 import com.ibm.icu.impl.ICUResourceBundle;
17 import com.ibm.icu.impl.ICUService;
18 import com.ibm.icu.impl.ICUService.Factory;
19 import com.ibm.icu.text.Collator.CollatorFactory;
20 import com.ibm.icu.util.ULocale;
21
22 final class CollatorServiceShim extends Collator.ServiceShim {
23
24     Collator getInstance(ULocale locale) {
25     // use service cache, it's faster than instantiation
26 //          if (service.isDefault()) {
27 //              return new RuleBasedCollator(locale);
28 //          }
29         try {
30             ULocale[] actualLoc = new ULocale[1];
31             Collator coll = (Collator)service.get(locale, actualLoc);
32             if (coll == null) {
33                 ///CLOVER:OFF
34                 //Can't really change coll after it's been initialized
35                 throw new MissingResourceException("Could not locate Collator data", "", "");
36                 ///CLOVER:ON
37             }
38             coll = (Collator) coll.clone();
39             coll.setLocale(actualLoc[0], actualLoc[0]); // services make no distinction between actual & valid
40             return coll;
41         }
42         catch (CloneNotSupportedException e) {
43         ///CLOVER:OFF
44             throw new IllegalStateException(e.getMessage());
45         ///CLOVER:ON
46         }
47     }
48
49     Object registerInstance(Collator collator, ULocale locale) {
50         return service.registerObject(collator, locale);
51     }
52
53     Object registerFactory(CollatorFactory f) {
54         class CFactory extends LocaleKeyFactory {
55             CollatorFactory delegate;
56
57             CFactory(CollatorFactory fctry) {
58                 super(fctry.visible());
59                 this.delegate = fctry;
60             }
61
62             public Object handleCreate(ULocale loc, int kind, ICUService srvc) {
63                 Object coll = delegate.createCollator(loc);
64                 return coll;
65             }
66
67             public String getDisplayName(String id, ULocale displayLocale) {
68                 ULocale objectLocale = new ULocale(id);
69                 return delegate.getDisplayName(objectLocale, displayLocale);
70             }
71
72             public Set<String> getSupportedIDs() {
73                 return delegate.getSupportedLocaleIDs();
74             }
75         }
76
77         return service.registerFactory(new CFactory(f));
78     }
79
80     boolean unregister(Object registryKey) {
81         return service.unregisterFactory((Factory)registryKey);
82     }
83
84     Locale[] getAvailableLocales() {
85         // TODO rewrite this to just wrap getAvailableULocales later
86         Locale[] result;
87         if (service.isDefault()) {
88             result = ICUResourceBundle.getAvailableLocales(ICUResourceBundle.ICU_COLLATION_BASE_NAME,
89                     ICUResourceBundle.ICU_DATA_CLASS_LOADER);
90         } else {
91             result = service.getAvailableLocales();
92         }
93         return result;
94     }
95
96     ULocale[] getAvailableULocales() {
97         ULocale[] result;
98         if (service.isDefault()) {
99             result = ICUResourceBundle.getAvailableULocales(ICUResourceBundle.ICU_COLLATION_BASE_NAME,
100                     ICUResourceBundle.ICU_DATA_CLASS_LOADER);
101         } else {
102             result = service.getAvailableULocales();
103         }
104         return result;
105     }
106
107     String getDisplayName(ULocale objectLocale, ULocale displayLocale) {
108         String id = objectLocale.getName();
109         return service.getDisplayName(id, displayLocale);
110     }
111
112     private static class CService extends ICULocaleService {
113         CService() {
114             super("Collator");
115
116             class CollatorFactory extends ICUResourceBundleFactory {
117                 CollatorFactory() {
118                     super(ICUResourceBundle.ICU_COLLATION_BASE_NAME);
119                 }
120
121                 protected Object handleCreate(ULocale uloc, int kind, ICUService srvc) {
122                     return new RuleBasedCollator(uloc);
123                 }
124             }
125
126             this.registerFactory(new CollatorFactory());
127             markDefault();
128         }
129         ///CLOVER:OFF
130         // The following method can not be reached by testing
131         protected Object handleDefault(Key key, String[] actualIDReturn) {
132             if (actualIDReturn != null) {
133                 actualIDReturn[0] = "root";
134             }
135             try {
136                 return new RuleBasedCollator(ULocale.ROOT);
137             }
138             catch (MissingResourceException e) {
139                 return null;
140             }
141         }
142         ///CLOVER:ON
143     }
144     private static ICULocaleService service = new CService();
145 }