01: /*
02: * Copyright 2004 Brian S O'Neill
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16:
17: package org.cojen.classfile;
18:
19: /**
20: *
21: * @author Brian S O'Neill
22: */
23: public class LocationRangeImpl implements LocationRange {
24: private final Location mStart;
25: private final Location mEnd;
26:
27: public LocationRangeImpl(Location a, Location b) {
28: if (a.compareTo(b) <= 0) {
29: mStart = a;
30: mEnd = b;
31: } else {
32: mStart = b;
33: mEnd = a;
34: }
35: }
36:
37: public LocationRangeImpl(LocationRange a, LocationRange b) {
38: mStart = (a.getStartLocation().compareTo(b.getStartLocation()) <= 0) ? a
39: .getStartLocation()
40: : b.getStartLocation();
41:
42: mEnd = (b.getEndLocation().compareTo(a.getEndLocation()) >= 0) ? b
43: .getEndLocation()
44: : a.getEndLocation();
45: }
46:
47: public Location getStartLocation() {
48: return mStart;
49: }
50:
51: public Location getEndLocation() {
52: return mEnd;
53: }
54:
55: public int hashCode() {
56: return getStartLocation().hashCode() * 31
57: + getEndLocation().hashCode();
58: }
59:
60: public boolean equals(Object obj) {
61: if (this == obj) {
62: return true;
63: }
64: if (obj instanceof LocationRangeImpl) {
65: LocationRangeImpl other = (LocationRangeImpl) obj;
66: return getStartLocation() == other.getStartLocation()
67: && getEndLocation() == other.getEndLocation();
68: }
69: return false;
70: }
71:
72: public String toString() {
73: int start = getStartLocation().getLocation();
74: int end = getEndLocation().getLocation() - 1;
75:
76: if (start == end) {
77: return String.valueOf(start);
78: } else {
79: return start + ".." + end;
80: }
81: }
82: }
|