pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / benchmark / src / java / org / apache / lucene / benchmark / byTask / feeds / AbstractQueryMaker.java
1 package org.apache.lucene.benchmark.byTask.feeds;
2 /**
3  * Copyright 2005 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 import org.apache.lucene.search.Query;
19 import org.apache.lucene.benchmark.byTask.utils.Config;
20
21 /**
22  * Abstract base query maker. 
23  * Each query maker should just implement the {@link #prepareQueries()} method.
24  **/
25 public abstract class AbstractQueryMaker implements QueryMaker {
26
27   protected int qnum = 0;
28   protected Query[] queries;
29   protected Config config;
30
31   public void resetInputs() {
32     qnum = 0;
33   }
34
35   protected abstract Query[] prepareQueries() throws Exception;
36
37   public void setConfig(Config config) throws Exception {
38     this.config = config;
39     queries = prepareQueries();
40   }
41
42   public String printQueries() {
43     String newline = System.getProperty("line.separator");
44     StringBuilder sb = new StringBuilder();
45     if (queries != null) {
46       for (int i = 0; i < queries.length; i++) {
47         sb.append(i+". "+ queries[i].getClass().getSimpleName()+" - "+queries[i].toString());
48         sb.append(newline);
49       }
50     }
51     return sb.toString();
52   }
53
54   public Query makeQuery() throws Exception {
55     return queries[nextQnum()];
56   }
57   
58   // return next qnum
59   protected synchronized int nextQnum() {
60     int res = qnum;
61     qnum = (qnum+1) % queries.length;
62     return res;
63   }
64
65   /*
66   *  (non-Javadoc)
67   * @see org.apache.lucene.benchmark.byTask.feeds.QueryMaker#makeQuery(int)
68   */
69   public Query makeQuery(int size) throws Exception {
70     throw new Exception(this+".makeQuery(int size) is not supported!");
71   }
72 }