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
TermAttribute.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 System;
19 using Lucene.Net.Support;
20 using ArrayUtil = Lucene.Net.Util.ArrayUtil;
21 using Attribute = Lucene.Net.Util.Attribute;
22 
23 namespace Lucene.Net.Analysis.Tokenattributes
24 {
25 
27  [Serializable]
28  public class TermAttribute:Attribute, ITermAttribute, System.ICloneable
29  {
30  private static int MIN_BUFFER_SIZE = 10;
31 
32  private char[] termBuffer;
33  private int termLength;
34 
44  public virtual string Term
45  {
46  get
47  {
48  InitTermBuffer();
49  return new System.String(termBuffer, 0, termLength);
50  }
51  }
52 
62  public virtual void SetTermBuffer(char[] buffer, int offset, int length)
63  {
64  GrowTermBuffer(length);
65  Array.Copy(buffer, offset, termBuffer, 0, length);
66  termLength = length;
67  }
68 
72  public virtual void SetTermBuffer(System.String buffer)
73  {
74  int length = buffer.Length;
75  GrowTermBuffer(length);
76  TextSupport.GetCharsFromString(buffer, 0, length, termBuffer, 0);
77  termLength = length;
78  }
79 
89  public virtual void SetTermBuffer(System.String buffer, int offset, int length)
90  {
91  System.Diagnostics.Debug.Assert(offset <= buffer.Length);
92  System.Diagnostics.Debug.Assert(offset + length <= buffer.Length);
93  GrowTermBuffer(length);
94  TextSupport.GetCharsFromString(buffer, offset, offset + length, termBuffer, 0);
95  termLength = length;
96  }
97 
106  public virtual char[] TermBuffer()
107  {
108  InitTermBuffer();
109  return termBuffer;
110  }
111 
124  public virtual char[] ResizeTermBuffer(int newSize)
125  {
126  if (termBuffer == null)
127  {
128  // The buffer is always at least MIN_BUFFER_SIZE
129  termBuffer = new char[ArrayUtil.GetNextSize(newSize < MIN_BUFFER_SIZE?MIN_BUFFER_SIZE:newSize)];
130  }
131  else
132  {
133  if (termBuffer.Length < newSize)
134  {
135  // Not big enough; create a new array with slight
136  // over allocation and preserve content
137  char[] newCharBuffer = new char[ArrayUtil.GetNextSize(newSize)];
138  Array.Copy(termBuffer, 0, newCharBuffer, 0, termBuffer.Length);
139  termBuffer = newCharBuffer;
140  }
141  }
142  return termBuffer;
143  }
144 
145 
151  private void GrowTermBuffer(int newSize)
152  {
153  if (termBuffer == null)
154  {
155  // The buffer is always at least MIN_BUFFER_SIZE
156  termBuffer = new char[ArrayUtil.GetNextSize(newSize < MIN_BUFFER_SIZE?MIN_BUFFER_SIZE:newSize)];
157  }
158  else
159  {
160  if (termBuffer.Length < newSize)
161  {
162  // Not big enough; create a new array with slight
163  // over allocation:
164  termBuffer = new char[ArrayUtil.GetNextSize(newSize)];
165  }
166  }
167  }
168 
169  private void InitTermBuffer()
170  {
171  if (termBuffer == null)
172  {
173  termBuffer = new char[ArrayUtil.GetNextSize(MIN_BUFFER_SIZE)];
174  termLength = 0;
175  }
176  }
177 
181  public virtual int TermLength()
182  {
183  return termLength;
184  }
185 
194  public virtual void SetTermLength(int length)
195  {
196  InitTermBuffer();
197  if (length > termBuffer.Length)
198  throw new System.ArgumentException("length " + length + " exceeds the size of the termBuffer (" + termBuffer.Length + ")");
199  termLength = length;
200  }
201 
202  public override int GetHashCode()
203  {
204  InitTermBuffer();
205  int code = termLength;
206  code = code * 31 + ArrayUtil.HashCode(termBuffer, 0, termLength);
207  return code;
208  }
209 
210  public override void Clear()
211  {
212  termLength = 0;
213  }
214 
215  public override System.Object Clone()
216  {
217  TermAttribute t = (TermAttribute) base.Clone();
218  // Do a deep clone
219  if (termBuffer != null)
220  {
221  t.termBuffer = new char[termBuffer.Length];
222  termBuffer.CopyTo(t.termBuffer, 0);
223  }
224  return t;
225  }
226 
227  public override bool Equals(System.Object other)
228  {
229  if (other == this)
230  {
231  return true;
232  }
233 
234  if (other is ITermAttribute)
235  {
236  InitTermBuffer();
237  TermAttribute o = ((TermAttribute) other);
238  o.InitTermBuffer();
239 
240  if (termLength != o.termLength)
241  return false;
242  for (int i = 0; i < termLength; i++)
243  {
244  if (termBuffer[i] != o.termBuffer[i])
245  {
246  return false;
247  }
248  }
249  return true;
250  }
251 
252  return false;
253  }
254 
255  public override System.String ToString()
256  {
257  InitTermBuffer();
258  return "term=" + new System.String(termBuffer, 0, termLength);
259  }
260 
261  public override void CopyTo(Attribute target)
262  {
263  InitTermBuffer();
264  ITermAttribute t = (ITermAttribute) target;
265  t.SetTermBuffer(termBuffer, 0, termLength);
266  }
267  }
268 }