001: /*
002: * Hammurapi
003: * Automated Java code review system.
004: * Copyright (C) 2004 Hammurapi Group
005: *
006: * This program is free software; you can redistribute it and/or modify
007: * it under the terms of the GNU General Public License as published by
008: * the Free Software Foundation; either version 2 of the License, or
009: * (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * URL: http://www.hammurapi.org
021: * e-Mail: support@hammurapi.biz
022: */
023: package org.hammurapi;
024:
025: import java.io.Serializable;
026: import java.util.HashMap;
027: import java.util.Map;
028:
029: import com.pavelvlasov.review.SourceMarker;
030: import com.pavelvlasov.review.SourceMarkerComparator;
031:
032: /**
033: * @author Pavel Vlasov
034: * @version $Revision: 1.5 $
035: */
036: public class SimpleViolation implements Violation, Serializable {
037: private static ThreadLocal inspectorMap = new ThreadLocal() {
038: protected Object initialValue() {
039: return new HashMap();
040: }
041: };
042:
043: /**
044: * Comment for <code>serialVersionUID</code>
045: */
046: private static final long serialVersionUID = -6111796217286959070L;
047: private SourceMarker source;
048: private String message;
049:
050: private String inspectorName;
051:
052: public SimpleViolation(SourceMarker source, String message,
053: InspectorDescriptor descriptor) {
054: super ();
055: this .source = source;
056:
057: if (descriptor != null) {
058: Map iMap = (Map) inspectorMap.get();
059: if (iMap != null) {
060: iMap.put(descriptor.getName(), descriptor);
061: }
062: inspectorName = descriptor.getName();
063: }
064:
065: this .message = message;
066: }
067:
068: /**
069: * @return Returns the message.
070: */
071: public String getMessage() {
072: return message;
073: }
074:
075: /**
076: * @return Returns the ruleName.
077: */
078: public InspectorDescriptor getDescriptor() {
079: return inspectorName == null ? null
080: : (InspectorDescriptor) ((Map) inspectorMap.get())
081: .get(inspectorName);
082: }
083:
084: /**
085: * @return Returns the source.
086: */
087: public SourceMarker getSource() {
088: return source;
089: }
090:
091: public int compareTo(Object o) {
092: if (o == this ) {
093: return 0;
094: } else if (o instanceof Violation) {
095: Violation v = (Violation) o;
096: int vline = v.getSource() == null ? 0 : v.getSource()
097: .getLine();
098: int line = getSource() == null ? 0 : getSource().getLine();
099: if (vline == line) {
100: int vcolumn = v.getSource() == null ? 0 : v.getSource()
101: .getColumn();
102: int column = getSource() == null ? 0 : getSource()
103: .getColumn();
104: if (vcolumn == column) {
105: if (message == null) {
106: return v.getMessage() == null ? 0 : 1;
107: }
108:
109: if (v.getMessage() == null) {
110: return -1;
111: }
112:
113: return message.compareTo(v.getMessage());
114: }
115:
116: return column - vcolumn;
117: }
118:
119: return line - vline;
120: } else {
121: return 1;
122: }
123: }
124:
125: public boolean equals(Object obj) {
126: if (obj == this ) {
127: return true;
128: } else if (obj instanceof Violation) {
129: Violation v = (Violation) obj;
130: if (SourceMarkerComparator._compare(getSource(), v
131: .getSource()) == 0) {
132: // Inspector descriptor is ignored in equality.
133: return message == null ? v.getMessage() == null
134: : message.equals(v.getMessage());
135: }
136:
137: return false;
138: } else {
139: return super.equals(obj);
140: }
141: }
142:
143: }
|