2 *******************************************************************************
3 * Copyright (C) 2003-2010, International Business Machines Corporation and *
4 * others. All Rights Reserved. *
5 *******************************************************************************
8 package com.ibm.icu.text;
10 import java.util.Locale;
11 import java.util.MissingResourceException;
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;
22 final class CollatorServiceShim extends Collator.ServiceShim {
24 Collator getInstance(ULocale locale) {
25 // use service cache, it's faster than instantiation
26 // if (service.isDefault()) {
27 // return new RuleBasedCollator(locale);
30 ULocale[] actualLoc = new ULocale[1];
31 Collator coll = (Collator)service.get(locale, actualLoc);
34 //Can't really change coll after it's been initialized
35 throw new MissingResourceException("Could not locate Collator data", "", "");
38 coll = (Collator) coll.clone();
39 coll.setLocale(actualLoc[0], actualLoc[0]); // services make no distinction between actual & valid
42 catch (CloneNotSupportedException e) {
44 throw new IllegalStateException(e.getMessage());
49 Object registerInstance(Collator collator, ULocale locale) {
50 return service.registerObject(collator, locale);
53 Object registerFactory(CollatorFactory f) {
54 class CFactory extends LocaleKeyFactory {
55 CollatorFactory delegate;
57 CFactory(CollatorFactory fctry) {
58 super(fctry.visible());
59 this.delegate = fctry;
62 public Object handleCreate(ULocale loc, int kind, ICUService srvc) {
63 Object coll = delegate.createCollator(loc);
67 public String getDisplayName(String id, ULocale displayLocale) {
68 ULocale objectLocale = new ULocale(id);
69 return delegate.getDisplayName(objectLocale, displayLocale);
72 public Set<String> getSupportedIDs() {
73 return delegate.getSupportedLocaleIDs();
77 return service.registerFactory(new CFactory(f));
80 boolean unregister(Object registryKey) {
81 return service.unregisterFactory((Factory)registryKey);
84 Locale[] getAvailableLocales() {
85 // TODO rewrite this to just wrap getAvailableULocales later
87 if (service.isDefault()) {
88 result = ICUResourceBundle.getAvailableLocales(ICUResourceBundle.ICU_COLLATION_BASE_NAME,
89 ICUResourceBundle.ICU_DATA_CLASS_LOADER);
91 result = service.getAvailableLocales();
96 ULocale[] getAvailableULocales() {
98 if (service.isDefault()) {
99 result = ICUResourceBundle.getAvailableULocales(ICUResourceBundle.ICU_COLLATION_BASE_NAME,
100 ICUResourceBundle.ICU_DATA_CLASS_LOADER);
102 result = service.getAvailableULocales();
107 String getDisplayName(ULocale objectLocale, ULocale displayLocale) {
108 String id = objectLocale.getName();
109 return service.getDisplayName(id, displayLocale);
112 private static class CService extends ICULocaleService {
116 class CollatorFactory extends ICUResourceBundleFactory {
118 super(ICUResourceBundle.ICU_COLLATION_BASE_NAME);
121 protected Object handleCreate(ULocale uloc, int kind, ICUService srvc) {
122 return new RuleBasedCollator(uloc);
126 this.registerFactory(new CollatorFactory());
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";
136 return new RuleBasedCollator(ULocale.ROOT);
138 catch (MissingResourceException e) {
144 private static ICULocaleService service = new CService();