add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / queryparser / src / java / org / apache / lucene / queryParser / standard / nodes / NumericRangeQueryNode.java
1 package org.apache.lucene.queryParser.standard.nodes;
2
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;
8
9 /**
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
16  * 
17  * http://www.apache.org/licenses/LICENSE-2.0
18  * 
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
23  * the License.
24  */
25
26 /**
27  * This query node represents a range query composed by {@link NumericQueryNode}
28  * bounds, which means the bound values are {@link Number}s.
29  * 
30  * @see NumericQueryNode
31  * @see AbstractRangeQueryNode
32  */
33 public class NumericRangeQueryNode extends
34     AbstractRangeQueryNode<NumericQueryNode> {
35   
36   public NumericConfig numericConfig; 
37   
38   /**
39    * Constructs a {@link NumericRangeQueryNode} object using the given
40    * {@link NumericQueryNode} as its bounds and {@link NumericConfig}.
41    * 
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
47    * 
48    * @see #setBounds(NumericQueryNode, NumericQueryNode, boolean, boolean, NumericConfig)
49    */
50   public NumericRangeQueryNode(NumericQueryNode lower, NumericQueryNode upper,
51       boolean lowerInclusive, boolean upperInclusive, NumericConfig numericConfig) throws QueryNodeException {
52     setBounds(lower, upper, lowerInclusive, upperInclusive, numericConfig);
53   }
54   
55   private static NumericField.DataType getNumericDataType(Number number) throws QueryNodeException {
56     
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;
65     } else {
66       throw new QueryNodeException(
67           new MessageImpl(
68               QueryParserMessages.NUMBER_CLASS_NOT_SUPPORTED_BY_NUMERIC_RANGE_QUERY,
69               number.getClass()));
70     }
71     
72   }
73   
74   /**
75    * Sets the upper and lower bounds of this range query node and the
76    * {@link NumericConfig} associated with these bounds.
77    * 
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
83    * 
84    */
85   public void setBounds(NumericQueryNode lower, NumericQueryNode upper,
86       boolean lowerInclusive, boolean upperInclusive, NumericConfig numericConfig) throws QueryNodeException {
87     
88     if (numericConfig == null) {
89       throw new IllegalArgumentException("numericConfig cannot be null!");
90     }
91     
92     NumericField.DataType lowerNumberType, upperNumberType;
93     
94     if (lower != null && lower.getValue() != null) {
95       lowerNumberType = getNumericDataType(lower.getValue());
96     } else {
97       lowerNumberType = null;
98     }
99     
100     if (upper != null && upper.getValue() != null) {
101       upperNumberType = getNumericDataType(upper.getValue());
102     } else {
103       upperNumberType = null;
104     }
105     
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());
111     }
112     
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());
118     }
119     
120     super.setBounds(lower, upper, lowerInclusive, upperInclusive);
121     this.numericConfig = numericConfig;
122     
123   }
124   
125   /**
126    * Returns the {@link NumericConfig} associated with the lower and upper bounds.
127    * 
128    * @return the {@link NumericConfig} associated with the lower and upper bounds
129    */
130   public NumericConfig getNumericConfig() {
131     return this.numericConfig;
132   }
133   
134   @Override
135   public String toString() {
136     StringBuilder sb = new StringBuilder("<numericRange lowerInclusive='");
137     
138     sb.append(isLowerInclusive()).append("' upperInclusive='").append(
139         isUpperInclusive()).append(
140         "' precisionStep='" + numericConfig.getPrecisionStep()).append(
141         "' type='" + numericConfig.getType()).append("'>\n");
142     
143     sb.append(getLowerBound()).append('\n');
144     sb.append(getUpperBound()).append('\n');
145     sb.append("</numericRange>");
146     
147     return sb.toString();
148     
149   }
150   
151 }