• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* GENERATED SOURCE. DO NOT MODIFY. */
2 // © 2016 and later: Unicode, Inc. and others.
3 // License & terms of use: http://www.unicode.org/copyright.html#License
4 /**
5 *******************************************************************************
6 * Copyright (C) 2003-2016, International Business Machines Corporation and
7 * others. All Rights Reserved.
8 *******************************************************************************
9 */
10 
11 package ohos.global.icu.text;
12 
13 import java.util.Locale;
14 import java.util.MissingResourceException;
15 import java.util.Set;
16 
17 import ohos.global.icu.impl.ICUData;
18 import ohos.global.icu.impl.ICULocaleService;
19 import ohos.global.icu.impl.ICULocaleService.LocaleKeyFactory;
20 import ohos.global.icu.impl.ICUResourceBundle;
21 import ohos.global.icu.impl.ICUService;
22 import ohos.global.icu.impl.ICUService.Factory;
23 import ohos.global.icu.impl.coll.CollationLoader;
24 import ohos.global.icu.impl.coll.CollationTailoring;
25 import ohos.global.icu.text.Collator.CollatorFactory;
26 import ohos.global.icu.util.ICUCloneNotSupportedException;
27 import ohos.global.icu.util.Output;
28 import ohos.global.icu.util.ULocale;
29 
30 final class CollatorServiceShim extends Collator.ServiceShim {
31 
32     @Override
getInstance(ULocale locale)33     Collator getInstance(ULocale locale) {
34     // use service cache, it's faster than instantiation
35 //          if (service.isDefault()) {
36 //              return new RuleBasedCollator(locale);
37 //          }
38         try {
39             ULocale[] actualLoc = new ULocale[1];
40             Collator coll = (Collator)service.get(locale, actualLoc);
41             if (coll == null) {
42                 ///CLOVER:OFF
43                 //Can't really change coll after it's been initialized
44                 throw new MissingResourceException("Could not locate Collator data", "", "");
45                 ///CLOVER:ON
46             }
47             return (Collator) coll.clone();
48         }
49         catch (CloneNotSupportedException e) {
50         ///CLOVER:OFF
51             throw new ICUCloneNotSupportedException(e);
52         ///CLOVER:ON
53         }
54     }
55 
56     @Override
registerInstance(Collator collator, ULocale locale)57     Object registerInstance(Collator collator, ULocale locale) {
58         // Set the collator locales while registering so that getInstance()
59         // need not guess whether the collator's locales are already set properly
60         // (as they are by the data loader).
61         collator.setLocale(locale, locale);
62         return service.registerObject(collator, locale);
63     }
64 
65     @Override
registerFactory(CollatorFactory f)66     Object registerFactory(CollatorFactory f) {
67         class CFactory extends LocaleKeyFactory {
68             CollatorFactory delegate;
69 
70             CFactory(CollatorFactory fctry) {
71                 super(fctry.visible());
72                 this.delegate = fctry;
73             }
74 
75             @Override
76             public Object handleCreate(ULocale loc, int kind, ICUService srvc) {
77                 Object coll = delegate.createCollator(loc);
78                 return coll;
79             }
80 
81             @Override
82             public String getDisplayName(String id, ULocale displayLocale) {
83                 ULocale objectLocale = new ULocale(id);
84                 return delegate.getDisplayName(objectLocale, displayLocale);
85             }
86 
87             @Override
88             public Set<String> getSupportedIDs() {
89                 return delegate.getSupportedLocaleIDs();
90             }
91         }
92 
93         return service.registerFactory(new CFactory(f));
94     }
95 
96     @Override
unregister(Object registryKey)97     boolean unregister(Object registryKey) {
98         return service.unregisterFactory((Factory)registryKey);
99     }
100 
101     @Override
getAvailableLocales()102     Locale[] getAvailableLocales() {
103         // TODO rewrite this to just wrap getAvailableULocales later
104         Locale[] result;
105         if (service.isDefault()) {
106             result = ICUResourceBundle.getAvailableLocales(ICUData.ICU_COLLATION_BASE_NAME,
107                     ICUResourceBundle.ICU_DATA_CLASS_LOADER);
108         } else {
109             result = service.getAvailableLocales();
110         }
111         return result;
112     }
113 
114     @Override
getAvailableULocales()115     ULocale[] getAvailableULocales() {
116         ULocale[] result;
117         if (service.isDefault()) {
118             result = ICUResourceBundle.getAvailableULocales(ICUData.ICU_COLLATION_BASE_NAME,
119                     ICUResourceBundle.ICU_DATA_CLASS_LOADER);
120         } else {
121             result = service.getAvailableULocales();
122         }
123         return result;
124     }
125 
126     @Override
getDisplayName(ULocale objectLocale, ULocale displayLocale)127     String getDisplayName(ULocale objectLocale, ULocale displayLocale) {
128         String id = objectLocale.getName();
129         return service.getDisplayName(id, displayLocale);
130     }
131 
132     private static class CService extends ICULocaleService {
CService()133         CService() {
134             super("Collator");
135 
136             class CollatorFactory extends ICUResourceBundleFactory {
137                 CollatorFactory() {
138                     super(ICUData.ICU_COLLATION_BASE_NAME);
139                 }
140 
141                 @Override
142                 protected Object handleCreate(ULocale uloc, int kind, ICUService srvc) {
143                     return makeInstance(uloc);
144                 }
145             }
146 
147             this.registerFactory(new CollatorFactory());
148             markDefault();
149         }
150 
151         /**
152          * makeInstance() returns an appropriate Collator for any locale.
153          * It falls back to root if there is no specific data.
154          *
155          * <p>Without this override, the service code would fall back to the default locale
156          * which is not desirable for an algorithm with a good Unicode default,
157          * like collation.
158          */
159         @Override
validateFallbackLocale()160         public String validateFallbackLocale() {
161             return "";
162         }
163 
164         ///CLOVER:OFF
165         // The following method can not be reached by testing
166         @Override
handleDefault(Key key, String[] actualIDReturn)167         protected Object handleDefault(Key key, String[] actualIDReturn) {
168             if (actualIDReturn != null) {
169                 actualIDReturn[0] = "root";
170             }
171             try {
172                 return makeInstance(ULocale.ROOT);
173             }
174             catch (MissingResourceException e) {
175                 return null;
176             }
177         }
178         ///CLOVER:ON
179     }
180 
181     // Ported from C++ Collator::makeInstance().
makeInstance(ULocale desiredLocale)182     private static final Collator makeInstance(ULocale desiredLocale) {
183         Output<ULocale> validLocale = new Output<>(ULocale.ROOT);
184         CollationTailoring t =
185             CollationLoader.loadTailoring(desiredLocale, validLocale);
186         return new RuleBasedCollator(t, validLocale.value);
187     }
188 
189     private static ICULocaleService service = new CService();
190 }
191