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