2 *******************************************************************************
3 * Copyright (C) 2003-2011, 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.LocaleKey;
16 import com.ibm.icu.impl.ICULocaleService.LocaleKeyFactory;
17 import com.ibm.icu.impl.ICUResourceBundle;
18 import com.ibm.icu.impl.ICUService;
19 import com.ibm.icu.impl.ICUService.Factory;
20 import com.ibm.icu.impl.ICUService.Key;
21 import com.ibm.icu.text.NumberFormat.NumberFormatFactory;
22 import com.ibm.icu.util.Currency;
23 import com.ibm.icu.util.ULocale;
25 class NumberFormatServiceShim extends NumberFormat.NumberFormatShim {
27 Locale[] getAvailableLocales() {
28 if (service.isDefault()) {
29 return ICUResourceBundle.getAvailableLocales();
31 return service.getAvailableLocales();
34 ULocale[] getAvailableULocales() {
35 if (service.isDefault()) {
36 return ICUResourceBundle.getAvailableULocales();
38 return service.getAvailableULocales();
41 private static final class NFFactory extends LocaleKeyFactory {
42 private NumberFormatFactory delegate;
44 NFFactory(NumberFormatFactory delegate) {
45 super(delegate.visible() ? VISIBLE : INVISIBLE);
47 this.delegate = delegate;
50 public Object create(Key key, ICUService srvc) {
51 if (!handlesKey(key) || !(key instanceof LocaleKey)) {
55 LocaleKey lkey = (LocaleKey)key;
56 Object result = delegate.createFormat(lkey.canonicalLocale(), lkey.kind());
58 result = srvc.getKey(key, null, this);
63 protected Set<String> getSupportedIDs() {
64 return delegate.getSupportedLocaleNames();
68 Object registerFactory(NumberFormatFactory factory) {
69 return service.registerFactory(new NFFactory(factory));
72 boolean unregister(Object registryKey) {
73 return service.unregisterFactory((Factory)registryKey);
76 NumberFormat createInstance(ULocale desiredLocale, int choice) {
79 // if (service.isDefault()) {
80 // return NumberFormat.createInstance(desiredLocale, choice);
83 ULocale[] actualLoc = new ULocale[1];
84 NumberFormat fmt = (NumberFormat)service.get(desiredLocale, choice,
87 throw new MissingResourceException("Unable to construct NumberFormat", "", "");
89 fmt = (NumberFormat)fmt.clone();
91 // If we are creating a currency type formatter, then we may have to set the currency
92 // explicitly, since the actualLoc may be different than the desiredLocale
93 if ( choice == NumberFormat.CURRENCYSTYLE ||
94 choice == NumberFormat.ISOCURRENCYSTYLE ||
95 choice == NumberFormat.PLURALCURRENCYSTYLE) {
96 fmt.setCurrency(Currency.getInstance(desiredLocale));
99 ULocale uloc = actualLoc[0];
100 fmt.setLocale(uloc, uloc); // services make no distinction between actual & valid
104 private static class NFService extends ICULocaleService {
106 super("NumberFormat");
108 class RBNumberFormatFactory extends ICUResourceBundleFactory {
109 protected Object handleCreate(ULocale loc, int kind, ICUService srvc) {
110 return NumberFormat.createInstance(loc, kind);
114 this.registerFactory(new RBNumberFormatFactory());
118 private static ICULocaleService service = new NFService();