add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / queryparser / src / java / org / apache / lucene / queryParser / standard / nodes / NumericQueryNode.java
1 package org.apache.lucene.queryParser.standard.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.text.NumberFormat;
21 import java.util.Locale;
22
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;
27
28 /**
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}.
32  * 
33  * @see org.apache.lucene.queryParser.standard.config.NumericConfig
34  */
35 public class NumericQueryNode extends QueryNodeImpl implements
36     FieldValuePairQueryNode<Number> {
37   
38   private static final long serialVersionUID = -1969102979874574778L;
39
40   private NumberFormat numberFormat;
41   
42   private CharSequence field;
43   
44   private Number value;
45   
46   /**
47    * Creates a {@link NumericQueryNode} object using the given field,
48    * {@link Number} value and {@link NumberFormat} used to convert the value to
49    * {@link String}.
50    * 
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}
54    */
55   public NumericQueryNode(CharSequence field, Number value,
56       NumberFormat numberFormat) {
57     
58     super();
59     
60     setNumberFormat(numberFormat);
61     setField(field);
62     setValue(value);
63     
64   }
65   
66   /**
67    * Returns the field associated with this node.
68    * 
69    * @return the field associated with this node
70    */
71   public CharSequence getField() {
72     return this.field;
73   }
74   
75   /**
76    * Sets the field associated with this node.
77    * 
78    * @param fieldName the field associated with this node
79    */
80   public void setField(CharSequence fieldName) {
81     this.field = fieldName;
82   }
83   
84   /**
85    * This method is used to get the value converted to {@link String} and
86    * escaped using the given {@link EscapeQuerySyntax}.
87    * 
88    * @param escaper the {@link EscapeQuerySyntax} used to escape the value {@link String}
89    * 
90    * @return the value converte to {@link String} and escaped
91    */
92   protected CharSequence getTermEscaped(EscapeQuerySyntax escaper) {
93     return escaper.escape(NumberFormat.getNumberInstance().format(this.value),
94         Locale.ENGLISH, Type.NORMAL);
95   }
96   
97   public CharSequence toQueryString(EscapeQuerySyntax escapeSyntaxParser) {
98     if (isDefaultField(this.field)) {
99       return getTermEscaped(escapeSyntaxParser);
100     } else {
101       return this.field + ":" + getTermEscaped(escapeSyntaxParser);
102     }
103   }
104   
105   /**
106    * Sets the {@link NumberFormat} used to convert the value to {@link String}.
107    * 
108    * @param format the {@link NumberFormat} used to convert the value to {@link String}
109    */
110   public void setNumberFormat(NumberFormat format) {
111     this.numberFormat = format;
112   }
113   
114   /**
115    * Returns the {@link NumberFormat} used to convert the value to {@link String}.
116    * 
117    * @return the {@link NumberFormat} used to convert the value to {@link String}
118    */
119   public NumberFormat getNumberFormat() {
120     return this.numberFormat;
121   }
122   
123   /**
124    * Returns the numeric value as {@link Number}.
125    * 
126    * @return the numeric value
127    */
128   public Number getValue() {
129     return value;
130   }
131   
132   /**
133    * Sets the numeric value.
134    * 
135    * @param value the numeric value
136    */
137   public void setValue(Number value) {
138     this.value = value;
139   }
140   
141   @Override
142   public String toString() {
143     return "<numeric field='" + this.field + "' number='"
144         + numberFormat.format(value) + "'/>";
145   }
146   
147 }