2 *******************************************************************************
\r
3 * Copyright (C) 2001-2010, 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 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
17 import com.ibm.icu.impl.ICULocaleService;
\r
18 import com.ibm.icu.impl.ICUService;
\r
19 import com.ibm.icu.util.ULocale;
\r
21 public class ICUServiceTestSample {
\r
22 static public void main(String[] args) {
\r
23 new HelloServiceClient();
\r
25 Thread t = new HelloUpdateThread();
\r
30 catch (InterruptedException e) {
\r
32 System.out.println("done");
\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
39 static class HelloServiceClient implements HelloService.HelloServiceListener {
\r
41 HelloServiceClient() {
\r
42 HelloService.addListener(this);
\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
56 public void helloServiceChanged() {
\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
72 catch (InterruptedException e) {
\r
75 System.out.println("----");
\r
80 * A thread to update the service.
\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
90 { "TongZhi! MaoZeDong SiXiang Wan Sui!", "zh_CN" },
\r
91 { "Bier? Ja!", "de" },
\r
94 for (int i = 0; i < updates.length; ++i) {
\r
98 catch (InterruptedException e) {
\r
100 HelloService.register(updates[i][0], new ULocale(updates[i][1]));
\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
109 static final class HelloService {
\r
110 private static ICUService registry;
\r
111 private String name;
\r
113 private HelloService(String name) {
\r
118 * The hello service...
\r
120 public String hello() {
\r
124 public String toString() {
\r
125 return super.toString() + ": " + name;
\r
131 private static ICUService registry() {
\r
132 if (registry == null) {
\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
143 protected void notifyListener(EventListener l) {
\r
144 ((HelloServiceListener)l).helloServiceChanged();
\r
149 doRegister("Hello", "en");
\r
150 doRegister("Bonjour", "fr");
\r
151 doRegister("Ni Hao", "zh_CN");
\r
152 doRegister("Guten Tag", "de");
\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
160 public static interface HelloServiceListener extends EventListener {
\r
161 public void helloServiceChanged();
\r
165 * Type-safe notification for this service.
\r
167 public static void addListener(HelloServiceListener l) {
\r
168 registry().addListener(l);
\r
172 * Type-safe notification for this service.
\r
174 public static void removeListener(HelloServiceListener l) {
\r
175 registry().removeListener(l);
\r
179 * Type-safe access to the service.
\r
181 public static HelloService get(String id) {
\r
182 return (HelloService)registry().get(id);
\r
185 public static Set getVisibleIDs() {
\r
186 return registry().getVisibleIDs();
\r
189 public static Map getDisplayNames(ULocale locale) {
\r
190 return getDisplayNames(registry(), locale);
\r
194 * Register a new hello string for this locale.
\r
196 public static void register(String helloString, ULocale locale) {
\r
197 if (helloString == null || locale == null) {
\r
198 throw new NullPointerException();
\r
200 doRegister(helloString, locale.toString());
\r
203 private static void doRegister(String hello, String id) {
\r
204 registry().registerObject(new HelloService(hello), id);
\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
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