KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > cojen > classfile > LocationRangeImpl


1 /*
2  * Copyright 2004 Brian S O'Neill
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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) ?
39             a.getStartLocation() : b.getStartLocation();
40
41         mEnd = (b.getEndLocation().compareTo(a.getEndLocation()) >= 0) ?
42             b.getEndLocation() : a.getEndLocation();
43     }
44
45     public Location getStartLocation() {
46         return mStart;
47     }
48
49     public Location getEndLocation() {
50         return mEnd;
51     }
52
53     public int hashCode() {
54         return getStartLocation().hashCode() * 31 + getEndLocation().hashCode();
55     }
56
57     public boolean equals(Object JavaDoc obj) {
58         if (this == obj) {
59             return true;
60         }
61         if (obj instanceof LocationRangeImpl) {
62             LocationRangeImpl other = (LocationRangeImpl)obj;
63             return getStartLocation() == other.getStartLocation() &&
64                 getEndLocation() == other.getEndLocation();
65         }
66         return false;
67     }
68
69     public String JavaDoc toString() {
70         int start = getStartLocation().getLocation();
71         int end = getEndLocation().getLocation() - 1;
72
73         if (start == end) {
74             return String.valueOf(start);
75         } else {
76             return start + ".." + end;
77         }
78     }
79 }
80
Popular Tags