]> gitweb.fperrin.net Git - Dictionary.git/blob - src/com/hughes/android/dictionary/engine/TransliteratorManager.java
Fix issues noted by lint tools.
[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 ThreadSetup threadSetup = null;
29     private static final LRUCacheMap<String, Transliterator> cache = new LRUCacheMap<>(4);
30
31     // Whom to notify when we're all set up and ready to go.
32     private static final List<Callback> callbacks = new ArrayList<>();
33
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;
40         synchronized (cache) {
41             result = cache.get(rules);
42             if (result == null) {
43                 result = Transliterator.createFromRules("", rules, Transliterator.FORWARD);
44                 cache.put(rules, result);
45             }
46         }
47         return result;
48     }
49
50     public static synchronized boolean init(final Callback callback, final ThreadSetup setupCallback) {
51         if (ready) {
52             return true;
53         }
54         if (callback != null) {
55             callbacks.add(callback);
56         }
57         if (!starting) {
58             starting = true;
59             threadSetup = setupCallback;
60             new Thread(init).start();
61         }
62         return false;
63     }
64
65     private static final Runnable init = new Runnable() {
66         @Override
67         public void run() {
68             synchronized (TransliteratorManager.class) {
69                 if (threadSetup != null) threadSetup.onThreadStart();
70             }
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);
75             }
76
77             final List<Callback> callbacks;
78             synchronized (TransliteratorManager.class) {
79                 callbacks = new ArrayList<>(TransliteratorManager.callbacks);
80                 ready = true;
81             }
82             for (final Callback callback : callbacks) {
83                 callback.onTransliteratorReady();
84             }
85         }
86     };
87
88     public interface ThreadSetup {
89         void onThreadStart();
90     }
91
92     public interface Callback {
93         void onTransliteratorReady();
94     }
95
96 }