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