• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2013 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.latin.SuggestedWords.SuggestedWordInfo;
20 import com.android.inputmethod.latin.common.ComposedData;
21 import com.android.inputmethod.latin.settings.SettingsValuesForSuggestion;
22 
23 import java.util.ArrayList;
24 import java.util.Locale;
25 import java.util.concurrent.locks.ReentrantReadWriteLock;
26 
27 /**
28  * This class provides binary dictionary reading operations with locking. An instance of this class
29  * can be used by multiple threads. Note that different session IDs must be used when multiple
30  * threads get suggestions using this class.
31  */
32 public final class ReadOnlyBinaryDictionary extends Dictionary {
33     /**
34      * A lock for accessing binary dictionary. Only closing binary dictionary is the operation
35      * that change the state of dictionary.
36      */
37     private final ReentrantReadWriteLock mLock = new ReentrantReadWriteLock();
38 
39     private final BinaryDictionary mBinaryDictionary;
40 
ReadOnlyBinaryDictionary(final String filename, final long offset, final long length, final boolean useFullEditDistance, final Locale locale, final String dictType)41     public ReadOnlyBinaryDictionary(final String filename, final long offset, final long length,
42             final boolean useFullEditDistance, final Locale locale, final String dictType) {
43         super(dictType, locale);
44         mBinaryDictionary = new BinaryDictionary(filename, offset, length, useFullEditDistance,
45                 locale, dictType, false /* isUpdatable */);
46     }
47 
isValidDictionary()48     public boolean isValidDictionary() {
49         return mBinaryDictionary.isValidDictionary();
50     }
51 
52     @Override
getSuggestions(final ComposedData composedData, final NgramContext ngramContext, final long proximityInfoHandle, final SettingsValuesForSuggestion settingsValuesForSuggestion, final int sessionId, final float weightForLocale, final float[] inOutWeightOfLangModelVsSpatialModel)53     public ArrayList<SuggestedWordInfo> getSuggestions(final ComposedData composedData,
54             final NgramContext ngramContext, final long proximityInfoHandle,
55             final SettingsValuesForSuggestion settingsValuesForSuggestion,
56             final int sessionId, final float weightForLocale,
57             final float[] inOutWeightOfLangModelVsSpatialModel) {
58         if (mLock.readLock().tryLock()) {
59             try {
60                 return mBinaryDictionary.getSuggestions(composedData, ngramContext,
61                         proximityInfoHandle, settingsValuesForSuggestion, sessionId,
62                         weightForLocale, inOutWeightOfLangModelVsSpatialModel);
63             } finally {
64                 mLock.readLock().unlock();
65             }
66         }
67         return null;
68     }
69 
70     @Override
isInDictionary(final String word)71     public boolean isInDictionary(final String word) {
72         if (mLock.readLock().tryLock()) {
73             try {
74                 return mBinaryDictionary.isInDictionary(word);
75             } finally {
76                 mLock.readLock().unlock();
77             }
78         }
79         return false;
80     }
81 
82     @Override
shouldAutoCommit(final SuggestedWordInfo candidate)83     public boolean shouldAutoCommit(final SuggestedWordInfo candidate) {
84         if (mLock.readLock().tryLock()) {
85             try {
86                 return mBinaryDictionary.shouldAutoCommit(candidate);
87             } finally {
88                 mLock.readLock().unlock();
89             }
90         }
91         return false;
92     }
93 
94     @Override
getFrequency(final String word)95     public int getFrequency(final String word) {
96         if (mLock.readLock().tryLock()) {
97             try {
98                 return mBinaryDictionary.getFrequency(word);
99             } finally {
100                 mLock.readLock().unlock();
101             }
102         }
103         return NOT_A_PROBABILITY;
104     }
105 
106     @Override
getMaxFrequencyOfExactMatches(final String word)107     public int getMaxFrequencyOfExactMatches(final String word) {
108         if (mLock.readLock().tryLock()) {
109             try {
110                 return mBinaryDictionary.getMaxFrequencyOfExactMatches(word);
111             } finally {
112                 mLock.readLock().unlock();
113             }
114         }
115         return NOT_A_PROBABILITY;
116     }
117 
118     @Override
close()119     public void close() {
120         mLock.writeLock().lock();
121         try {
122             mBinaryDictionary.close();
123         } finally {
124             mLock.writeLock().unlock();
125         }
126     }
127 }
128