DictionaryCollection.java revision a69f12a246b6c047faf7002a57b1cebc3721d731
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 17package com.android.inputmethod.latin; 18 19import com.android.inputmethod.keyboard.ProximityInfo; 20import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo; 21 22import android.util.Log; 23 24import java.util.ArrayList; 25import java.util.Collection; 26import java.util.Collections; 27import java.util.concurrent.CopyOnWriteArrayList; 28 29/** 30 * Class for a collection of dictionaries that behave like one dictionary. 31 */ 32public class DictionaryCollection extends Dictionary { 33 private final String TAG = DictionaryCollection.class.getSimpleName(); 34 protected final CopyOnWriteArrayList<Dictionary> mDictionaries; 35 36 public DictionaryCollection(final String dictType) { 37 super(dictType); 38 mDictionaries = new CopyOnWriteArrayList<Dictionary>(); 39 } 40 41 public DictionaryCollection(final String dictType, Dictionary... dictionaries) { 42 super(dictType); 43 if (null == dictionaries) { 44 mDictionaries = new CopyOnWriteArrayList<Dictionary>(); 45 } else { 46 mDictionaries = new CopyOnWriteArrayList<Dictionary>(dictionaries); 47 mDictionaries.removeAll(Collections.singleton(null)); 48 } 49 } 50 51 public DictionaryCollection(final String dictType, Collection<Dictionary> dictionaries) { 52 super(dictType); 53 mDictionaries = new CopyOnWriteArrayList<Dictionary>(dictionaries); 54 mDictionaries.removeAll(Collections.singleton(null)); 55 } 56 57 @Override 58 protected ArrayList<SuggestedWordInfo> getWords(final WordComposer composer, 59 final CharSequence prevWordForBigrams, final ProximityInfo proximityInfo) { 60 final CopyOnWriteArrayList<Dictionary> dictionaries = mDictionaries; 61 if (dictionaries.isEmpty()) return null; 62 // To avoid creating unnecessary objects, we get the list out of the first 63 // dictionary and add the rest to it if not null, hence the get(0) 64 ArrayList<SuggestedWordInfo> suggestions = dictionaries.get(0).getWords(composer, 65 prevWordForBigrams, proximityInfo); 66 if (null == suggestions) suggestions = new ArrayList<SuggestedWordInfo>(); 67 final int length = dictionaries.size(); 68 for (int i = 0; i < length; ++ i) { 69 final ArrayList<SuggestedWordInfo> sugg = dictionaries.get(i).getWords(composer, 70 prevWordForBigrams, proximityInfo); 71 if (null != sugg) suggestions.addAll(sugg); 72 } 73 return suggestions; 74 } 75 76 @Override 77 protected ArrayList<SuggestedWordInfo> getBigrams(final WordComposer composer, 78 final CharSequence previousWord) { 79 final CopyOnWriteArrayList<Dictionary> dictionaries = mDictionaries; 80 if (dictionaries.isEmpty()) return null; 81 // To avoid creating unnecessary objects, we get the list out of the first 82 // dictionary and add the rest to it if not null, hence the get(0) 83 ArrayList<SuggestedWordInfo> suggestions = dictionaries.get(0).getBigrams(composer, 84 previousWord); 85 if (null == suggestions) suggestions = new ArrayList<SuggestedWordInfo>(); 86 final int length = dictionaries.size(); 87 for (int i = 0; i < length; ++ i) { 88 final ArrayList<SuggestedWordInfo> sugg = 89 dictionaries.get(i).getBigrams(composer, previousWord); 90 if (null != sugg) suggestions.addAll(sugg); 91 } 92 return suggestions; 93 } 94 95 @Override 96 public boolean isValidWord(CharSequence word) { 97 for (int i = mDictionaries.size() - 1; i >= 0; --i) 98 if (mDictionaries.get(i).isValidWord(word)) return true; 99 return false; 100 } 101 102 @Override 103 public int getFrequency(CharSequence word) { 104 int maxFreq = -1; 105 for (int i = mDictionaries.size() - 1; i >= 0; --i) { 106 final int tempFreq = mDictionaries.get(i).getFrequency(word); 107 if (tempFreq >= maxFreq) { 108 maxFreq = tempFreq; 109 } 110 } 111 return maxFreq; 112 } 113 114 @Override 115 public boolean isInitialized() { 116 return !mDictionaries.isEmpty(); 117 } 118 119 @Override 120 public void close() { 121 for (final Dictionary dict : mDictionaries) 122 dict.close(); 123 } 124 125 // Warning: this is not thread-safe. Take necessary precaution when calling. 126 public void addDictionary(final Dictionary newDict) { 127 if (null == newDict) return; 128 if (mDictionaries.contains(newDict)) { 129 Log.w(TAG, "This collection already contains this dictionary: " + newDict); 130 } 131 mDictionaries.add(newDict); 132 } 133 134 // Warning: this is not thread-safe. Take necessary precaution when calling. 135 public void removeDictionary(final Dictionary dict) { 136 if (mDictionaries.contains(dict)) { 137 mDictionaries.remove(dict); 138 } else { 139 Log.w(TAG, "This collection does not contain this dictionary: " + dict); 140 } 141 } 142} 143