Lucene.Net  3.0.3
Lucene.Net is a .NET port of the Java Lucene Indexing Library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties
NumericTokenStream.cs
Go to the documentation of this file.
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 using Lucene.Net.Analysis.Tokenattributes;
19 using Lucene.Net.Search;
20 using AttributeSource = Lucene.Net.Util.AttributeSource;
21 using NumericUtils = Lucene.Net.Util.NumericUtils;
22 using NumericField = Lucene.Net.Documents.NumericField;
23 // javadocs
24 
25 namespace Lucene.Net.Analysis
26 {
27 
84  public sealed class NumericTokenStream : TokenStream
85  {
86  private void InitBlock()
87  {
88  termAtt = AddAttribute<ITermAttribute>();
89  typeAtt = AddAttribute<ITypeAttribute>();
90  posIncrAtt = AddAttribute<IPositionIncrementAttribute>();
91  }
92 
94  public const System.String TOKEN_TYPE_FULL_PREC = "fullPrecNumeric";
95 
97  public const System.String TOKEN_TYPE_LOWER_PREC = "lowerPrecNumeric";
98 
103  public NumericTokenStream():this(NumericUtils.PRECISION_STEP_DEFAULT)
104  {
105  }
106 
111  public NumericTokenStream(int precisionStep):base()
112  {
113  InitBlock();
114  this.precisionStep = precisionStep;
115  if (precisionStep < 1)
116  throw new System.ArgumentException("precisionStep must be >=1");
117  }
118 
124  public NumericTokenStream(AttributeSource source, int precisionStep):base(source)
125  {
126  InitBlock();
127  this.precisionStep = precisionStep;
128  if (precisionStep < 1)
129  throw new System.ArgumentException("precisionStep must be >=1");
130  }
131 
138  public NumericTokenStream(AttributeFactory factory, int precisionStep):base(factory)
139  {
140  InitBlock();
141  this.precisionStep = precisionStep;
142  if (precisionStep < 1)
143  throw new System.ArgumentException("precisionStep must be >=1");
144  }
145 
152  public NumericTokenStream SetLongValue(long value_Renamed)
153  {
154  this.value_Renamed = value_Renamed;
155  valSize = 64;
156  shift = 0;
157  return this;
158  }
159 
166  public NumericTokenStream SetIntValue(int value_Renamed)
167  {
168  this.value_Renamed = (long) value_Renamed;
169  valSize = 32;
170  shift = 0;
171  return this;
172  }
173 
180  public NumericTokenStream SetDoubleValue(double value_Renamed)
181  {
182  this.value_Renamed = NumericUtils.DoubleToSortableLong(value_Renamed);
183  valSize = 64;
184  shift = 0;
185  return this;
186  }
187 
194  public NumericTokenStream SetFloatValue(float value_Renamed)
195  {
196  this.value_Renamed = (long) NumericUtils.FloatToSortableInt(value_Renamed);
197  valSize = 32;
198  shift = 0;
199  return this;
200  }
201 
202  // @Override
203  public override void Reset()
204  {
205  if (valSize == 0)
206  throw new System.SystemException("call set???Value() before usage");
207  shift = 0;
208  }
209 
210  protected override void Dispose(bool disposing)
211  {
212  // Do nothing.
213  }
214 
215  // @Override
216  public override bool IncrementToken()
217  {
218  if (valSize == 0)
219  throw new System.SystemException("call set???Value() before usage");
220  if (shift >= valSize)
221  return false;
222 
223  ClearAttributes();
224  char[] buffer;
225  switch (valSize)
226  {
227 
228  case 64:
229  buffer = termAtt.ResizeTermBuffer(NumericUtils.BUF_SIZE_LONG);
230  termAtt.SetTermLength(NumericUtils.LongToPrefixCoded(value_Renamed, shift, buffer));
231  break;
232 
233 
234  case 32:
235  buffer = termAtt.ResizeTermBuffer(NumericUtils.BUF_SIZE_INT);
236  termAtt.SetTermLength(NumericUtils.IntToPrefixCoded((int) value_Renamed, shift, buffer));
237  break;
238 
239 
240  default:
241  // should not happen
242  throw new System.ArgumentException("valSize must be 32 or 64");
243 
244  }
245 
246  typeAtt.Type = (shift == 0)?TOKEN_TYPE_FULL_PREC:TOKEN_TYPE_LOWER_PREC;
247  posIncrAtt.PositionIncrement = (shift == 0)?1:0;
248  shift += precisionStep;
249  return true;
250  }
251 
252  // @Override
253  public override System.String ToString()
254  {
255  System.Text.StringBuilder sb = new System.Text.StringBuilder("(numeric,valSize=").Append(valSize);
256  sb.Append(",precisionStep=").Append(precisionStep).Append(')');
257  return sb.ToString();
258  }
259 
260  // members
261  private ITermAttribute termAtt;
262  private ITypeAttribute typeAtt;
263  private IPositionIncrementAttribute posIncrAtt;
264 
265  private int shift = 0, valSize = 0; // valSize==0 means not initialized
266  private readonly int precisionStep;
267 
268  private long value_Renamed = 0L;
269  }
270 }