001: /**
002: *******************************************************************************
003: * Copyright (C) 2001-2006, International Business Machines Corporation and *
004: * others. All Rights Reserved. *
005: *******************************************************************************
006: */package com.ibm.icu.dev.test.util;
007:
008: import com.ibm.icu.impl.ICULocaleService;
009: import com.ibm.icu.impl.ICUService;
010: import com.ibm.icu.text.Collator;
011: import com.ibm.icu.util.ULocale;
012:
013: import java.util.EventListener;
014: import java.util.Iterator;
015: import java.util.Map;
016: import java.util.Map.Entry;
017: import java.util.Set;
018: import java.util.SortedMap;
019:
020: public class ICUServiceTestSample {
021: static public void main(String[] args) {
022: HelloServiceClient client = new HelloServiceClient();
023:
024: Thread t = new HelloUpdateThread();
025: t.start();
026: try {
027: t.join();
028: } catch (InterruptedException e) {
029: }
030: System.out.println("done");
031: if (client == null) {
032: }
033: }
034:
035: /**
036: * A class that displays the current names in the Hello service.
037: * Each time the service changes, it redisplays the names.
038: */
039: static class HelloServiceClient implements
040: HelloService.HelloServiceListener {
041:
042: HelloServiceClient() {
043: HelloService.addListener(this );
044: display();
045: }
046:
047: /**
048: * This will be called in the notification thread of
049: * ICUNotifier. ICUNotifier could spawn a (non-daemon) thread
050: * for each listener, so that impolite listeners wouldn't hold
051: * up notification, but right now it doesn't. Instead, all
052: * notifications are delivered on the notification thread.
053: * Since that's a daemon thread, a notification might not
054: * complete before main terminates.
055: */
056: public void helloServiceChanged() {
057: display();
058: }
059:
060: private void display() {
061: Map names = HelloService.getDisplayNames(ULocale.US);
062: System.out
063: .println("displaying " + names.size() + " names.");
064: Iterator iter = names.entrySet().iterator();
065: while (iter.hasNext()) {
066: Entry entry = (Entry) iter.next();
067: String displayName = (String) entry.getKey();
068: HelloService service = HelloService.get((String) entry
069: .getValue());
070: System.out.println(displayName + " says "
071: + service.hello());
072: try {
073: Thread.sleep(50);
074: } catch (InterruptedException e) {
075: }
076: }
077: System.out.println("----");
078: }
079: }
080:
081: /**
082: * A thread to update the service.
083: */
084: static class HelloUpdateThread extends Thread {
085: String[][] updates = { { "Hey", "en_US_INFORMAL" },
086: { "Hallo", "de_DE_INFORMAL" },
087: { "Yo!", "en_US_CALIFORNIA_INFORMAL" },
088: { "Chi Fanle Ma?", "zh__INFORMAL" },
089: { "Munch munch... Burger?", "en" }, { "Sniff", "fr" },
090: { "TongZhi! MaoZeDong SiXiang Wan Sui!", "zh_CN" },
091: { "Bier? Ja!", "de" }, };
092:
093: public void run() {
094: for (int i = 0; i < updates.length; ++i) {
095: try {
096: Thread.sleep(500);
097: } catch (InterruptedException e) {
098: }
099: HelloService.register(updates[i][0], new ULocale(
100: updates[i][1]));
101: }
102: }
103: }
104:
105: /**
106: * An example service that wraps an ICU service in order to export custom API and
107: * notification. The service just implements 'hello'.
108: */
109: static final class HelloService {
110: private static ICUService registry;
111: private String name;
112:
113: private HelloService(String name) {
114: this .name = name;
115: }
116:
117: /**
118: * The hello service...
119: */
120: public String hello() {
121: return name;
122: }
123:
124: public String toString() {
125: return super .toString() + ": " + name;
126: }
127:
128: /**
129: * Deferred init.
130: */
131: private static ICUService registry() {
132: if (registry == null) {
133: initRegistry();
134: }
135: return registry;
136: }
137:
138: private static void initRegistry() {
139: registry = new ICULocaleService() {
140: protected boolean acceptsListener(EventListener l) {
141: return true; // we already verify in our wrapper APIs
142: }
143:
144: protected void notifyListener(EventListener l) {
145: ((HelloServiceListener) l).helloServiceChanged();
146: }
147: };
148:
149: // initialize
150: doRegister("Hello", "en");
151: doRegister("Bonjour", "fr");
152: doRegister("Ni Hao", "zh_CN");
153: doRegister("Guten Tag", "de");
154: }
155:
156: /**
157: * A custom listener for changes to this service. We don't need to
158: * point to the service since it is defined by this class and not
159: * an object.
160: */
161: public static interface HelloServiceListener extends
162: EventListener {
163: public void helloServiceChanged();
164: }
165:
166: /**
167: * Type-safe notification for this service.
168: */
169: public static void addListener(HelloServiceListener l) {
170: registry().addListener(l);
171: }
172:
173: /**
174: * Type-safe notification for this service.
175: */
176: public static void removeListener(HelloServiceListener l) {
177: registry().removeListener(l);
178: }
179:
180: /**
181: * Type-safe access to the service.
182: */
183: public static HelloService get(String id) {
184: return (HelloService) registry().get(id);
185: }
186:
187: public static Set getVisibleIDs() {
188: return registry().getVisibleIDs();
189: }
190:
191: public static Map getDisplayNames(ULocale locale) {
192: return getDisplayNames(registry(), locale);
193: }
194:
195: /**
196: * Register a new hello string for this locale.
197: */
198: public static void register(String helloString, ULocale locale) {
199: if (helloString == null || locale == null) {
200: throw new NullPointerException();
201: }
202: doRegister(helloString, locale.toString());
203: }
204:
205: private static void doRegister(String hello, String id) {
206: registry().registerObject(new HelloService(hello), id);
207: }
208:
209: /**
210: * Convenience override of getDisplayNames(ULocale, Comparator, String) that
211: * uses the default collator for the locale as the comparator to
212: * sort the display names, and null for the matchID.
213: */
214: public static SortedMap getDisplayNames(ICUService service,
215: ULocale locale) {
216: Collator col = Collator.getInstance(locale);
217: return service.getDisplayNames(locale, col, null);
218: }
219: }
220: }
|