Lucene.Net  3.0.3
Lucene.Net is a port of the Lucene search engine library, written in C# and targeted at .NET runtime users.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Pages
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 
26  /// <summary> The term text of a Token.</summary>
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 
35  /// <summary>Returns the Token's term text.
36  ///
37  /// This method has a performance penalty
38  /// because the text is stored internally in a char[]. If
39  /// possible, use <see cref="TermBuffer()" /> and
40  /// <see cref="TermLength()" /> directly instead. If you
41  /// really need a String, use this method, which is nothing more than
42  /// a convenience call to <b>new String(token.termBuffer(), 0, token.termLength())</b>
43  /// </summary>
44  public virtual string Term
45  {
46  get
47  {
48  InitTermBuffer();
49  return new System.String(termBuffer, 0, termLength);
50  }
51  }
52 
53  /// <summary>Copies the contents of buffer, starting at offset for
54  /// length characters, into the termBuffer array.
55  /// </summary>
56  /// <param name="buffer">the buffer to copy
57  /// </param>
58  /// <param name="offset">the index in the buffer of the first character to copy
59  /// </param>
60  /// <param name="length">the number of characters to copy
61  /// </param>
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 
69  /// <summary>Copies the contents of buffer into the termBuffer array.</summary>
70  /// <param name="buffer">the buffer to copy
71  /// </param>
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 
80  /// <summary>Copies the contents of buffer, starting at offset and continuing
81  /// for length characters, into the termBuffer array.
82  /// </summary>
83  /// <param name="buffer">the buffer to copy
84  /// </param>
85  /// <param name="offset">the index in the buffer of the first character to copy
86  /// </param>
87  /// <param name="length">the number of characters to copy
88  /// </param>
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 
98  /// <summary>Returns the internal termBuffer character array which
99  /// you can then directly alter. If the array is too
100  /// small for your token, use <see cref="ResizeTermBuffer(int)" />
101  /// to increase it. After
102  /// altering the buffer be sure to call <see cref="SetTermLength" />
103  /// to record the number of valid
104  /// characters that were placed into the termBuffer.
105  /// </summary>
106  public virtual char[] TermBuffer()
107  {
108  InitTermBuffer();
109  return termBuffer;
110  }
111 
112  /// <summary>Grows the termBuffer to at least size newSize, preserving the
113  /// existing content. Note: If the next operation is to change
114  /// the contents of the term buffer use
115  /// <see cref="SetTermBuffer(char[], int, int)" />,
116  /// <see cref="SetTermBuffer(String)" />, or
117  /// <see cref="SetTermBuffer(String, int, int)" />
118  /// to optimally combine the resize with the setting of the termBuffer.
119  /// </summary>
120  /// <param name="newSize">minimum size of the new termBuffer
121  /// </param>
122  /// <returns> newly created termBuffer with length >= newSize
123  /// </returns>
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 
146  /// <summary>Allocates a buffer char[] of at least newSize, without preserving the existing content.
147  /// its always used in places that set the content
148  /// </summary>
149  /// <param name="newSize">minimum size of the buffer
150  /// </param>
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 
178  /// <summary>Return number of valid characters (length of the term)
179  /// in the termBuffer array.
180  /// </summary>
181  public virtual int TermLength()
182  {
183  return termLength;
184  }
185 
186  /// <summary>Set number of valid characters (length of the term) in
187  /// the termBuffer array. Use this to truncate the termBuffer
188  /// or to synchronize with external manipulation of the termBuffer.
189  /// Note: to grow the size of the array,
190  /// use <see cref="ResizeTermBuffer(int)" /> first.
191  /// </summary>
192  /// <param name="length">the truncated length
193  /// </param>
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 }