KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > gsf > OffsetRange


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.api.gsf;
20
21
22 /**
23  * An offset range provides a range (start, end) pair of offsets
24  * that indicate a range in a character buffer. The range represented
25  * is {@code [start,end>}, which means that the range includes the
26  * character at index=start, and ends right before the character at end.
27  * Put yet another way, the starting offset is inclusive, and the ending
28  * offset is exclusive.
29  *
30  * @author Tor Norbye
31  */

32 public class OffsetRange {
33     public static final OffsetRange NONE = new OffsetRange(0, 0);
34     private final int start;
35     private final int end;
36
37     /** Creates a new instance of OffsetRange */
38     public OffsetRange(int start, int end) {
39         assert start >= 0;
40         assert end >= start;
41
42         this.start = start;
43         this.end = end;
44     }
45
46     /** Get the start offset of offset range */
47     public int getStart() {
48         return start;
49     }
50
51     /** Get the end offset of offset range */
52     public int getEnd() {
53         return end;
54     }
55     
56     /** Get the length of the offset range */
57     public int getLength() {
58         return end-start;
59     }
60
61     public String JavaDoc toString() {
62         if (this == NONE) {
63             return "OffsetRange[NONE]";
64         } else {
65             return "OffsetRange[" + start + "," + end + ">"; // NOI18N
66
}
67     }
68
69     public boolean equals(Object JavaDoc o) {
70         if (o == null) {
71             return false;
72         }
73
74         if (getClass() != o.getClass()) {
75             return false;
76         }
77
78         final OffsetRange test = (OffsetRange)o;
79
80         if (this.start != test.start) {
81             return false;
82         }
83
84         if (this.end != test.end) {
85             return false;
86         }
87
88         return true;
89     }
90
91     public int hashCode() {
92         int hash = 7;
93
94         hash = (23 * hash) + this.start;
95         hash = (23 * hash) + this.end;
96
97         return hash;
98     }
99
100     /** Return true iff the given offset is within the bounds (or at the bounds) of the range */
101     public boolean containsInclusive(int offset) {
102         if (this == NONE) {
103             return false;
104         }
105
106         return (offset >= start) && (offset <= end);
107     }
108 }
109
Popular Tags