]> gitweb.fperrin.net Git - Dictionary.git/blob - src/com/hughes/android/dictionary/engine/TransliteratorManager.java
eacab80daac3bd9a5189bbb6433b6dd7c2f99923
[Dictionary.git] / src / com / hughes / android / dictionary / engine / TransliteratorManager.java
1 // Copyright 2011 Google Inc. All Rights Reserved.
2 //
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
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
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.
14
15 package com.hughes.android.dictionary.engine;
16
17 import com.ibm.icu.text.Transliterator;
18
19 import java.util.ArrayList;
20 import java.util.List;
21
22 import com.hughes.util.LRUCacheMap;
23
24 public class TransliteratorManager {
25
26     private static boolean starting = false;
27     private static boolean ready = false;
28     private static LRUCacheMap<String, Transliterator> cache = new LRUCacheMap<String, Transliterator>(4);
29
30     // Whom to notify when we're all set up and ready to go.
31     private static List<Callback> callbacks = new ArrayList<TransliteratorManager.Callback>();
32
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 = null;
39         synchronized (cache) {
40             result = cache.get(rules);
41             if (result == null) {
42                 result = Transliterator.createFromRules("", rules, Transliterator.FORWARD);
43                 cache.put(rules, result);
44             }
45         }
46         return result;
47     }
48
49     public static synchronized boolean init(final Callback callback) {
50         if (ready) {
51             return true;
52         }
53         if (callback != null) {
54             callbacks.add(callback);
55         }
56         if (!starting) {
57             starting = true;
58             new Thread(init).start();
59         }
60         return false;
61     }
62
63     private static final Runnable init = new Runnable() {
64         @Override
65         public void run() {
66             System.out.println("Starting Transliterator load.");
67             final String transliterated = get(Language.en.getDefaultNormalizerRules()).transliterate("Îñţérñåţîöñåļîžåţîờñ");
68             if (!"internationalization".equals(transliterated)) {
69                 System.out.println("Wrong transliteration: " + transliterated);
70             }
71
72             final List<Callback> callbacks = new ArrayList<TransliteratorManager.Callback>();
73             synchronized (TransliteratorManager.class) {
74                 callbacks.addAll(TransliteratorManager.callbacks);
75                 ready = true;
76             }
77             for (final Callback callback : callbacks) {
78                 callback.onTransliteratorReady();
79             }
80         }
81     };
82
83     public interface Callback {
84         void onTransliteratorReady();
85     }
86
87 }