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