Home | History | Annotate | Download | only in spellcheck
      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 
     17 package com.android.inputmethod.latin.spellcheck;
     18 
     19 import java.util.Locale;
     20 import java.util.concurrent.LinkedBlockingQueue;
     21 
     22 /**
     23  * A blocking queue that creates dictionaries up to a certain limit as necessary.
     24  */
     25 @SuppressWarnings("serial")
     26 public class DictionaryPool extends LinkedBlockingQueue<DictAndProximity> {
     27     private final AndroidSpellCheckerService mService;
     28     private final int mMaxSize;
     29     private final Locale mLocale;
     30     private int mSize;
     31     private volatile boolean mClosed;
     32 
     33     public DictionaryPool(final int maxSize, final AndroidSpellCheckerService service,
     34             final Locale locale) {
     35         super();
     36         mMaxSize = maxSize;
     37         mService = service;
     38         mLocale = locale;
     39         mSize = 0;
     40         mClosed = false;
     41     }
     42 
     43     @Override
     44     public DictAndProximity take() throws InterruptedException {
     45         final DictAndProximity dict = poll();
     46         if (null != dict) return dict;
     47         synchronized(this) {
     48             if (mSize >= mMaxSize) {
     49                 // Our pool is already full. Wait until some dictionary is ready.
     50                 return super.take();
     51             } else {
     52                 ++mSize;
     53                 return mService.createDictAndProximity(mLocale);
     54             }
     55         }
     56     }
     57 
     58     // Convenience method
     59     public DictAndProximity takeOrGetNull() {
     60         try {
     61             return take();
     62         } catch (InterruptedException e) {
     63             return null;
     64         }
     65     }
     66 
     67     public void close() {
     68         synchronized(this) {
     69             mClosed = true;
     70             for (DictAndProximity dict : this) {
     71                 dict.mDictionary.close();
     72             }
     73             clear();
     74         }
     75     }
     76 
     77     @Override
     78     public boolean offer(final DictAndProximity dict) {
     79         if (mClosed) {
     80             dict.mDictionary.close();
     81             return false;
     82         } else {
     83             return super.offer(dict);
     84         }
     85     }
     86 }
     87