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
OffsetAttribute.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 Attribute = Lucene.Net.Util.Attribute;
20 
21 namespace Lucene.Net.Analysis.Tokenattributes
22 {
23 
24  /// <summary> The start and end character offset of a Token. </summary>
25  [Serializable]
26  public class OffsetAttribute:Attribute, IOffsetAttribute, System.ICloneable
27  {
28  private int startOffset;
29  private int endOffset;
30 
31  /// <summary>Returns this Token's starting offset, the position of the first character
32  /// corresponding to this token in the source text.
33  /// Note that the difference between endOffset() and startOffset() may not be
34  /// equal to termText.length(), as the term text may have been altered by a
35  /// stemmer or some other filter.
36  /// </summary>
37  public virtual int StartOffset
38  {
39  get { return startOffset; }
40  }
41 
42 
43  /// <summary>Set the starting and ending offset.
44  /// See StartOffset() and EndOffset()
45  /// </summary>
46  public virtual void SetOffset(int startOffset, int endOffset)
47  {
48  this.startOffset = startOffset;
49  this.endOffset = endOffset;
50  }
51 
52 
53  /// <summary>Returns this Token's ending offset, one greater than the position of the
54  /// last character corresponding to this token in the source text. The length
55  /// of the token in the source text is (endOffset - startOffset).
56  /// </summary>
57  public virtual int EndOffset
58  {
59  get { return endOffset; }
60  }
61 
62 
63  public override void Clear()
64  {
65  startOffset = 0;
66  endOffset = 0;
67  }
68 
69  public override bool Equals(System.Object other)
70  {
71  if (other == this)
72  {
73  return true;
74  }
75 
76  if (other is OffsetAttribute)
77  {
78  OffsetAttribute o = (OffsetAttribute) other;
79  return o.startOffset == startOffset && o.endOffset == endOffset;
80  }
81 
82  return false;
83  }
84 
85  public override int GetHashCode()
86  {
87  int code = startOffset;
88  code = code * 31 + endOffset;
89  return code;
90  }
91 
92  public override void CopyTo(Attribute target)
93  {
95  t.SetOffset(startOffset, endOffset);
96  }
97 
98  override public System.Object Clone()
99  {
100  OffsetAttribute impl = new OffsetAttribute();
101  impl.endOffset = endOffset;
102  impl.startOffset = startOffset;
103  return impl;
104  }
105  }
106 }