summaryrefslogtreecommitdiff
path: root/projects/net.wotonomy.foundation/src/main/java/net/wotonomy/foundation/NSRange.java
blob: 13dca2f58d84e51931dfbb10bf628614616e5977 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/*
Wotonomy: OpenStep design patterns for pure Java applications.
Copyright (C) 2000 Blacksmith, Inc.

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, see http://www.gnu.org
*/

package net.wotonomy.foundation;

/**
 * A pure java implementation of NSRange. An NSRange represents a range of
 * numbers having a starting location and spanning a length.
 *
 * @author michael@mpowers.net
 * @author $Author: cgruber $
 * @version $Revision: 920 $
 */
public class NSRange implements Cloneable {
	/**
	 * An empty range.
	 */
	public static final NSRange ZeroRange = new NSRange();

	protected int loc;
	protected int len;

	/**
	 * Default constructor produces an empty range.
	 */
	public NSRange() {
		this(0, 0);
	}

	/**
	 * Produces a range with the specified location and length.
	 */
	public NSRange(int location, int length) {
		loc = location;
		len = length;
	}

	/**
	 * Produces a range that has the same location and length as the specified
	 * range.
	 */
	public NSRange(NSRange aRange) {
		this(aRange.location(), aRange.length());
	}

	/**
	 * Returns the location of this range.
	 */
	public int location() {
		return loc;
	}

	/**
	 * Returns the length of this range.
	 */
	public int length() {
		return len;
	}

	/**
	 * Returns the maximum extent of the range. This number is one more than the
	 * last position in the range.
	 */
	public int maxRange() {
		return location() + length() - 1;
	}

	/**
	 * Returns whether this is an empty range, therefore whether the length is zero.
	 */
	public boolean isEmpty() {
		return (length() == 0);
	}

	/**
	 * Returns whether the specified location is contained within this range.
	 */
	public boolean locationInRange(int location) {
		if (location < location())
			return false;
		if (location >= maxRange())
			return false;
		return true;
	}

	/**
	 * Returns whether the specified range is equal to this range.
	 */
	public boolean isEqualToRange(NSRange aRange) {
		if (aRange == null)
			return false;
		return ((aRange.location() == location()) && (aRange.length() == length()));
	}

	/**
	 * Returns whether the specified object is equal to this range.
	 */
	public boolean equals(Object anObject) {
		if (anObject instanceof NSRange)
			return isEqualToRange((NSRange) anObject);
		return false;
	}

	/**
	 * Returns a hashCode.
	 */
	public int hashCode() {
		// TODO: Test this logic.
		return (location() << 2) & length(); // bitwise ops never my forte
	}

	/**
	 * Returns a string representation of this range.
	 */
	public String toString() {
		return "[NSRange: location = " + location() + "; length = " + length() + "]";
	}

	/**
	 * Returns the union of this range and the specified range, if any. Gaps are
	 * filled, so the result is the smallest starting position and the largest
	 * ending position.
	 */
	public NSRange rangeByUnioningRange(NSRange aRange) {
		if (aRange == null)
			return this;

		// TODO: Test this logic.
		int resultLoc = Math.min(this.location(), aRange.location());
		int resultLen = Math.max(this.location() + this.length(), aRange.location() + aRange.length()) - resultLoc;
		return new NSRange(resultLoc, resultLen);
	}

	/**
	 * Returns the intersection of this range and the specified range, if any. If no
	 * intersection, returns an empty range.
	 */
	public NSRange rangeByIntersectingRange(NSRange aRange) {
		// TODO: Test this logic.
		if (!intersectsRange(aRange))
			return ZeroRange;
		int start = Math.max(this.location(), aRange.location());
		int end = Math.min(this.location() + this.length(), aRange.location() + aRange.length());
		return new NSRange(start, end - start);
	}

