Home | History | Annotate | Download | only in latin
      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.keyboard.ProximityInfo;
     20 import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo;
     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 
     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);
     44         mBinaryDictionary = new BinaryDictionary(filename, offset, length, useFullEditDistance,
     45                 locale, dictType, false /* isUpdatable */);
     46     }
     47 
     48     public boolean isValidDictionary() {
     49         return mBinaryDictionary.isValidDictionary();
     50     }
     51 
     52     @Override
     53     public ArrayList<SuggestedWordInfo> getSuggestions(final WordComposer composer,
     54             final PrevWordsInfo prevWordsInfo, final ProximityInfo proximityInfo,
     55             final SettingsValuesForSuggestion settingsValuesForSuggestion,
     56             final int sessionId, final float[] inOutLanguageWeight) {
     57         if (mLock.readLock().tryLock()) {
     58             try {
     59                 return mBinaryDictionary.getSuggestions(composer, prevWordsInfo, proximityInfo,
     60                         settingsValuesForSuggestion, sessionId, inOutLanguageWeight);
     61             } finally {
     62                 mLock.readLock().unlock();
     63             }
     64         }
     65         return null;
     66     }
     67 
     68     @Override
     69     public boolean isInDictionary(final String word) {
     70         if (mLock.readLock().tryLock()) {
     71             try {
     72                 return mBinaryDictionary.isInDictionary(word);
     73             } finally {
     74                 mLock.readLock().unlock();
     75             }
     76         }
     77         return false;
     78     }
     79 
     80     @Override
     81     public boolean shouldAutoCommit(final SuggestedWordInfo candidate) {
     82         if (mLock.readLock().tryLock()) {
     83             try {
     84                 return mBinaryDictionary.shouldAutoCommit(candidate);
     85             } finally {
     86                 mLock.readLock().unlock();
     87             }
     88         }
     89         return false;
     90     }
     91 
     92     @Override
     93     public int getFrequency(final String word) {
     94         if (mLock.readLock().tryLock()) {
     95             try {
     96                 return mBinaryDictionary.getFrequency(word);
     97             } finally {
     98                 mLock.readLock().unlock();
     99             }
    100         }
    101         return NOT_A_PROBABILITY;
    102     }
    103 
    104     @Override
    105     public int getMaxFrequencyOfExactMatches(final String word) {
    106         if (mLock.readLock().tryLock()) {
    107             try {
    108                 return mBinaryDictionary.getMaxFrequencyOfExactMatches(word);
    109             } finally {
    110                 mLock.readLock().unlock();
    111             }
    112         }
    113         return NOT_A_PROBABILITY;
    114     }
    115 
    116     @Override
    117     public void close() {
    118         mLock.writeLock().lock();
    119         try {
    120             mBinaryDictionary.close();
    121         } finally {
    122             mLock.writeLock().unlock();
    123         }
    124     }
    125 }
    126