DictionaryCollection.java revision a91561aa58db1c43092c1caecc051a11fa5391c7
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
17package com.android.inputmethod.latin;
18
19import android.util.Log;
20
21import com.android.inputmethod.keyboard.ProximityInfo;
22import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo;
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 = new CopyOnWriteArrayList<>();
39    }
40
41    public DictionaryCollection(final String dictType, final Dictionary... dictionaries) {
42        super(dictType);
43        if (null == dictionaries) {
44            mDictionaries = new CopyOnWriteArrayList<>();
45        } else {
46            mDictionaries = new CopyOnWriteArrayList<>(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 = new CopyOnWriteArrayList<>(dictionaries);
54        mDictionaries.removeAll(Collections.singleton(null));
55    }
56
57    @Override
58    public ArrayList<SuggestedWordInfo> getSuggestions(final WordComposer composer,
59            final PrevWordsInfo prevWordsInfo, final ProximityInfo proximityInfo,
60            final boolean blockOffensiveWords, final int[] additionalFeaturesOptions,
61            final int sessionId, final float[] inOutLanguageWeight) {
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                prevWordsInfo, proximityInfo, blockOffensiveWords, additionalFeaturesOptions,
68                sessionId, inOutLanguageWeight);
69        if (null == suggestions) suggestions = new ArrayList<>();
70        final int length = dictionaries.size();
71        for (int i = 1; i < length; ++ i) {
72            final ArrayList<SuggestedWordInfo> sugg = dictionaries.get(i).getSuggestions(composer,
73                    prevWordsInfo, proximityInfo, blockOffensiveWords, additionalFeaturesOptions,
74                    sessionId, inOutLanguageWeight);
75            if (null != sugg) suggestions.addAll(sugg);
76        }
77        return suggestions;
78    }
79
80    @Override
81    public boolean isValidWord(final String word) {
82        for (int i = mDictionaries.size() - 1; i >= 0; --i)
83            if (mDictionaries.get(i).isValidWord(word)) return true;
84        return false;
85    }
86
87    @Override
88    public int getFrequency(final String word) {
89        int maxFreq = -1;
90        for (int i = mDictionaries.size() - 1; i >= 0; --i) {
91            final int tempFreq = mDictionaries.get(i).getFrequency(word);
92            if (tempFreq >= maxFreq) {
93                maxFreq = tempFreq;
94            }
95        }
96        return maxFreq;
97    }
98
99    @Override
100    public boolean isInitialized() {
101        return !mDictionaries.isEmpty();
102    }
103
104    @Override
105    public void close() {
106        for (final Dictionary dict : mDictionaries)
107            dict.close();
108    }
109
110    // Warning: this is not thread-safe. Take necessary precaution when calling.
111    public void addDictionary(final Dictionary newDict) {
112        if (null == newDict) return;
113        if (mDictionaries.contains(newDict)) {
114            Log.w(TAG, "This collection already contains this dictionary: " + newDict);
115        }
116        mDictionaries.add(newDict);
117    }
118
119    // Warning: this is not thread-safe. Take necessary precaution when calling.
120    public void removeDictionary(final Dictionary dict) {
121        if (mDictionaries.contains(dict)) {
122            mDictionaries.remove(dict);
123        } else {
124            Log.w(TAG, "This collection does not contain this dictionary: " + dict);
125        }
126    }
127}
128