MS_SHOULD_BE_FINAL: make constant String "final"
[cfb.git] / prod / net / jaekl / cfb / store / Location.java
1 package net.jaekl.cfb.store;
2
3 import java.io.PrintWriter;
4
5 import net.jaekl.cfb.util.Util;
6 import net.jaekl.cfb.xml.BugClass;
7 import net.jaekl.cfb.xml.BugMethod;
8 import net.jaekl.cfb.xml.SourceLine;
9
10 public class Location {
11         public static final String METHOD_CALLED = "METHOD_CALLED";
12         
13         Long m_id;
14         String m_className;
15         String m_methodName;
16         String m_methodRole;
17         Integer m_startLine;
18         Integer m_endLine;
19         
20         public Location(SourceLine sourceLine)
21         {
22                 init(sourceLine);
23         }
24         
25         public Location(BugMethod method) 
26         {
27                 init(method.getSourceLines());
28                 m_className = method.getClassName();
29                 m_methodName = method.getMethodName();
30                 m_methodRole = method.getRole();
31         }
32         
33         public Location(BugMethod method, SourceLine sourceLine)
34         {
35                 init(sourceLine);
36                 m_className = method.getClassName();
37                 m_methodName = method.getMethodName();
38                 m_methodRole = method.getRole();
39         }
40         
41         public Location(BugClass bugClass)
42         {
43                 init(bugClass.getSourceLines());
44                 m_className = bugClass.getClassName();
45         }
46         
47         public Location(Long id, String className, String methodName, String methodRole, Integer startLine, Integer endLine)
48         {
49                 m_id = id;
50                 m_className = className;
51                 m_methodName = methodName;
52                 m_methodRole = methodRole;
53                 m_startLine = startLine;
54                 m_endLine = endLine;
55         }
56         
57         public String getClassName() { return m_className; }
58         public String getMethodName() { return m_methodName; }
59         public String getMethodRole() { return m_methodRole; }
60         public int getStart() { return m_startLine; }
61         public int getEnd() { return m_endLine; }
62         
63         public void dump(PrintWriter pw, int indent) 
64         {
65                 String margin = String.format("%" + indent + "s", "");
66                 String tab = margin + "  ";
67                 pw.println(margin + "Location");
68                 if (null != m_className) {
69                         pw.println(tab + "classname = " + m_className);
70                 }
71                 if (null != m_methodName) {
72                         if (null != m_methodRole) {
73                                 pw.println(tab + "method = " + m_methodName + " (" + m_methodRole + ")");
74                         }
75                         else {
76                                 pw.println(tab + "method = " + m_methodName);
77                         }
78                 }
79                 if (null != m_startLine) {
80                         pw.println(tab + "lines = " + m_startLine + " .. " + m_endLine);
81                 }
82         }
83
84         public boolean fuzzyEquals(Location that)
85         {
86                 if (null == that) {
87                         return false;
88                 }
89                 
90                 if (! Util.objsAreEqual(this.m_className, that.m_className)) {
91                         return false;
92                 }
93                 
94                 if (! Util.objsAreEqual(this.m_methodName, that.m_methodName)) {
95                         return false;
96                 }
97                 
98                 if (! Util.objsAreEqual(this.m_methodRole, that.m_methodRole)) {
99                         return false;
100                 }
101                 
102                 return true;
103         }
104         
105         @Override
106         public boolean equals(Object other)
107         {
108                 if (null == other) {
109                         return false;
110                 }
111                 if (other instanceof Location) {
112                         return fuzzyEquals((Location)other);
113                 }
114                 return false;
115         }
116         
117         @Override
118         public int hashCode()
119         {
120                 return   Util.objHashCode(m_className)
121                            ^ Util.objHashCode(m_methodName)
122                            ^ Util.objHashCode(m_methodRole);
123         }
124         
125         private void init(SourceLine[] sourceLines) 
126         {
127                 if (sourceLines.length > 0) {
128                         assert(null != sourceLines[0]);
129                         init(sourceLines[0]);
130                 }               
131         }
132         
133         private void init(SourceLine sourceLine) 
134         {
135                 init(sourceLine.getClassName(), sourceLine.getStart(), sourceLine.getEnd());
136         }
137
138         private void init(String className, int startLine, int endLine)
139         {
140                 m_id = null;
141                 m_className = className;
142                 m_methodName = null;
143                 m_methodRole = null;
144                 m_startLine = startLine;
145                 m_endLine = endLine;
146         }
147 }