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