KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hammurapi > SimpleViolation


1 /*
2  * Hammurapi
3  * Automated Java code review system.
4  * Copyright (C) 2004 Hammurapi Group
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  * URL: http://www.hammurapi.org
21  * e-Mail: support@hammurapi.biz
22  */

23 package org.hammurapi;
24
25 import java.io.Serializable JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.Map JavaDoc;
28
29 import com.pavelvlasov.review.SourceMarker;
30 import com.pavelvlasov.review.SourceMarkerComparator;
31
32 /**
33  * @author Pavel Vlasov
34  * @version $Revision: 1.5 $
35  */

36 public class SimpleViolation implements Violation, Serializable JavaDoc {
37     private static ThreadLocal JavaDoc inspectorMap=new ThreadLocal JavaDoc() {
38         protected Object JavaDoc initialValue() {
39             return new HashMap JavaDoc();
40         }
41     };
42     
43     /**
44      * Comment for <code>serialVersionUID</code>
45      */

46     private static final long serialVersionUID = -6111796217286959070L;
47     private SourceMarker source;
48     private String JavaDoc message;
49
50     private String JavaDoc inspectorName;
51     
52     public SimpleViolation(SourceMarker source, String JavaDoc message, InspectorDescriptor descriptor) {
53         super();
54         this.source=source;
55         
56         if (descriptor!=null) {
57             Map JavaDoc iMap = (Map JavaDoc) inspectorMap.get();
58             if (iMap!=null) {
59                 iMap.put(descriptor.getName(), descriptor);
60             }
61             inspectorName=descriptor.getName();
62         }
63         
64         this.message=message;
65     }
66
67     /**
68      * @return Returns the message.
69      */

70     public String JavaDoc getMessage() {
71         return message;
72     }
73
74     /**
75      * @return Returns the ruleName.
76      */

77     public InspectorDescriptor getDescriptor() {
78         return inspectorName==null ? null : (InspectorDescriptor) ((Map JavaDoc) inspectorMap.get()).get(inspectorName);
79     }
80
81     /**
82      * @return Returns the source.
83      */

84     public SourceMarker getSource() {
85         return source;
86     }
87
88     public int compareTo(Object JavaDoc o) {
89         if (o==this) {
90             return 0;
91         } else if (o instanceof Violation) {
92             Violation v=(Violation) o;
93             int vline = v.getSource()==null ? 0 : v.getSource().getLine();
94             int line = getSource()==null ? 0 : getSource().getLine();
95             if (vline==line) {
96                 int vcolumn = v.getSource()==null ? 0 : v.getSource().getColumn();
97                 int column = getSource()==null ? 0 : getSource().getColumn();
98                 if (vcolumn==column) {
99                     if (message==null) {
100                         return v.getMessage()==null ? 0 : 1;
101                     }
102                     
103                     if (v.getMessage()==null) {
104                         return -1;
105                     }
106                     
107                     return message.compareTo(v.getMessage());
108                 }
109                 
110                 return column-vcolumn;
111             }
112             
113             return line-vline;
114         } else {
115             return 1;
116         }
117     }
118     
119     public boolean equals(Object JavaDoc obj) {
120         if (obj==this) {
121             return true;
122         } else if (obj instanceof Violation) {
123             Violation v=(Violation) obj;
124             if (SourceMarkerComparator._compare(getSource(), v.getSource())==0) {
125                 // Inspector descriptor is ignored in equality.
126
return message==null ? v.getMessage()==null : message.equals(v.getMessage());
127             }
128             
129             return false;
130         } else {
131             return super.equals(obj);
132         }
133     }
134
135 }
136
Popular Tags