1 7 8 package com.ibm.icu.text; 9 10 import java.util.Locale ; 11 import java.util.Set ; 12 import java.util.MissingResourceException ; 13 14 import com.ibm.icu.impl.ICUResourceBundle; 16 import com.ibm.icu.impl.ICUService; 17 import com.ibm.icu.impl.ICUService.Factory; 18 import com.ibm.icu.impl.ICUService.Key; 19 import com.ibm.icu.impl.ICULocaleService; 20 import com.ibm.icu.impl.ICULocaleService.LocaleKey; 21 import com.ibm.icu.impl.ICULocaleService.LocaleKeyFactory; 22 import com.ibm.icu.text.NumberFormat; 23 import com.ibm.icu.text.NumberFormat.NumberFormatFactory; 24 import com.ibm.icu.util.ULocale; 25 import com.ibm.icu.util.UResourceBundle; 26 27 class NumberFormatServiceShim extends NumberFormat.NumberFormatShim { 28 29 Locale [] getAvailableLocales() { 30 if (service.isDefault()) { 31 return ICUResourceBundle.getAvailableLocales(ICUResourceBundle.ICU_BASE_NAME); 32 } 33 return service.getAvailableLocales(); 34 } 35 36 ULocale[] getAvailableULocales() { 37 if (service.isDefault()) { 38 return ICUResourceBundle.getAvailableULocales(ICUResourceBundle.ICU_BASE_NAME); 39 } 40 return service.getAvailableULocales(); 41 } 42 43 private static final class NFFactory extends LocaleKeyFactory { 44 private NumberFormatFactory delegate; 45 46 NFFactory(NumberFormatFactory delegate) { 47 super(delegate.visible() ? VISIBLE : INVISIBLE); 48 49 this.delegate = delegate; 50 } 51 52 public Object create(Key key, ICUService service) { 53 if (handlesKey(key)) { 54 LocaleKey lkey = (LocaleKey)key; 55 ULocale loc = lkey.canonicalLocale(); 56 int kind = lkey.kind(); 57 58 Object result = delegate.createFormat(loc, kind); 59 if (result == null) { 60 result = service.getKey(key, null, this); 61 } 62 return result; 63 } 64 return null; 65 } 66 67 protected Set getSupportedIDs() { 68 return delegate.getSupportedLocaleNames(); 69 } 70 } 71 72 Object registerFactory(NumberFormatFactory factory) { 73 return service.registerFactory(new NFFactory(factory)); 74 } 75 76 boolean unregister(Object registryKey) { 77 return service.unregisterFactory((Factory)registryKey); 78 } 79 80 NumberFormat createInstance(ULocale desiredLocale, int choice) { 81 82 87 ULocale[] actualLoc = new ULocale[1]; 88 if (desiredLocale.equals(ULocale.ROOT)) { 89 desiredLocale = ULocale.ROOT; 90 } 91 NumberFormat fmt = (NumberFormat)service.get(desiredLocale, choice, 92 actualLoc); 93 if (fmt == null) { 94 throw new MissingResourceException ("Unable to construct NumberFormat", "", ""); 95 } 96 fmt = (NumberFormat)fmt.clone(); 97 98 ULocale uloc = actualLoc[0]; 99 fmt.setLocale(uloc, uloc); return fmt; 101 } 102 103 private static class NFService extends ICULocaleService { 104 NFService() { 105 super("NumberFormat"); 106 107 class RBNumberFormatFactory extends ICUResourceBundleFactory { 108 protected Object handleCreate(ULocale loc, int kind, ICUService service) { 109 return NumberFormat.createInstance(loc, kind); 110 } 111 } 112 113 this.registerFactory(new RBNumberFormatFactory()); 114 markDefault(); 115 } 116 } 117 private static ICULocaleService service = new NFService(); 118 } 119 | Popular Tags |