2 *******************************************************************************
\r
3 * Copyright (C) 2001-2006, International Business Machines Corporation and *
\r
4 * others. All Rights Reserved. *
\r
5 *******************************************************************************
\r
7 package com.ibm.icu.dev.test.util;
\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
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
21 public class ICUServiceTestSample {
\r
22 static public void main(String[] args) {
\r
23 HelloServiceClient client = new HelloServiceClient();
\r
25 Thread t = new HelloUpdateThread();
\r
30 catch (InterruptedException e) {
\r
32 System.out.println("done");
\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
41 static class HelloServiceClient implements HelloService.HelloServiceListener {
\r
43 HelloServiceClient() {
\r
44 HelloService.addListener(this);
\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
58 public void helloServiceChanged() {
\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
74 catch (InterruptedException e) {
\r
77 System.out.println("----");
\r
82 * A thread to update the service.
\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
92 { "TongZhi! MaoZeDong SiXiang Wan Sui!", "zh_CN" },
\r
93 { "Bier? Ja!", "de" },
\r
96 for (int i = 0; i < updates.length; ++i) {
\r
100 catch (InterruptedException e) {
\r
102 HelloService.register(updates[i][0], new ULocale(updates[i][1]));
\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
111 static final class HelloService {
\r
112 private static ICUService registry;
\r
113 private String name;
\r
115 private HelloService(String name) {
\r
120 * The hello service...
\r
122 public String hello() {
\r
126 public String toString() {
\r
127 return super.toString() + ": " + name;
\r
133 private static ICUService registry() {
\r
134 if (registry == null) {
\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
145 protected void notifyListener(EventListener l) {
\r
146 ((HelloServiceListener)l).helloServiceChanged();
\r
151 doRegister("Hello", "en");
\r
152 doRegister("Bonjour", "fr");
\r
153 doRegister("Ni Hao", "zh_CN");
\r
154 doRegister("Guten Tag", "de");
\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
162 public static interface HelloServiceListener extends EventListener {
\r
163 public void helloServiceChanged();
\r
167 * Type-safe notification for this service.
\r
169 public static void addListener(HelloServiceListener l) {
\r
170 registry().addListener(l);
\r
174 * Type-safe notification for this service.
\r
176 public static void removeListener(HelloServiceListener l) {
\r
177 registry().removeListener(l);
\r
181 * Type-safe access to the service.
\r
183 public static HelloService get(String id) {
\r
184 return (HelloService)registry().get(id);
\r
187 public static Set getVisibleIDs() {
\r
188 return registry().getVisibleIDs();
\r
191 public static Map getDisplayNames(ULocale locale) {
\r
192 return getDisplayNames(registry(), locale);
\r
196 * Register a new hello string for this locale.
\r
198 public static void register(String helloString, ULocale locale) {
\r
199 if (helloString == null || locale == null) {
\r
200 throw new NullPointerException();
\r
202 doRegister(helloString, locale.toString());
\r
205 private static void doRegister(String hello, String id) {
\r
206 registry().registerObject(new HelloService(hello), id);
\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
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