MultiSourceCorpus.java revision f252dc7a25ba08b973ecc1cfbbce58eb78d42167
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
17package com.android.quicksearchbox;
18
19
20import com.android.quicksearchbox.util.BarrierConsumer;
21import com.android.quicksearchbox.util.NamedTaskExecutor;
22
23import android.content.Context;
24
25import java.util.ArrayList;
26import java.util.Collection;
27
28/**
29 * Base class for corpora backed by multiple sources.
30 */
31public abstract class MultiSourceCorpus extends AbstractCorpus {
32
33    private final Context mContext;
34
35    private final NamedTaskExecutor mExecutor;
36
37    private final ArrayList<Source> mSources;
38
39    public MultiSourceCorpus(Context context, NamedTaskExecutor executor, Source... sources) {
40        mContext = context;
41        mExecutor = executor;
42
43        mSources = new ArrayList<Source>();
44        for (Source source : sources) {
45            mSources.add(source);
46        }
47    }
48
49    protected Context getContext() {
50        return mContext;
51    }
52
53    public Collection<Source> getSources() {
54        return mSources;
55    }
56
57    /**
58     * Creates a corpus result object for a set of source results.
59     * This method should not call {@link Result#fill}.
60     *
61     * @param query The query text.
62     * @param results The results of the queries.
63     * @return An instance of {@link Result} or a subclass of it.
64     */
65    protected abstract Result createResult(String query, ArrayList<SourceResult> results);
66
67    public CorpusResult getSuggestions(String query, int queryLimit) {
68        BarrierConsumer<SourceResult> consumer =
69                new BarrierConsumer<SourceResult>(mSources.size());
70        QueryTask.startQueries(query, queryLimit, mSources, mExecutor,
71                null, consumer);
72        ArrayList<SourceResult> results = consumer.getValues();
73        Result result = createResult(query, results);
74        result.fill();
75        return result;
76    }
77
78    /**
79     * Base class for results returned by {@link MultiSourceCorpus#getSuggestions}.
80     * Subclasses of {@link MultiSourceCorpus} should override
81     * {@link MultiSourceCorpus#createResult} and return an instance of this class or a
82     * subclass.
83     */
84    protected class Result extends ListSuggestionCursor implements CorpusResult {
85
86        private final ArrayList<SourceResult> mResults;
87
88        public Result(String userQuery, ArrayList<SourceResult> results) {
89            super(userQuery);
90            mResults = results;
91        }
92
93        protected ArrayList<SourceResult> getResults() {
94            return mResults;
95        }
96
97        /**
98         * Fills the list of suggestions using the list of results.
99         * The default implementation concatenates the results.
100         */
101        public void fill() {
102            for (SourceResult result : getResults()) {
103                int count = result.getCount();
104                for (int i = 0; i < count; i++) {
105                    result.moveTo(i);
106                    add(new SuggestionPosition(result));
107                }
108            }
109        }
110
111        public Corpus getCorpus() {
112            return MultiSourceCorpus.this;
113        }
114
115        @Override
116        public void close() {
117            super.close();
118            for (SourceResult result : mResults) {
119                result.close();
120            }
121        }
122    }
123
124}
125