// ----------------------------------------------------------------------- // // // Licensed to the Apache Software Foundation (ASF) under one or more // contributor license agreements. See the NOTICE file distributed with // this work for additional information regarding copyright ownership. // The ASF licenses this file to You under the Apache License, Version 2.0 // (the "License"); you may not use this file except in compliance with // the License. You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // // // ----------------------------------------------------------------------- namespace Lucene.Net.Analysis.TokenAttributes { using System.Diagnostics.CodeAnalysis; using Lucene.Net.Util; /// /// The start and end character offset of a . /// [SuppressMessage("Microsoft.Naming", "CA1711:IdentifiersShouldNotHaveIncorrectSuffix", Justification = "The class was called Attribute in Java. It would be fun to call it Annotation. However, " + "its probably best to try to honor the correlating names when possible.")] public class OffsetAttribute : AttributeBase, IOffsetAttribute { /// /// Initializes a new instance of the class. /// public OffsetAttribute() : this(0, 0) { } /// /// Initializes a new instance of the class. /// /// The start. /// The end. public OffsetAttribute(int start, int end) { this.SetOffset(start, end); } /// /// Gets or sets the start of the offset. /// /// The offset start. /// /// The difference between the offset's start and end /// may not be equal to the length of the term text. /// The term text may have been altered by a stemmer /// some other filter /// public int OffsetStart { get; set; } /// /// Gets or sets the end of the offset. /// /// The offset end. /// /// This will be the end of the offset. Which is one greater than /// the position of the last character corresponding to this token /// in the source text. The length of the token in the source text /// is the OffsetEnd - OffsetStart /// public int OffsetEnd { get; set; } /// /// Clears the instance. /// public override void Clear() { this.SetOffset(0, 0); } /// /// Copies the values to the target. /// /// The attribute base. public override void CopyTo(AttributeBase attributeBase) { IOffsetAttribute attribute = (IOffsetAttribute)attributeBase; attribute.SetOffset(this.OffsetStart, this.OffsetEnd); } /// /// Determines whether the specified is equal to this instance. /// /// The to compare with this instance. /// /// true if the specified is equal to this instance; otherwise, false. /// public override bool Equals(object obj) { if (this == obj) return true; OffsetAttribute y = obj as OffsetAttribute; return y != null && y.OffsetStart == this.OffsetStart && y.OffsetEnd == this.OffsetEnd; } /// /// Returns a hash code for this instance. /// /// /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table. /// public override int GetHashCode() { int code = this.OffsetStart; code = (code * 31) + this.OffsetEnd; return code; } /// /// Sets the offset. /// /// The start of the offset. /// The end of the offset. public void SetOffset(int offsetStart, int offsetEnd) { this.OffsetStart = offsetStart; this.OffsetEnd = offsetEnd; } } }