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