pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / queryparser / src / java / org / apache / lucene / queryParser / core / nodes / AnyQueryNode.java
1 package org.apache.lucene.queryParser.core.nodes;
2
3 /**
4  * Licensed to the Apache Software Foundation (ASF) under one or more
5  * contributor license agreements.  See the NOTICE file distributed with
6  * this work for additional information regarding copyright ownership.
7  * The ASF licenses this file to You under the Apache License, Version 2.0
8  * (the "License"); you may not use this file except in compliance with
9  * the License.  You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 import java.util.List;
21
22 import org.apache.lucene.queryParser.core.parser.EscapeQuerySyntax;
23
24 /**
25  * A {@link AnyQueryNode} represents an ANY operator performed on a list of
26  * nodes.
27  */
28 public class AnyQueryNode extends AndQueryNode {
29   private static final long serialVersionUID = 1000791433562954187L;
30
31   private CharSequence field = null;
32   private int minimumMatchingmElements = 0;
33
34   /**
35    * @param clauses
36    *          - the query nodes to be or'ed
37    */
38   public AnyQueryNode(List<QueryNode> clauses, CharSequence field,
39       int minimumMatchingElements) {
40     super(clauses);
41     this.field = field;
42     this.minimumMatchingmElements = minimumMatchingElements;
43
44     if (clauses != null) {
45
46       for (QueryNode clause : clauses) {
47
48         if (clause instanceof FieldQueryNode) {
49
50           if (clause instanceof QueryNodeImpl) {
51             ((QueryNodeImpl) clause).toQueryStringIgnoreFields = true;
52           }
53
54           if (clause instanceof FieldableNode) {
55             ((FieldableNode) clause).setField(field);
56           }
57
58         }
59       }
60
61     }
62
63   }
64
65   public int getMinimumMatchingElements() {
66     return this.minimumMatchingmElements;
67   }
68
69   /**
70    * returns null if the field was not specified
71    * 
72    * @return the field
73    */
74   public CharSequence getField() {
75     return this.field;
76   }
77
78   /**
79    * returns - null if the field was not specified
80    * 
81    * @return the field as a String
82    */
83   public String getFieldAsString() {
84     if (this.field == null)
85       return null;
86     else
87       return this.field.toString();
88   }
89
90   /**
91    * @param field
92    *          - the field to set
93    */
94   public void setField(CharSequence field) {
95     this.field = field;
96   }
97
98   @Override
99   public QueryNode cloneTree() throws CloneNotSupportedException {
100     AnyQueryNode clone = (AnyQueryNode) super.cloneTree();
101
102     clone.field = this.field;
103     clone.minimumMatchingmElements = this.minimumMatchingmElements;
104
105     return clone;
106   }
107
108   @Override
109   public String toString() {
110     if (getChildren() == null || getChildren().size() == 0)
111       return "<any field='" + this.field + "'  matchelements="
112           + this.minimumMatchingmElements + "/>";
113     StringBuilder sb = new StringBuilder();
114     sb.append("<any field='" + this.field + "'  matchelements="
115         + this.minimumMatchingmElements + ">");
116     for (QueryNode clause : getChildren()) {
117       sb.append("\n");
118       sb.append(clause.toString());
119     }
120     sb.append("\n</any>");
121     return sb.toString();
122   }
123
124   @Override
125   public CharSequence toQueryString(EscapeQuerySyntax escapeSyntaxParser) {
126     String anySTR = "ANY " + this.minimumMatchingmElements;
127
128     StringBuilder sb = new StringBuilder();
129     if (getChildren() == null || getChildren().size() == 0) {
130       // no childs case
131     } else {
132       String filler = "";
133       for (QueryNode clause : getChildren()) {
134         sb.append(filler).append(clause.toQueryString(escapeSyntaxParser));
135         filler = " ";
136       }
137     }
138
139     if (isDefaultField(this.field)) {
140       return "( " + sb.toString() + " ) " + anySTR;
141     } else {
142       return this.field + ":(( " + sb.toString() + " ) " + anySTR + ")";
143     }
144   }
145
146 }