	/**
	 * Returns whether the specified range overlaps at any point with this range.
	 */
	public boolean intersectsRange(NSRange aRange) {
		// TODO: Test this logic.
		if (aRange == null)
			return false;
		if ((this.location() >= aRange.location()) && (this.location() < aRange.location() + aRange.length()))
			return true;
		if ((aRange.location() >= this.location()) && (aRange.location() < this.location() + this.length()))
			return true;
		return false;
	}

	/**
	 * Returns whether this range is completely contained within the specified
	 * range.
	 */
	public boolean isSubrangeOfRange(NSRange aRange) {
		// TODO: Test this logic.
		if (aRange == null)
			return false;
		if ((this.location() >= aRange.location()) && (this.maxRange() <= aRange.maxRange()))
			return true;
		return false;
	}

	/**
	 * Eliminates any intersections between this range and the specified range. This
	 * produces two ranges, either of which may be empty. These two ranges are
	 * returned by modifying the supplied second and third parameters.
	 */
	public void subtractRange(NSRange aRange, NSMutableRange firstResult, NSMutableRange secondResult) {
		if (aRange == null)
			return;

		// TODO: Test this logic.
		// no intersection: return this and aRange without calculation
		if (!intersectsRange(aRange)) {
			if (firstResult != null) {
				firstResult.setLocation(this.location());
				firstResult.setLength(this.length());
			}
			if (secondResult != null) {
				secondResult.setLocation(aRange.location());
				secondResult.setLength(aRange.location());
			}
			return;
		}

		// TODO: Test this logic.
		// this range is completely contained by other range
		if (isSubrangeOfRange(aRange)) {
			if (firstResult != null) {
				firstResult.setLocation(aRange.location());
				firstResult.setLength(this.location() - aRange.location());
			}
			if (secondResult != null) {
				secondResult.setLocation(this.maxRange());
				secondResult.setLength(aRange.maxRange() - this.maxRange() - 1); // test this
			}
			return;
		}

		// TODO: Test this logic.
		// other range is completely contained by this range
		if (aRange.isSubrangeOfRange(this)) {
			if (firstResult != null) {
				firstResult.setLocation(this.location());
				firstResult.setLength(aRange.location() - this.location());
			}
			if (secondResult != null) {
				secondResult.setLocation(aRange.maxRange());
				secondResult.setLength(this.maxRange() - aRange.maxRange() - 1); // test this
			}
			return;
		}

		// TODO: Test this logic.
		// ranges intersect: remove only the intersection

		NSRange firstRange, secondRange;
		if (this.location() <= aRange.location()) {
			firstRange = this;
			secondRange = aRange;
		} else {
			firstRange = aRange;
			secondRange = this;
		}

		if (firstResult != null) {
			firstResult.setLocation(firstRange.location());
			firstResult.setLength(secondRange.location() - firstRange.location());
		}
		if (secondResult != null) {
			secondResult.setLocation(firstRange.maxRange());
			secondResult.setLength(secondRange.maxRange() - aRange.maxRange() - 1); // test this
		}
		return;

	}

	/**
	 * Returns a copy of this range.
	 */
	public Object clone() {
		return new NSRange(location(), length());
	}

	/**
	 * Parses a range from a string of the form "{x,y}" where x is the location and
	 * y is the length. If not parsable, an IllegalArgumentException is thrown.
	 */
	public static NSRange fromString(String aString) {
		// TODO: Test this logic.
		try {
			java.util.StringTokenizer tokens = new java.util.StringTokenizer(aString, "{,}");
			int loc = Integer.parseInt(tokens.nextToken());
			int len = Integer.parseInt(tokens.nextToken());
			return new NSRange(loc, len);
		} catch (Exception exc) {
			throw new IllegalArgumentException(exc.toString());
		}
	}

}

/*
 * $Log$ Revision 1.2 2006/02/16 13:15:00 cgruber Check in all sources in
 * eclipse-friendly maven-enabled packages.
 *
 * Revision 1.1.1.1 2000/12/21 15:47:42 mpowers Contributing wotonomy.
 *
 * Revision 1.3 2000/12/20 16:25:38 michael Added log to all files.
 *
 *
 */