add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / queryparser / src / java / org / apache / lucene / queryParser / core / util / QueryNodeOperation.java
1 package org.apache.lucene.queryParser.core.util;
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.ArrayList;
21 import java.util.List;
22
23 import org.apache.lucene.queryParser.core.QueryNodeError;
24 import org.apache.lucene.queryParser.core.nodes.AndQueryNode;
25 import org.apache.lucene.queryParser.core.nodes.QueryNode;
26
27 /**
28  * Allow joining 2 QueryNode Trees, into one.
29  */
30 public final class QueryNodeOperation {
31   private QueryNodeOperation() {
32     // Exists only to defeat instantiation.
33   }
34
35   private enum ANDOperation {
36     BOTH, Q1, Q2, NONE
37   }
38
39   /**
40    * perform a logical and of 2 QueryNode trees. if q1 and q2 are ANDQueryNode
41    * nodes it uses head Node from q1 and adds the children of q2 to q1 if q1 is
42    * a AND node and q2 is not, add q2 as a child of the head node of q1 if q2 is
43    * a AND node and q1 is not, add q1 as a child of the head node of q2 if q1
44    * and q2 are not ANDQueryNode nodes, create a AND node and make q1 and q2
45    * children of that node if q1 or q2 is null it returns the not null node if
46    * q1 = q2 = null it returns null
47    */
48   public final static QueryNode logicalAnd(QueryNode q1, QueryNode q2) {
49     if (q1 == null)
50       return q2;
51     if (q2 == null)
52       return q1;
53
54     ANDOperation op = null;
55     if (q1 instanceof AndQueryNode && q2 instanceof AndQueryNode)
56       op = ANDOperation.BOTH;
57     else if (q1 instanceof AndQueryNode)
58       op = ANDOperation.Q1;
59     else if (q1 instanceof AndQueryNode)
60       op = ANDOperation.Q2;
61     else
62       op = ANDOperation.NONE;
63
64     try {
65       QueryNode result = null;
66       switch (op) {
67       case NONE:
68         List<QueryNode> children = new ArrayList<QueryNode>();
69         children.add(q1.cloneTree());
70         children.add(q2.cloneTree());
71         result = new AndQueryNode(children);
72         return result;
73       case Q1:
74         result = q1.cloneTree();
75         result.add(q2.cloneTree());
76         return result;
77       case Q2:
78         result = q2.cloneTree();
79         result.add(q1.cloneTree());
80         return result;
81       case BOTH:
82         result = q1.cloneTree();
83         result.add(q2.cloneTree().getChildren());
84         return result;
85       }
86     } catch (CloneNotSupportedException e) {
87       throw new QueryNodeError(e);
88     }
89
90     return null;
91
92   }
93
94 }