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 java.util.ArrayList;
18 import java.util.List;
20 import com.hughes.util.LRUCacheMap;
21 import com.ibm.icu.text.Transliterator;
23 public class TransliteratorManager {
25 private static boolean starting = false;
26 private static boolean ready = false;
27 private static ThreadSetup threadSetup = null;
28 private static final LRUCacheMap<String, Transliterator> cache = new LRUCacheMap<>(4);
30 // Whom to notify when we're all set up and ready to go.
31 private static final List<Callback> callbacks = new ArrayList<>();
33 public static Transliterator get(String rules) {
34 // DO NOT make the method synchronized!
35 // synchronizing on the class would break the whole
36 // asynchronous init concept, since the runnable
37 // then holds the same lock as the init function needs.
38 Transliterator result;
39 synchronized (cache) {
40 result = cache.get(rules);
42 result = Transliterator.createFromRules("", rules, Transliterator.FORWARD);
43 cache.put(rules, result);
49 public static synchronized boolean init(final Callback callback, final ThreadSetup setupCallback) {
53 if (callback != null) {
54 callbacks.add(callback);
58 threadSetup = setupCallback;
59 new Thread(init).start();
64 private static final Runnable init = new Runnable() {
67 synchronized (TransliteratorManager.class) {
68 if (threadSetup != null) threadSetup.onThreadStart();
70 System.out.println("Starting Transliterator load.");
71 final String transliterated = get(Language.en.getDefaultNormalizerRules()).transliterate("Îñţérñåţîöñåļîžåţîờñ");
72 if (!"internationalization".equals(transliterated)) {
73 System.out.println("Wrong transliteration: " + transliterated);
76 final List<Callback> callbacks;
77 synchronized (TransliteratorManager.class) {
78 callbacks = new ArrayList<>(TransliteratorManager.callbacks);
81 for (final Callback callback : callbacks) {
82 callback.onTransliteratorReady();
87 public interface ThreadSetup {
91 public interface Callback {
92 void onTransliteratorReady();