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.text.NumberFormat;
21 import java.util.Locale;
23 import org.apache.lucene.queryParser.core.nodes.FieldValuePairQueryNode;
24 import org.apache.lucene.queryParser.core.nodes.QueryNodeImpl;
25 import org.apache.lucene.queryParser.core.parser.EscapeQuerySyntax;
26 import org.apache.lucene.queryParser.core.parser.EscapeQuerySyntax.Type;
29 * This query node represents a field query that holds a numeric value. It is
30 * similar to {@link org.apache.lucene.queryParser.core.nodes.FieldQueryNode},
31 * however the {@link #getValue()} returns a {@link Number}.
33 * @see org.apache.lucene.queryParser.standard.config.NumericConfig
35 public class NumericQueryNode extends QueryNodeImpl implements
36 FieldValuePairQueryNode<Number> {
38 private static final long serialVersionUID = -1969102979874574778L;
40 private NumberFormat numberFormat;
42 private CharSequence field;
47 * Creates a {@link NumericQueryNode} object using the given field,
48 * {@link Number} value and {@link NumberFormat} used to convert the value to
51 * @param field the field associated with this query node
52 * @param value the value hold by this node
53 * @param numberFormat the {@link NumberFormat} used to convert the value to {@link String}
55 public NumericQueryNode(CharSequence field, Number value,
56 NumberFormat numberFormat) {
60 setNumberFormat(numberFormat);
67 * Returns the field associated with this node.
69 * @return the field associated with this node
71 public CharSequence getField() {
76 * Sets the field associated with this node.
78 * @param fieldName the field associated with this node
80 public void setField(CharSequence fieldName) {
81 this.field = fieldName;
85 * This method is used to get the value converted to {@link String} and
86 * escaped using the given {@link EscapeQuerySyntax}.
88 * @param escaper the {@link EscapeQuerySyntax} used to escape the value {@link String}
90 * @return the value converte to {@link String} and escaped
92 protected CharSequence getTermEscaped(EscapeQuerySyntax escaper) {
93 return escaper.escape(NumberFormat.getNumberInstance().format(this.value),
94 Locale.ENGLISH, Type.NORMAL);
97 public CharSequence toQueryString(EscapeQuerySyntax escapeSyntaxParser) {
98 if (isDefaultField(this.field)) {
99 return getTermEscaped(escapeSyntaxParser);
101 return this.field + ":" + getTermEscaped(escapeSyntaxParser);
106 * Sets the {@link NumberFormat} used to convert the value to {@link String}.
108 * @param format the {@link NumberFormat} used to convert the value to {@link String}
110 public void setNumberFormat(NumberFormat format) {
111 this.numberFormat = format;
115 * Returns the {@link NumberFormat} used to convert the value to {@link String}.
117 * @return the {@link NumberFormat} used to convert the value to {@link String}
119 public NumberFormat getNumberFormat() {
120 return this.numberFormat;
124 * Returns the numeric value as {@link Number}.
126 * @return the numeric value
128 public Number getValue() {
133 * Sets the numeric value.
135 * @param value the numeric value
137 public void setValue(Number value) {
142 public String toString() {
143 return "<numeric field='" + this.field + "' number='"
144 + numberFormat.format(value) + "'/>";