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