1
package org.apache.lucene.queryParser.standard.nodes;
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;
22
import org.apache.lucene.queryParser.core.nodes.FieldValuePairQueryNode;
23
import org.apache.lucene.queryParser.core.nodes.QueryNode;
24
import org.apache.lucene.queryParser.core.nodes.QueryNodeImpl;
25
import org.apache.lucene.queryParser.core.nodes.RangeQueryNode;
26
import org.apache.lucene.queryParser.core.parser.EscapeQuerySyntax;
27
import org.apache.lucene.queryParser.core.util.StringUtils;
30
* This class should be extended by nodes intending to represent range queries.
33
* the type of the range query bounds (lower and upper)
35
public abstract class AbstractRangeQueryNode<T extends FieldValuePairQueryNode<?>>
36
extends QueryNodeImpl implements RangeQueryNode<FieldValuePairQueryNode<?>> {
38
private static final long serialVersionUID = 4475492120315147792L;
40
private boolean lowerInclusive, upperInclusive;
43
* Constructs an {@link AbstractRangeQueryNode}, it should be invoked only by
46
protected AbstractRangeQueryNode() {
52
* Returns the field associated with this node.
54
* @return the field associated with this node
56
* @see org.apache.lucene.queryParser.core.nodes.FieldableNode
58
public CharSequence getField() {
59
CharSequence field = null;
60
T lower = getLowerBound();
61
T upper = getUpperBound();
64
field = lower.getField();
66
} else if (upper != null) {
67
field = upper.getField();
75
* Sets the field associated with this node.
77
* @param fieldName the field associated with this node
79
public void setField(CharSequence fieldName) {
80
T lower = getLowerBound();
81
T upper = getUpperBound();
84
lower.setField(fieldName);
88
upper.setField(fieldName);
94
* Returns the lower bound node.
96
* @return the lower bound node.
98
@SuppressWarnings("unchecked")
99
public T getLowerBound() {
100
return (T) getChildren().get(0);
104
* Returns the upper bound node.
106
* @return the upper bound node.
108
@SuppressWarnings("unchecked")
109
public T getUpperBound() {
110
return (T) getChildren().get(1);
114
* Returns whether the lower bound is inclusive or exclusive.
116
* @return <code>true</code> if the lower bound is inclusive, otherwise, <code>false</code>
118
public boolean isLowerInclusive() {
119
return lowerInclusive;
123
* Returns whether the upper bound is inclusive or exclusive.
125
* @return <code>true</code> if the upper bound is inclusive, otherwise, <code>false</code>
127
public boolean isUpperInclusive() {
128
return upperInclusive;
132
* Sets the lower and upper bounds.
134
* @param lower the lower bound, <code>null</code> if lower bound is open
135
* @param upper the upper bound, <code>null</code> if upper bound is open
136
* @param lowerInclusive <code>true</code> if the lower bound is inclusive, otherwise, <code>false</code>
137
* @param upperInclusive <code>true</code> if the upper bound is inclusive, otherwise, <code>false</code>
139
* @see #getLowerBound()
140
* @see #getUpperBound()
141
* @see #isLowerInclusive()
142
* @see #isUpperInclusive()
144
public void setBounds(T lower, T upper, boolean lowerInclusive,
145
boolean upperInclusive) {
147
if (lower != null && upper != null) {
148
String lowerField = StringUtils.toString(lower.getField());
149
String upperField = StringUtils.toString(upper.getField());
151
if ((upperField != null || lowerField != null)
152
&& ((upperField != null && !upperField.equals(lowerField)) || !lowerField
153
.equals(upperField))) {
154
throw new IllegalArgumentException(
155
"lower and upper bounds should have the same field name!");
158
this.lowerInclusive = lowerInclusive;
159
this.upperInclusive = upperInclusive;
161
ArrayList<QueryNode> children = new ArrayList<QueryNode>(2);
171
public CharSequence toQueryString(EscapeQuerySyntax escapeSyntaxParser) {
172
StringBuilder sb = new StringBuilder();
174
T lower = getLowerBound();
175
T upper = getUpperBound();
177
if (lowerInclusive) {
185
sb.append(lower.toQueryString(escapeSyntaxParser));
194
sb.append(upper.toQueryString(escapeSyntaxParser));
200
if (upperInclusive) {
207
return sb.toString();