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
11package android.icu.text;
12
13import java.util.Locale;
14import java.util.MissingResourceException;
15import java.util.Set;
16
17import android.icu.impl.ICUData;
18import android.icu.impl.ICULocaleService;
19import android.icu.impl.ICULocaleService.LocaleKeyFactory;
20import android.icu.impl.ICUResourceBundle;
21import android.icu.impl.ICUService;
22import android.icu.impl.ICUService.Factory;
23import android.icu.impl.coll.CollationLoader;
24import android.icu.impl.coll.CollationTailoring;
25import android.icu.text.Collator.CollatorFactory;
26import android.icu.util.ICUCloneNotSupportedException;
27import android.icu.util.Output;
28import android.icu.util.ULocale;
29
30final class CollatorServiceShim extends Collator.ServiceShim {
31
32    @Override
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
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
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
97    boolean unregister(Object registryKey) {
98        return service.unregisterFactory((Factory)registryKey);
99    }
100
101    @Override
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
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
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 {
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
160        public String validateFallbackLocale() {
161            return "";
162        }
163
164        ///CLOVER:OFF
165        // The following method can not be reached by testing
166        @Override
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().
182    private static final Collator makeInstance(ULocale desiredLocale) {
183        Output<ULocale> validLocale = new Output<ULocale>(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