1 /* 2 * Copyright (C) 2011 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of 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, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.inputmethod.latin; 18 19 import android.util.Log; 20 21 import com.android.inputmethod.keyboard.ProximityInfo; 22 import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo; 23 import com.android.inputmethod.latin.settings.SettingsValuesForSuggestion; 24 25 import java.util.ArrayList; 26 import java.util.Collection; 27 import java.util.Collections; 28 import java.util.concurrent.CopyOnWriteArrayList; 29 30 /** 31 * Class for a collection of dictionaries that behave like one dictionary. 32 */ 33 public final class DictionaryCollection extends Dictionary { 34 private final String TAG = DictionaryCollection.class.getSimpleName(); 35 protected final CopyOnWriteArrayList<Dictionary> mDictionaries; 36 DictionaryCollection(final String dictType)37 public DictionaryCollection(final String dictType) { 38 super(dictType); 39 mDictionaries = new CopyOnWriteArrayList<>(); 40 } 41 DictionaryCollection(final String dictType, final Dictionary... dictionaries)42 public DictionaryCollection(final String dictType, final Dictionary... dictionaries) { 43 super(dictType); 44 if (null == dictionaries) { 45 mDictionaries = new CopyOnWriteArrayList<>(); 46 } else { 47 mDictionaries = new CopyOnWriteArrayList<>(dictionaries); 48 mDictionaries.removeAll(Collections.singleton(null)); 49 } 50 } 51 DictionaryCollection(final String dictType, final Collection<Dictionary> dictionaries)52 public DictionaryCollection(final String dictType, final Collection<Dictionary> dictionaries) { 53 super(dictType); 54 mDictionaries = new CopyOnWriteArrayList<>(dictionaries); 55 mDictionaries.removeAll(Collections.singleton(null)); 56 } 57 58 @Override getSuggestions(final WordComposer composer, final PrevWordsInfo prevWordsInfo, final ProximityInfo proximityInfo, final SettingsValuesForSuggestion settingsValuesForSuggestion, final int sessionId, final float[] inOutLanguageWeight)59 public ArrayList<SuggestedWordInfo> getSuggestions(final WordComposer composer, 60 final PrevWordsInfo prevWordsInfo, final ProximityInfo proximityInfo, 61 final SettingsValuesForSuggestion settingsValuesForSuggestion, 62 final int sessionId, final float[] inOutLanguageWeight) { 63 final CopyOnWriteArrayList<Dictionary> dictionaries = mDictionaries; 64 if (dictionaries.isEmpty()) return null; 65 // To avoid creating unnecessary objects, we get the list out of the first 66 // dictionary and add the rest to it if not null, hence the get(0) 67 ArrayList<SuggestedWordInfo> suggestions = dictionaries.get(0).getSuggestions(composer, 68 prevWordsInfo, proximityInfo, settingsValuesForSuggestion, sessionId, 69 inOutLanguageWeight); 70 if (null == suggestions) suggestions = new ArrayList<>(); 71 final int length = dictionaries.size(); 72 for (int i = 1; i < length; ++ i) { 73 final ArrayList<SuggestedWordInfo> sugg = dictionaries.get(i).getSuggestions(composer, 74 prevWordsInfo, proximityInfo, settingsValuesForSuggestion, sessionId, 75 inOutLanguageWeight); 76 if (null != sugg) suggestions.addAll(sugg); 77 } 78 return suggestions; 79 } 80 81 @Override isInDictionary(final String word)82 public boolean isInDictionary(final String word) { 83 for (int i = mDictionaries.size() - 1; i >= 0; --i) 84 if (mDictionaries.get(i).isInDictionary(word)) return true; 85 return false; 86 } 87 88 @Override getFrequency(final String word)89 public int getFrequency(final String word) { 90 int maxFreq = -1; 91 for (int i = mDictionaries.size() - 1; i >= 0; --i) { 92 final int tempFreq = mDictionaries.get(i).getFrequency(word); 93 maxFreq = Math.max(tempFreq, maxFreq); 94 } 95 return maxFreq; 96 } 97 98 @Override getMaxFrequencyOfExactMatches(final String word)99 public int getMaxFrequencyOfExactMatches(final String word) { 100 int maxFreq = -1; 101 for (int i = mDictionaries.size() - 1; i >= 0; --i) { 102 final int tempFreq = mDictionaries.get(i).getMaxFrequencyOfExactMatches(word); 103 maxFreq = Math.max(tempFreq, maxFreq); 104 } 105 return maxFreq; 106 } 107 108 @Override isInitialized()109 public boolean isInitialized() { 110 return !mDictionaries.isEmpty(); 111 } 112 113 @Override close()114 public void close() { 115 for (final Dictionary dict : mDictionaries) 116 dict.close(); 117 } 118 119 // Warning: this is not thread-safe. Take necessary precaution when calling. addDictionary(final Dictionary newDict)120 public void addDictionary(final Dictionary newDict) { 121 if (null == newDict) return; 122 if (mDictionaries.contains(newDict)) { 123 Log.w(TAG, "This collection already contains this dictionary: " + newDict); 124 } 125 mDictionaries.add(newDict); 126 } 127 128 // Warning: this is not thread-safe. Take necessary precaution when calling. removeDictionary(final Dictionary dict)129 public void removeDictionary(final Dictionary dict) { 130 if (mDictionaries.contains(dict)) { 131 mDictionaries.remove(dict); 132 } else { 133 Log.w(TAG, "This collection does not contain this dictionary: " + dict); 134 } 135 } 136 } 137