Home | History | Annotate | Download | only in latin
      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.utils.CollectionUtils;
     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 
     37     public DictionaryCollection(final String dictType) {
     38         super(dictType);
     39         mDictionaries = CollectionUtils.newCopyOnWriteArrayList();
     40     }
     41 
     42     public DictionaryCollection(final String dictType, final Dictionary... dictionaries) {
     43         super(dictType);
     44         if (null == dictionaries) {
     45             mDictionaries = CollectionUtils.newCopyOnWriteArrayList();
     46         } else {
     47             mDictionaries = CollectionUtils.newCopyOnWriteArrayList(dictionaries);
     48             mDictionaries.removeAll(Collections.singleton(null));
     49         }
     50     }
     51 
     52     public DictionaryCollection(final String dictType, final Collection<Dictionary> dictionaries) {
     53         super(dictType);
     54         mDictionaries = CollectionUtils.newCopyOnWriteArrayList(dictionaries);
     55         mDictionaries.removeAll(Collections.singleton(null));
     56     }
     57 
     58     @Override
     59     public ArrayList<SuggestedWordInfo> getSuggestions(final WordComposer composer,
     60             final String prevWord, final ProximityInfo proximityInfo,
     61             final boolean blockOffensiveWords, final int[] additionalFeaturesOptions) {
     62         final CopyOnWriteArrayList<Dictionary> dictionaries = mDictionaries;
     63         if (dictionaries.isEmpty()) return null;
     64         // To avoid creating unnecessary objects, we get the list out of the first
     65         // dictionary and add the rest to it if not null, hence the get(0)
     66         ArrayList<SuggestedWordInfo> suggestions = dictionaries.get(0).getSuggestions(composer,
     67                 prevWord, proximityInfo, blockOffensiveWords, additionalFeaturesOptions);
     68         if (null == suggestions) suggestions = CollectionUtils.newArrayList();
     69         final int length = dictionaries.size();
     70         for (int i = 1; i < length; ++ i) {
     71             final ArrayList<SuggestedWordInfo> sugg = dictionaries.get(i).getSuggestions(composer,
     72                     prevWord, proximityInfo, blockOffensiveWords, additionalFeaturesOptions);
     73             if (null != sugg) suggestions.addAll(sugg);
     74         }
     75         return suggestions;
     76     }
     77 
     78     @Override
     79     public boolean isValidWord(final String word) {
     80         for (int i = mDictionaries.size() - 1; i >= 0; --i)
     81             if (mDictionaries.get(i).isValidWord(word)) return true;
     82         return false;
     83     }
     84 
     85     @Override
     86     public int getFrequency(final String word) {
     87         int maxFreq = -1;
     88         for (int i = mDictionaries.size() - 1; i >= 0; --i) {
     89             final int tempFreq = mDictionaries.get(i).getFrequency(word);
     90             if (tempFreq >= maxFreq) {
     91                 maxFreq = tempFreq;
     92             }
     93         }
     94         return maxFreq;
     95     }
     96 
     97     @Override
     98     public boolean isInitialized() {
     99         return !mDictionaries.isEmpty();
    100     }
    101 
    102     @Override
    103     public void close() {
    104         for (final Dictionary dict : mDictionaries)
    105             dict.close();
    106     }
    107 
    108     // Warning: this is not thread-safe. Take necessary precaution when calling.
    109     public void addDictionary(final Dictionary newDict) {
    110         if (null == newDict) return;
    111         if (mDictionaries.contains(newDict)) {
    112             Log.w(TAG, "This collection already contains this dictionary: " + newDict);
    113         }
    114         mDictionaries.add(newDict);
    115     }
    116 
    117     // Warning: this is not thread-safe. Take necessary precaution when calling.
    118     public void removeDictionary(final Dictionary dict) {
    119         if (mDictionaries.contains(dict)) {
    120             mDictionaries.remove(dict);
    121         } else {
    122             Log.w(TAG, "This collection does not contain this dictionary: " + dict);
    123         }
    124     }
    125 }
    126