• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2011 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */
16 
17 package com.android.inputmethod.latin;
18 
19 import com.android.inputmethod.keyboard.ProximityInfo;
20 
21 import android.util.Log;
22 
23 import java.util.Collection;
24 import java.util.Collections;
25 import java.util.concurrent.CopyOnWriteArrayList;
26 
27 /**
28  * Class for a collection of dictionaries that behave like one dictionary.
29  */
30 public class DictionaryCollection extends Dictionary {
31     private final String TAG = DictionaryCollection.class.getSimpleName();
32     protected final CopyOnWriteArrayList<Dictionary> mDictionaries;
33 
DictionaryCollection()34     public DictionaryCollection() {
35         mDictionaries = new CopyOnWriteArrayList<Dictionary>();
36     }
37 
DictionaryCollection(Dictionary... dictionaries)38     public DictionaryCollection(Dictionary... dictionaries) {
39         if (null == dictionaries) {
40             mDictionaries = new CopyOnWriteArrayList<Dictionary>();
41         } else {
42             mDictionaries = new CopyOnWriteArrayList<Dictionary>(dictionaries);
43             mDictionaries.removeAll(Collections.singleton(null));
44         }
45     }
46 
DictionaryCollection(Collection<Dictionary> dictionaries)47     public DictionaryCollection(Collection<Dictionary> dictionaries) {
48         mDictionaries = new CopyOnWriteArrayList<Dictionary>(dictionaries);
49         mDictionaries.removeAll(Collections.singleton(null));
50     }
51 
52     @Override
getWords(final WordComposer composer, final CharSequence prevWordForBigrams, final WordCallback callback, final ProximityInfo proximityInfo)53     public void getWords(final WordComposer composer, final CharSequence prevWordForBigrams,
54             final WordCallback callback, final ProximityInfo proximityInfo) {
55         for (final Dictionary dict : mDictionaries)
56             dict.getWords(composer, prevWordForBigrams, callback, proximityInfo);
57     }
58 
59     @Override
getBigrams(final WordComposer composer, final CharSequence previousWord, final WordCallback callback)60     public void getBigrams(final WordComposer composer, final CharSequence previousWord,
61             final WordCallback callback) {
62         for (final Dictionary dict : mDictionaries)
63             dict.getBigrams(composer, previousWord, callback);
64     }
65 
66     @Override
isValidWord(CharSequence word)67     public boolean isValidWord(CharSequence word) {
68         for (int i = mDictionaries.size() - 1; i >= 0; --i)
69             if (mDictionaries.get(i).isValidWord(word)) return true;
70         return false;
71     }
72 
73     @Override
getFrequency(CharSequence word)74     public int getFrequency(CharSequence word) {
75         int maxFreq = -1;
76         for (int i = mDictionaries.size() - 1; i >= 0; --i) {
77             final int tempFreq = mDictionaries.get(i).getFrequency(word);
78             if (tempFreq >= maxFreq) {
79                 maxFreq = tempFreq;
80             }
81         }
82         return maxFreq;
83     }
84 
isEmpty()85     public boolean isEmpty() {
86         return mDictionaries.isEmpty();
87     }
88 
89     @Override
close()90     public void close() {
91         for (final Dictionary dict : mDictionaries)
92             dict.close();
93     }
94 
95     // Warning: this is not thread-safe. Take necessary precaution when calling.
addDictionary(final Dictionary newDict)96     public void addDictionary(final Dictionary newDict) {
97         if (null == newDict) return;
98         if (mDictionaries.contains(newDict)) {
99             Log.w(TAG, "This collection already contains this dictionary: " + newDict);
100         }
101         mDictionaries.add(newDict);
102     }
103 
104     // Warning: this is not thread-safe. Take necessary precaution when calling.
removeDictionary(final Dictionary dict)105     public void removeDictionary(final Dictionary dict) {
106         if (mDictionaries.contains(dict)) {
107             mDictionaries.remove(dict);
108         } else {
109             Log.w(TAG, "This collection does not contain this dictionary: " + dict);
110         }
111     }
112 }
113