DictionaryCollection.java revision b6ca354431367b625daf9fff5fbe4b1f5ef996ab
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 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 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).getSuggestions(composer,
65                prevWord, proximityInfo);
66        if (null == suggestions) suggestions = CollectionUtils.newArrayList();
67        final int length = dictionaries.size();
68        for (int i = 1; i < length; ++ i) {
69            final ArrayList<SuggestedWordInfo> sugg = dictionaries.get(i).getSuggestions(composer,
70                    prevWord, proximityInfo);
71            if (null != sugg) suggestions.addAll(sugg);
72        }
73        return suggestions;
74    }
75
76    @Override
77    public boolean isValidWord(final String word) {
78        for (int i = mDictionaries.size() - 1; i >= 0; --i)
79            if (mDictionaries.get(i).isValidWord(word)) return true;
80        return false;
81    }
82
83    @Override
84    public int getFrequency(final String word) {
85        int maxFreq = -1;
86        for (int i = mDictionaries.size() - 1; i >= 0; --i) {
87            final int tempFreq = mDictionaries.get(i).getFrequency(word);
88            if (tempFreq >= maxFreq) {
89                maxFreq = tempFreq;
90            }
91        }
92        return maxFreq;
93    }
94
95    @Override
96    public boolean isInitialized() {
97        return !mDictionaries.isEmpty();
98    }
99
100    @Override
101    public void close() {
102        for (final Dictionary dict : mDictionaries)
103            dict.close();
104    }
105
106    // Warning: this is not thread-safe. Take necessary precaution when calling.
107    public void addDictionary(final Dictionary newDict) {
108        if (null == newDict) return;
109        if (mDictionaries.contains(newDict)) {
110            Log.w(TAG, "This collection already contains this dictionary: " + newDict);
111        }
112        mDictionaries.add(newDict);
113    }
114
115    // Warning: this is not thread-safe. Take necessary precaution when calling.
116    public void removeDictionary(final Dictionary dict) {
117        if (mDictionaries.contains(dict)) {
118            mDictionaries.remove(dict);
119        } else {
120            Log.w(TAG, "This collection does not contain this dictionary: " + dict);
121        }
122    }
123}
124