1 package org.apache.lucene.queryParser.core.util;
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 import java.util.ArrayList;
21 import java.util.List;
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;
28 * Allow joining 2 QueryNode Trees, into one.
30 public final class QueryNodeOperation {
31 private QueryNodeOperation() {
32 // Exists only to defeat instantiation.
35 private enum ANDOperation {
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
48 public final static QueryNode logicalAnd(QueryNode q1, QueryNode q2) {
54 ANDOperation op = null;
55 if (q1 instanceof AndQueryNode && q2 instanceof AndQueryNode)
56 op = ANDOperation.BOTH;
57 else if (q1 instanceof AndQueryNode)
59 else if (q1 instanceof AndQueryNode)
62 op = ANDOperation.NONE;
65 QueryNode result = null;
68 List<QueryNode> children = new ArrayList<QueryNode>();
69 children.add(q1.cloneTree());
70 children.add(q2.cloneTree());
71 result = new AndQueryNode(children);
74 result = q1.cloneTree();
75 result.add(q2.cloneTree());
78 result = q2.cloneTree();
79 result.add(q1.cloneTree());
82 result = q1.cloneTree();
83 result.add(q2.cloneTree().getChildren());
86 } catch (CloneNotSupportedException e) {
87 throw new QueryNodeError(e);