1/* 2 ******************************************************************************* 3 * Copyright (C) 2003-2011, International Business Machines Corporation and * 4 * others. All Rights Reserved. * 5 ******************************************************************************* 6 */ 7 8package com.ibm.icu.text; 9 10import java.util.Locale; 11import java.util.MissingResourceException; 12import java.util.Set; 13 14import com.ibm.icu.impl.ICULocaleService; 15import com.ibm.icu.impl.ICULocaleService.LocaleKey; 16import com.ibm.icu.impl.ICULocaleService.LocaleKeyFactory; 17import com.ibm.icu.impl.ICUResourceBundle; 18import com.ibm.icu.impl.ICUService; 19import com.ibm.icu.impl.ICUService.Factory; 20import com.ibm.icu.impl.ICUService.Key; 21import com.ibm.icu.text.NumberFormat.NumberFormatFactory; 22import com.ibm.icu.util.Currency; 23import com.ibm.icu.util.ULocale; 24 25class NumberFormatServiceShim extends NumberFormat.NumberFormatShim { 26 27 Locale[] getAvailableLocales() { 28 if (service.isDefault()) { 29 return ICUResourceBundle.getAvailableLocales(); 30 } 31 return service.getAvailableLocales(); 32 } 33 34 ULocale[] getAvailableULocales() { 35 if (service.isDefault()) { 36 return ICUResourceBundle.getAvailableULocales(); 37 } 38 return service.getAvailableULocales(); 39 } 40 41 private static final class NFFactory extends LocaleKeyFactory { 42 private NumberFormatFactory delegate; 43 44 NFFactory(NumberFormatFactory delegate) { 45 super(delegate.visible() ? VISIBLE : INVISIBLE); 46 47 this.delegate = delegate; 48 } 49 50 public Object create(Key key, ICUService srvc) { 51 if (!handlesKey(key) || !(key instanceof LocaleKey)) { 52 return null; 53 } 54 55 LocaleKey lkey = (LocaleKey)key; 56 Object result = delegate.createFormat(lkey.canonicalLocale(), lkey.kind()); 57 if (result == null) { 58 result = srvc.getKey(key, null, this); 59 } 60 return result; 61 } 62 63 protected Set<String> getSupportedIDs() { 64 return delegate.getSupportedLocaleNames(); 65 } 66 } 67 68 Object registerFactory(NumberFormatFactory factory) { 69 return service.registerFactory(new NFFactory(factory)); 70 } 71 72 boolean unregister(Object registryKey) { 73 return service.unregisterFactory((Factory)registryKey); 74 } 75 76 NumberFormat createInstance(ULocale desiredLocale, int choice) { 77 78 // use service cache 79// if (service.isDefault()) { 80// return NumberFormat.createInstance(desiredLocale, choice); 81// } 82 83 ULocale[] actualLoc = new ULocale[1]; 84 NumberFormat fmt = (NumberFormat)service.get(desiredLocale, choice, 85 actualLoc); 86 if (fmt == null) { 87 throw new MissingResourceException("Unable to construct NumberFormat", "", ""); 88 } 89 fmt = (NumberFormat)fmt.clone(); 90 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)); 97 } 98 99 ULocale uloc = actualLoc[0]; 100 fmt.setLocale(uloc, uloc); // services make no distinction between actual & valid 101 return fmt; 102 } 103 104 private static class NFService extends ICULocaleService { 105 NFService() { 106 super("NumberFormat"); 107 108 class RBNumberFormatFactory extends ICUResourceBundleFactory { 109 protected Object handleCreate(ULocale loc, int kind, ICUService srvc) { 110 return NumberFormat.createInstance(loc, kind); 111 } 112 } 113 114 this.registerFactory(new RBNumberFormatFactory()); 115 markDefault(); 116 } 117 } 118 private static ICULocaleService service = new NFService(); 119} 120