1 package org.apache.lucene.queryParser.standard.nodes;
3 import org.apache.lucene.document.NumericField;
4 import org.apache.lucene.messages.MessageImpl;
5 import org.apache.lucene.queryParser.core.QueryNodeException;
6 import org.apache.lucene.queryParser.core.messages.QueryParserMessages;
7 import org.apache.lucene.queryParser.standard.config.NumericConfig;
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed with this
12 * work for additional information regarding copyright ownership. The ASF
13 * licenses this file to You under the Apache License, Version 2.0 (the
14 * "License"); you may not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
17 * http://www.apache.org/licenses/LICENSE-2.0
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
21 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
22 * License for the specific language governing permissions and limitations under
27 * This query node represents a range query composed by {@link NumericQueryNode}
28 * bounds, which means the bound values are {@link Number}s.
30 * @see NumericQueryNode
31 * @see AbstractRangeQueryNode
33 public class NumericRangeQueryNode extends
34 AbstractRangeQueryNode<NumericQueryNode> {
36 public NumericConfig numericConfig;
39 * Constructs a {@link NumericRangeQueryNode} object using the given
40 * {@link NumericQueryNode} as its bounds and {@link NumericConfig}.
42 * @param lower the lower bound
43 * @param upper the upper bound
44 * @param lowerInclusive <code>true</code> if the lower bound is inclusive, otherwise, <code>false</code>
45 * @param upperInclusive <code>true</code> if the upper bound is inclusive, otherwise, <code>false</code>
46 * @param numericConfig the {@link NumericConfig} that represents associated with the upper and lower bounds
48 * @see #setBounds(NumericQueryNode, NumericQueryNode, boolean, boolean, NumericConfig)
50 public NumericRangeQueryNode(NumericQueryNode lower, NumericQueryNode upper,
51 boolean lowerInclusive, boolean upperInclusive, NumericConfig numericConfig) throws QueryNodeException {
52 setBounds(lower, upper, lowerInclusive, upperInclusive, numericConfig);
55 private static NumericField.DataType getNumericDataType(Number number) throws QueryNodeException {
57 if (number instanceof Long) {
58 return NumericField.DataType.LONG;
59 } else if (number instanceof Integer) {
60 return NumericField.DataType.INT;
61 } else if (number instanceof Double) {
62 return NumericField.DataType.DOUBLE;
63 } else if (number instanceof Float) {
64 return NumericField.DataType.FLOAT;
66 throw new QueryNodeException(
68 QueryParserMessages.NUMBER_CLASS_NOT_SUPPORTED_BY_NUMERIC_RANGE_QUERY,
75 * Sets the upper and lower bounds of this range query node and the
76 * {@link NumericConfig} associated with these bounds.
78 * @param lower the lower bound
79 * @param upper the upper bound
80 * @param lowerInclusive <code>true</code> if the lower bound is inclusive, otherwise, <code>false</code>
81 * @param upperInclusive <code>true</code> if the upper bound is inclusive, otherwise, <code>false</code>
82 * @param numericConfig the {@link NumericConfig} that represents associated with the upper and lower bounds
85 public void setBounds(NumericQueryNode lower, NumericQueryNode upper,
86 boolean lowerInclusive, boolean upperInclusive, NumericConfig numericConfig) throws QueryNodeException {
88 if (numericConfig == null) {
89 throw new IllegalArgumentException("numericConfig cannot be null!");
92 NumericField.DataType lowerNumberType, upperNumberType;
94 if (lower != null && lower.getValue() != null) {
95 lowerNumberType = getNumericDataType(lower.getValue());
97 lowerNumberType = null;
100 if (upper != null && upper.getValue() != null) {
101 upperNumberType = getNumericDataType(upper.getValue());
103 upperNumberType = null;
106 if (lowerNumberType != null
107 && !lowerNumberType.equals(numericConfig.getType())) {
108 throw new IllegalArgumentException(
109 "lower value's type should be the same as numericConfig type: "
110 + lowerNumberType + " != " + numericConfig.getType());
113 if (upperNumberType != null
114 && !upperNumberType.equals(numericConfig.getType())) {
115 throw new IllegalArgumentException(
116 "upper value's type should be the same as numericConfig type: "
117 + upperNumberType + " != " + numericConfig.getType());
120 super.setBounds(lower, upper, lowerInclusive, upperInclusive);
121 this.numericConfig = numericConfig;
126 * Returns the {@link NumericConfig} associated with the lower and upper bounds.
128 * @return the {@link NumericConfig} associated with the lower and upper bounds
130 public NumericConfig getNumericConfig() {
131 return this.numericConfig;
135 public String toString() {
136 StringBuilder sb = new StringBuilder("<numericRange lowerInclusive='");
138 sb.append(isLowerInclusive()).append("' upperInclusive='").append(
139 isUpperInclusive()).append(
140 "' precisionStep='" + numericConfig.getPrecisionStep()).append(
141 "' type='" + numericConfig.getType()).append("'>\n");
143 sb.append(getLowerBound()).append('\n');
144 sb.append(getUpperBound()).append('\n');
145 sb.append("</numericRange>");
147 return sb.toString();