]> gitweb.fperrin.net Git - Dictionary.git/blob - src/com/hughes/android/dictionary/engine/TransliteratorManager.java
Clean up order of imports.
[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 java.util.ArrayList;
18 import java.util.List;
19
20 import com.hughes.util.LRUCacheMap;
21 import com.ibm.icu.text.Transliterator;
22
23 public class TransliteratorManager {
24
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);
29
30     // Whom to notify when we're all set up and ready to go.
31     private static final List<Callback> callbacks = new ArrayList<>();
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;
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, final ThreadSetup setupCallback) {
50         if (ready) {
51             return true;
52         }
53         if (callback != null) {
54             callbacks.add(callback);
55         }
56         if (!starting) {
57             starting = true;
58             threadSetup = setupCallback;
59             new Thread(init).start();
60         }
61         return false;
62     }
63
64     private static final Runnable init = new Runnable() {
65         @Override
66         public void run() {
67             synchronized (TransliteratorManager.class) {
68                 if (threadSetup != null) threadSetup.onThreadStart();
69             }
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);
74             }
75
76             final List<Callback> callbacks;
77             synchronized (TransliteratorManager.class) {
78                 callbacks = new ArrayList<>(TransliteratorManager.callbacks);
79                 ready = true;
80             }
81             for (final Callback callback : callbacks) {
82                 callback.onTransliteratorReady();
83             }
84         }
85     };
86
87     public interface ThreadSetup {
88         void onThreadStart();
89     }
90
91     public interface Callback {
92         void onTransliteratorReady();
93     }
94
95 }