Initial import
[jpf-core.git] / src / main / gov / nasa / jpf / util / SourceRef.java
1 /*
2  * Copyright (C) 2014, United States Government, as represented by the
3  * Administrator of the National Aeronautics and Space Administration.
4  * All rights reserved.
5  *
6  * The Java Pathfinder core (jpf-core) platform is licensed under the
7  * Apache License, Version 2.0 (the "License"); you may not use this file except
8  * in compliance with the License. You may obtain a copy of the License at
9  * 
10  *        http://www.apache.org/licenses/LICENSE-2.0. 
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and 
16  * limitations under the License.
17  */
18 package gov.nasa.jpf.util;
19
20 /**
21  * a source reference abstraction wrapping file and line information
22  */
23 public class SourceRef {
24   public String fileName;
25   public int    line;
26
27   public SourceRef (String f, int l) {
28     if (f == null) {
29       fileName = "?";
30     } else {
31       fileName = f;
32     }
33     
34     line = l;
35   }
36
37   public SourceRef (String spec){
38     int idx = spec.indexOf(':');
39     if (idx > 0){
40       fileName = spec.substring(0, idx);
41       line = Integer.parseInt(spec.substring(idx+1));
42     } else {
43       fileName = spec;
44       line = 0;
45     }
46   }
47
48   public String getLocationString() {
49     return (fileName + ':' + line);
50   }
51   
52   public String getLineString () {
53     Source source = Source.getSource(fileName);
54     if (source != null) {
55       return source.getLine(line);
56     } else {
57       return null;
58     }
59   }
60
61   @Override
62   public boolean equals (Object o) {
63     if (o == null) {
64       return false;
65     }
66
67     if (!(o instanceof SourceRef)) {
68       return false;
69     }
70
71     SourceRef that = (SourceRef) o;
72
73     if (this.fileName == null) {
74       return false;
75     }
76
77     if (this.line == -1) {
78       return false;
79     }
80
81     if (!this.fileName.equals(that.fileName)) {
82       return false;
83     }
84
85     if (this.line != that.line) {
86       return false;
87     }
88
89     return true;
90   }
91
92   public boolean equals (String f, int l) {
93     if (fileName == null) {
94       return false;
95     }
96
97     if (line == -1) {
98       return false;
99     }
100
101     if (!fileName.equals(f)) {
102       return false;
103     }
104
105     if (line != l) {
106       return false;
107     }
108
109     return true;
110   }
111
112   public boolean equals (String filePos){
113     if (filePos.startsWith(fileName)){
114       int len = fileName.length();
115       if (filePos.charAt(len) == ':'){
116         if (Integer.parseInt(filePos.substring(len+1)) == line){
117           return true;
118         }
119       }
120     }
121
122     return false;
123   }
124
125   @Override
126   public int hashCode() {
127     assert false : "hashCode not designed";
128     return 42; // any arbitrary constant will do
129     // thanks, FindBugs!
130   }
131
132   public String getFileName () {
133     return fileName;
134   }
135
136   public void set (SourceRef sr) {
137     fileName = sr.fileName;
138     line = sr.line;
139   }
140
141   @Override
142   public String toString () {
143     return (fileName + ':' + line);
144   }
145 }