add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / queryparser / src / java / org / apache / lucene / queryParser / core / nodes / ParametricRangeQueryNode.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.nodes.ParametricQueryNode.CompareOperator;
23 import org.apache.lucene.queryParser.core.parser.EscapeQuerySyntax;
24
25 /**
26  * A {@link ParametricRangeQueryNode} represents LE, LT, GE, GT, EQ, NE query.
27  * Example: date >= "2009-10-10" OR price = 200
28  */
29 public class ParametricRangeQueryNode extends QueryNodeImpl implements
30     RangeQueryNode<ParametricQueryNode> {
31
32   private static final long serialVersionUID = 7120958816535573935L;
33
34   public ParametricRangeQueryNode(ParametricQueryNode lowerBound,
35       ParametricQueryNode upperBound) {
36
37     if (upperBound.getOperator() != CompareOperator.LE
38         && upperBound.getOperator() != CompareOperator.LT) {
39       throw new IllegalArgumentException("upper bound should have "
40           + CompareOperator.LE + " or " + CompareOperator.LT);
41     }
42
43     if (lowerBound.getOperator() != CompareOperator.GE
44         && lowerBound.getOperator() != CompareOperator.GT) {
45       throw new IllegalArgumentException("lower bound should have "
46           + CompareOperator.GE + " or " + CompareOperator.GT);
47     }
48
49     if (upperBound.getField() != lowerBound.getField()
50         || (upperBound.getField() != null && !upperBound.getField().equals(
51             lowerBound.getField()))) {
52
53       throw new IllegalArgumentException(
54           "lower and upper bounds should have the same field name!");
55
56     }
57
58     allocate();
59     setLeaf(false);
60
61     add(lowerBound);
62     add(upperBound);
63
64   }
65
66   public ParametricQueryNode getUpperBound() {
67     return (ParametricQueryNode) getChildren().get(1);
68   }
69
70   public ParametricQueryNode getLowerBound() {
71     return (ParametricQueryNode) getChildren().get(0);
72   }
73
74   public CharSequence toQueryString(EscapeQuerySyntax escapeSyntaxParser) {
75     return getLowerBound().toQueryString(escapeSyntaxParser) + " AND "
76         + getUpperBound().toQueryString(escapeSyntaxParser);
77   }
78
79   public CharSequence getField() {
80     return getLowerBound().getField();
81   }
82
83   @Override
84   public String toString() {
85     StringBuilder sb = new StringBuilder("<parametricRange>\n\t");
86     sb.append(getUpperBound()).append("\n\t");
87     sb.append(getLowerBound()).append("\n");
88     sb.append("</parametricRange>\n");
89
90     return sb.toString();
91
92   }
93
94   @Override
95   public ParametricRangeQueryNode cloneTree() throws CloneNotSupportedException {
96     ParametricRangeQueryNode clone = (ParametricRangeQueryNode) super
97         .cloneTree();
98
99     // nothing to do here
100
101     return clone;
102   }
103
104   public void setField(CharSequence fieldName) {
105     List<QueryNode> children = getChildren();
106
107     if (children != null) {
108
109       for (QueryNode child : getChildren()) {
110
111         if (child instanceof FieldableNode) {
112           ((FieldableNode) child).setField(fieldName);
113         }
114
115       }
116
117     }
118
119   }
120
121   public boolean isLowerInclusive() {
122     return getUpperBound().getOperator() == CompareOperator.GE;
123   }
124   
125   public boolean isUpperInclusive() {
126     return getLowerBound().getOperator() == CompareOperator.LE;
127   }
128   
129 }