1 package org.apache.lucene.queryParser.surround.query;
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 import java.util.List;
20 import java.util.ArrayList;
21 import java.util.Iterator;
23 import org.apache.lucene.search.Query;
25 public abstract class ComposedQuery extends SrndQuery {
27 public ComposedQuery(List qs, boolean operatorInfix, String opName) {
29 this.operatorInfix = operatorInfix;
33 protected void recompose(List queries) {
34 if (queries.size() < 2) throw new AssertionError("Too few subqueries");
35 this.queries = queries;
38 private String opName;
39 public String getOperatorName() {return opName;}
43 public Iterator getSubQueriesIterator() {return queries.listIterator();}
45 public int getNrSubQueries() {return queries.size();}
47 public SrndQuery getSubQuery(int qn) {return (SrndQuery) queries.get(qn);}
49 private boolean operatorInfix;
50 public boolean isOperatorInfix() { return operatorInfix; } /* else prefix operator */
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));
58 return luceneSubQueries;
62 public String toString() {
63 StringBuilder r = new StringBuilder();
64 if (isOperatorInfix()) {
73 /* Override for different spacing */
74 protected String getPrefixSeparator() { return ", ";}
75 protected String getBracketOpen() { return "(";}
76 protected String getBracketClose() { return ")";}
78 protected void infixToString(StringBuilder r) {
79 /* Brackets are possibly redundant in the result. */
80 Iterator sqi = getSubQueriesIterator();
81 r.append(getBracketOpen());
83 r.append(sqi.next().toString());
84 while (sqi.hasNext()) {
86 r.append(getOperatorName()); /* infix operator */
88 r.append(sqi.next().toString());
91 r.append(getBracketClose());
94 protected void prefixToString(StringBuilder r) {
95 Iterator sqi = getSubQueriesIterator();
96 r.append(getOperatorName()); /* prefix operator */
97 r.append(getBracketOpen());
99 r.append(sqi.next().toString());
100 while (sqi.hasNext()) {
101 r.append(getPrefixSeparator());
102 r.append(sqi.next().toString());
105 r.append(getBracketClose());
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()) {