1 // Copyright 2011 Google Inc. All Rights Reserved.
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 package com.hughes.android.dictionary.engine;
17 import com.ibm.icu.text.Transliterator;
19 import java.util.ArrayList;
20 import java.util.List;
22 import com.hughes.util.LRUCacheMap;
24 public class TransliteratorManager {
26 private static boolean starting = false;
27 private static boolean ready = false;
28 private static ThreadSetup threadSetup = null;
29 private static LRUCacheMap<String, Transliterator> cache = new LRUCacheMap<String, Transliterator>(4);
31 // Whom to notify when we're all set up and ready to go.
32 private static List<Callback> callbacks = new ArrayList<TransliteratorManager.Callback>();
34 public static Transliterator get(String rules) {
35 // DO NOT make the method synchronized!
36 // synchronizing on the class would break the whole
37 // asynchronous init concept, since the runnable
38 // then holds the same lock as the init function needs.
39 Transliterator result = null;
40 synchronized (cache) {
41 result = cache.get(rules);
43 result = Transliterator.createFromRules("", rules, Transliterator.FORWARD);
44 cache.put(rules, result);
50 public static synchronized boolean init(final Callback callback, final ThreadSetup setupCallback) {
54 if (callback != null) {
55 callbacks.add(callback);
59 threadSetup = setupCallback;
60 new Thread(init).start();
65 private static final Runnable init = new Runnable() {
68 synchronized (TransliteratorManager.class) {
69 if (threadSetup != null) threadSetup.onThreadStart();
71 System.out.println("Starting Transliterator load.");
72 final String transliterated = get(Language.en.getDefaultNormalizerRules()).transliterate("Îñţérñåţîöñåļîžåţîờñ");
73 if (!"internationalization".equals(transliterated)) {
74 System.out.println("Wrong transliteration: " + transliterated);
77 final List<Callback> callbacks = new ArrayList<TransliteratorManager.Callback>();
78 synchronized (TransliteratorManager.class) {
79 callbacks.addAll(TransliteratorManager.callbacks);
82 for (final Callback callback : callbacks) {
83 callback.onTransliteratorReady();
88 public interface ThreadSetup {
92 public interface Callback {
93 void onTransliteratorReady();