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) 2001-2010, International Business Machines Corporation and * 7 * others. All Rights Reserved. * 8 ******************************************************************************* 9 */ 10 package android.icu.dev.test.util; 11 12 import java.text.Collator; 13 import java.util.EventListener; 14 import java.util.Iterator; 15 import java.util.Map; 16 import java.util.Map.Entry; 17 import java.util.Set; 18 import java.util.SortedMap; 19 20 import android.icu.impl.ICULocaleService; 21 import android.icu.impl.ICUService; 22 import android.icu.util.ULocale; 23 import android.icu.testsharding.MainTestShard; 24 25 @MainTestShard 26 public class ICUServiceTestSample { 27 static public void main(String[] args) { 28 new HelloServiceClient(); 29 30 Thread t = new HelloUpdateThread(); 31 t.start(); 32 try { 33 t.join(); 34 } 35 catch (InterruptedException e) { 36 } 37 System.out.println("done"); 38 } 39 40 /** 41 * A class that displays the current names in the Hello service. 42 * Each time the service changes, it redisplays the names. 43 */ 44 static class HelloServiceClient implements HelloService.HelloServiceListener { 45 46 HelloServiceClient() { 47 HelloService.addListener(this); 48 display(); 49 } 50 51 52 /** 53 * This will be called in the notification thread of 54 * ICUNotifier. ICUNotifier could spawn a (non-daemon) thread 55 * for each listener, so that impolite listeners wouldn't hold 56 * up notification, but right now it doesn't. Instead, all 57 * notifications are delivered on the notification thread. 58 * Since that's a daemon thread, a notification might not 59 * complete before main terminates. 60 */ 61 public void helloServiceChanged() { 62 display(); 63 } 64 65 private void display() { 66 Map names = HelloService.getDisplayNames(ULocale.US); 67 System.out.println("displaying " + names.size() + " names."); 68 Iterator iter = names.entrySet().iterator(); 69 while (iter.hasNext()) { 70 Entry entry = (Entry)iter.next(); 71 String displayName = (String)entry.getKey(); 72 HelloService service = HelloService.get((String)entry.getValue()); 73 System.out.println(displayName + " says " + service.hello()); 74 try { 75 Thread.sleep(50); 76 } 77 catch (InterruptedException e) { 78 } 79 } 80 System.out.println("----"); 81 } 82 } 83 84 /** 85 * A thread to update the service. 86 */ 87 static class HelloUpdateThread extends Thread { 88 String[][] updates = { 89 { "Hey", "en_US_INFORMAL" }, 90 { "Hallo", "de_DE_INFORMAL" }, 91 { "Yo!", "en_US_CALIFORNIA_INFORMAL" }, 92 { "Chi Fanle Ma?", "zh__INFORMAL" }, 93 { "Munch munch... Burger?", "en" }, 94 { "Sniff", "fr" }, 95 { "TongZhi! MaoZeDong SiXiang Wan Sui!", "zh_CN" }, 96 { "Bier? Ja!", "de" }, 97 }; 98 public void run() { 99 for (int i = 0; i < updates.length; ++i) { 100 try { 101 Thread.sleep(500); 102 } 103 catch (InterruptedException e) { 104 } 105 HelloService.register(updates[i][0], new ULocale(updates[i][1])); 106 } 107 } 108 } 109 110 /** 111 * An example service that wraps an ICU service in order to export custom API and 112 * notification. The service just implements 'hello'. 113 */ 114 static final class HelloService { 115 private static ICUService registry; 116 private String name; 117 118 private HelloService(String name) { 119 this.name = name; 120 } 121 122 /** 123 * The hello service... 124 */ 125 public String hello() { 126 return name; 127 } 128 129 public String toString() { 130 return super.toString() + ": " + name; 131 } 132 133 /** 134 * Deferred init. 135 */ 136 private static ICUService registry() { 137 if (registry == null) { 138 initRegistry(); 139 } 140 return registry; 141 } 142 143 private static void initRegistry() { 144 registry = new ICULocaleService() { 145 protected boolean acceptsListener(EventListener l) { 146 return true; // we already verify in our wrapper APIs 147 } 148 protected void notifyListener(EventListener l) { 149 ((HelloServiceListener)l).helloServiceChanged(); 150 } 151 }; 152 153 // initialize 154 doRegister("Hello", "en"); 155 doRegister("Bonjour", "fr"); 156 doRegister("Ni Hao", "zh_CN"); 157 doRegister("Guten Tag", "de"); 158 } 159 160 /** 161 * A custom listener for changes to this service. We don't need to 162 * point to the service since it is defined by this class and not 163 * an object. 164 */ 165 public static interface HelloServiceListener extends EventListener { 166 public void helloServiceChanged(); 167 } 168 169 /** 170 * Type-safe notification for this service. 171 */ 172 public static void addListener(HelloServiceListener l) { 173 registry().addListener(l); 174 } 175 176 /** 177 * Type-safe notification for this service. 178 */ 179 public static void removeListener(HelloServiceListener l) { 180 registry().removeListener(l); 181 } 182 183 /** 184 * Type-safe access to the service. 185 */ 186 public static HelloService get(String id) { 187 return (HelloService)registry().get(id); 188 } 189 190 public static Set getVisibleIDs() { 191 return registry().getVisibleIDs(); 192 } 193 194 public static Map getDisplayNames(ULocale locale) { 195 return getDisplayNames(registry(), locale); 196 } 197 198 /** 199 * Register a new hello string for this locale. 200 */ 201 public static void register(String helloString, ULocale locale) { 202 if (helloString == null || locale == null) { 203 throw new NullPointerException(); 204 } 205 doRegister(helloString, locale.toString()); 206 } 207 208 private static void doRegister(String hello, String id) { 209 registry().registerObject(new HelloService(hello), id); 210 } 211 /** 212 * Convenience override of getDisplayNames(ULocale, Comparator, String) that 213 * uses the default collator for the locale as the comparator to 214 * sort the display names, and null for the matchID. 215 */ 216 public static SortedMap getDisplayNames(ICUService service, ULocale locale) { 217 Collator col = Collator.getInstance(locale.toLocale()); 218 return service.getDisplayNames(locale, col, null); 219 } 220 } 221 } 222