add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / queryparser / src / java / org / apache / lucene / queryParser / core / nodes / OrQueryNode.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.Iterator;
21 import java.util.List;
22
23 import org.apache.lucene.queryParser.core.parser.EscapeQuerySyntax;
24
25 /**
26  * A {@link OrQueryNode} represents an OR boolean operation performed on a list
27  * of nodes.
28  * 
29  */
30 public class OrQueryNode extends BooleanQueryNode {
31
32   private static final long serialVersionUID = -3692323307688017852L;
33
34   /**
35    * @param clauses
36    *          - the query nodes to be or'ed
37    */
38   public OrQueryNode(List<QueryNode> clauses) {
39     super(clauses);
40     if ((clauses == null) || (clauses.size() == 0)) {
41       throw new IllegalArgumentException(
42           "OR query must have at least one clause");
43     }
44   }
45
46   @Override
47   public String toString() {
48     if (getChildren() == null || getChildren().size() == 0)
49       return "<boolean operation='or'/>";
50     StringBuilder sb = new StringBuilder();
51     sb.append("<boolean operation='or'>");
52     for (QueryNode child : getChildren()) {
53       sb.append("\n");
54       sb.append(child.toString());
55
56     }
57     sb.append("\n</boolean>");
58     return sb.toString();
59   }
60
61   @Override
62   public CharSequence toQueryString(EscapeQuerySyntax escapeSyntaxParser) {
63     if (getChildren() == null || getChildren().size() == 0)
64       return "";
65
66     StringBuilder sb = new StringBuilder();
67     String filler = "";
68     for (Iterator<QueryNode> it = getChildren().iterator(); it.hasNext();) {
69       sb.append(filler).append(it.next().toQueryString(escapeSyntaxParser));
70       filler = " OR ";
71     }
72
73     // in case is root or the parent is a group node avoid parenthesis
74     if ((getParent() != null && getParent() instanceof GroupQueryNode)
75         || isRoot())
76       return sb.toString();
77     else
78       return "( " + sb.toString() + " )";
79   }
80 }