Home | History | Annotate | Download | only in quicksearchbox
      1 /*
      2  * Copyright (C) 2010 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.quicksearchbox;
     18 
     19 import android.database.DataSetObservable;
     20 import android.database.DataSetObserver;
     21 import android.util.Log;
     22 
     23 import java.util.Collection;
     24 import java.util.Collections;
     25 import java.util.HashMap;
     26 import java.util.HashSet;
     27 
     28 
     29 /**
     30  * Mock implementation of {@link Corpora}.
     31  */
     32 public class MockCorpora implements Corpora {
     33 
     34     private static final String TAG = "QSB.MockCorpora";
     35 
     36     private final DataSetObservable mDataSetObservable = new DataSetObservable();
     37 
     38     private HashMap<String,Corpus> mCorporaByName = new HashMap<String,Corpus>();
     39     private HashSet<Corpus> mDefaultCorpora = new HashSet<Corpus>();
     40 
     41     private Corpus mWebCorpus;
     42 
     43     public void addCorpus(Corpus corpus) {
     44         Corpus oldCorpus = mCorporaByName.put(corpus.getName(), corpus);
     45         if (oldCorpus != null) {
     46             Log.d(TAG, "Replaced " + oldCorpus + " with " + corpus);
     47         }
     48         notifyDataSetChanged();
     49     }
     50 
     51     public void setWebCorpus(Corpus webCorpus) {
     52         mWebCorpus = webCorpus;
     53     }
     54 
     55     public void addDefaultCorpus(Corpus corpus) {
     56         mDefaultCorpora.add(corpus);
     57     }
     58 
     59     public Collection<Corpus> getAllCorpora() {
     60         return Collections.unmodifiableCollection(mCorporaByName.values());
     61     }
     62 
     63     public Corpus getCorpus(String name) {
     64         return mCorporaByName.get(name);
     65     }
     66 
     67     public Corpus getWebCorpus() {
     68         return mWebCorpus;
     69     }
     70 
     71     public Corpus getCorpusForSource(Source source) {
     72         for (Corpus corpus : mCorporaByName.values()) {
     73             for (Source corpusSource : corpus.getSources()) {
     74                 if (corpusSource.equals(source)) {
     75                     return corpus;
     76                 }
     77             }
     78         }
     79         return null;
     80     }
     81 
     82     public Collection<Corpus> getEnabledCorpora() {
     83         return getAllCorpora();
     84     }
     85 
     86     public Source getSource(String name) {
     87         for (Corpus corpus : mCorporaByName.values()) {
     88             for (Source source : corpus.getSources()) {
     89                 if (source.getName().equals(name)) {
     90                     return source;
     91                 }
     92             }
     93         }
     94         return null;
     95     }
     96 
     97     public boolean isCorpusDefaultEnabled(Corpus corpus) {
     98         return mDefaultCorpora.contains(corpus);
     99     }
    100 
    101     public boolean isCorpusEnabled(Corpus corpus) {
    102         return true;
    103     }
    104 
    105     public void close() {
    106         // Nothing to release
    107     }
    108 
    109     public void registerDataSetObserver(DataSetObserver observer) {
    110         mDataSetObservable.registerObserver(observer);
    111     }
    112 
    113     public void unregisterDataSetObserver(DataSetObserver observer) {
    114         mDataSetObservable.unregisterObserver(observer);
    115     }
    116 
    117     protected void notifyDataSetChanged() {
    118         mDataSetObservable.notifyChanged();
    119     }
    120 
    121 }
    122