add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / queryparser / src / java / org / apache / lucene / queryParser / surround / query / ComposedQuery.java
1 package org.apache.lucene.queryParser.surround.query;
2 /**
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements.  See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License.  You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18
19 import java.util.List;
20 import java.util.ArrayList;
21 import java.util.Iterator;
22
23 import org.apache.lucene.search.Query;
24
25 public abstract class ComposedQuery extends SrndQuery { 
26   
27   public ComposedQuery(List qs, boolean operatorInfix, String opName) {
28     recompose(qs);
29     this.operatorInfix = operatorInfix;
30     this.opName = opName;
31   }
32   
33   protected void recompose(List queries) {
34     if (queries.size() < 2) throw new AssertionError("Too few subqueries"); 
35     this.queries = queries;
36   }
37   
38   private String opName;
39   public String getOperatorName() {return opName;}
40   
41   private List queries;
42   
43   public Iterator getSubQueriesIterator() {return queries.listIterator();}
44
45   public int getNrSubQueries() {return queries.size();}
46   
47   public SrndQuery getSubQuery(int qn) {return (SrndQuery) queries.get(qn);}
48
49   private boolean operatorInfix; 
50   public boolean isOperatorInfix() { return operatorInfix; } /* else prefix operator */
51   
52   public List<Query> makeLuceneSubQueriesField(String fn, BasicQueryFactory qf) {
53     List<Query> luceneSubQueries = new ArrayList<Query>();
54     Iterator sqi = getSubQueriesIterator();
55     while (sqi.hasNext()) {
56       luceneSubQueries.add( ((SrndQuery) sqi.next()).makeLuceneQueryField(fn, qf));
57     }
58     return luceneSubQueries;
59   }
60
61   @Override
62   public String toString() {
63     StringBuilder r = new StringBuilder();
64     if (isOperatorInfix()) {
65       infixToString(r);
66     } else {
67       prefixToString(r);
68     }
69     weightToString(r);
70     return r.toString();
71   }
72
73   /* Override for different spacing */
74   protected String getPrefixSeparator() { return ", ";}
75   protected String getBracketOpen() { return "(";}
76   protected String getBracketClose() { return ")";}
77   
78   protected void infixToString(StringBuilder r) {
79     /* Brackets are possibly redundant in the result. */
80     Iterator sqi = getSubQueriesIterator();
81     r.append(getBracketOpen());
82     if (sqi.hasNext()) {
83       r.append(sqi.next().toString());
84       while (sqi.hasNext()) {
85         r.append(" ");
86         r.append(getOperatorName()); /* infix operator */
87         r.append(" ");
88         r.append(sqi.next().toString());
89       }
90     }
91     r.append(getBracketClose());
92   }
93
94   protected void prefixToString(StringBuilder r) {
95     Iterator sqi = getSubQueriesIterator();
96     r.append(getOperatorName()); /* prefix operator */
97     r.append(getBracketOpen());
98     if (sqi.hasNext()) {
99       r.append(sqi.next().toString());
100       while (sqi.hasNext()) {
101         r.append(getPrefixSeparator());
102         r.append(sqi.next().toString());
103       }
104     }
105     r.append(getBracketClose());
106   }
107   
108   
109   @Override
110   public boolean isFieldsSubQueryAcceptable() {
111     /* at least one subquery should be acceptable */
112     Iterator sqi = getSubQueriesIterator();
113     while (sqi.hasNext()) {
114       if (((SrndQuery) sqi.next()).isFieldsSubQueryAcceptable()) {
115         return true;
116       }
117     }
118     return false;
119   }
120 }
121