pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / src / java / org / apache / lucene / analysis / tokenattributes / OffsetAttributeImpl.java
1 package org.apache.lucene.analysis.tokenattributes;
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.io.Serializable;
21
22 import org.apache.lucene.util.AttributeImpl;
23
24 /**
25  * The start and end character offset of a Token. 
26  */
27 public class OffsetAttributeImpl extends AttributeImpl implements OffsetAttribute, Cloneable, Serializable {
28   private int startOffset;
29   private int endOffset;
30
31   /** Returns this Token's starting offset, the position of the first character
32   corresponding to this token in the source text.
33
34   Note that the difference between endOffset() and startOffset() may not be
35   equal to termText.length(), as the term text may have been altered by a
36   stemmer or some other filter. */
37   public int startOffset() {
38     return startOffset;
39   }
40
41   
42   /** Set the starting and ending offset.
43     @see #startOffset() and #endOffset()*/
44   public void setOffset(int startOffset, int endOffset) {
45     this.startOffset = startOffset;
46     this.endOffset = endOffset;
47   }
48   
49
50   /** Returns this Token's ending offset, one greater than the position of the
51   last character corresponding to this token in the source text. The length
52   of the token in the source text is (endOffset - startOffset). */
53   public int endOffset() {
54     return endOffset;
55   }
56
57
58   @Override
59   public void clear() {
60     startOffset = 0;
61     endOffset = 0;
62   }
63   
64   @Override
65   public boolean equals(Object other) {
66     if (other == this) {
67       return true;
68     }
69     
70     if (other instanceof OffsetAttributeImpl) {
71       OffsetAttributeImpl o = (OffsetAttributeImpl) other;
72       return o.startOffset == startOffset && o.endOffset == endOffset;
73     }
74     
75     return false;
76   }
77
78   @Override
79   public int hashCode() {
80     int code = startOffset;
81     code = code * 31 + endOffset;
82     return code;
83   } 
84   
85   @Override
86   public void copyTo(AttributeImpl target) {
87     OffsetAttribute t = (OffsetAttribute) target;
88     t.setOffset(startOffset, endOffset);
89   }  
90 }