001 /*
002 * Sonar, open source software quality management tool.
003 * Copyright (C) 2009 SonarSource SA
004 * mailto:contact AT sonarsource DOT com
005 *
006 * Sonar is free software; you can redistribute it and/or
007 * modify it under the terms of the GNU Lesser General Public
008 * License as published by the Free Software Foundation; either
009 * version 3 of the License, or (at your option) any later version.
010 *
011 * Sonar 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 GNU
014 * Lesser General Public License for more details.
015 *
016 * You should have received a copy of the GNU Lesser General Public
017 * License along with Sonar; if not, write to the Free Software
018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
019 */
020 package org.sonar.api.rules;
021
022 import org.apache.commons.lang.builder.EqualsBuilder;
023 import org.apache.commons.lang.builder.HashCodeBuilder;
024 import org.apache.commons.lang.builder.ReflectionToStringBuilder;
025 import org.sonar.api.resources.Resource;
026
027 import java.util.Date;
028
029 /**
030 * A class that represents a violation. A violation happens when a resource does not respect a defined rule.
031 */
032 public class Violation {
033
034 private Resource resource;
035 private Rule rule;
036 private String message;
037 private RulePriority severity;
038 private Integer lineId;
039 private Double cost;
040 private Date createdAt;
041
042 /**
043 * Creates of a violation from a rule. Will need to define the resource later on
044 *
045 * @deprecated since 2.3. Use the factory method create()
046 */
047 @Deprecated
048 public Violation(Rule rule) {
049 this.rule = rule;
050 }
051
052 /**
053 * Creates a fully qualified violation
054 *
055 * @param rule the rule that has been violated
056 * @param resource the resource the violation should be attached to
057 * @deprecated since 2.3. Use the factory method create()
058 */
059 @Deprecated
060 public Violation(Rule rule, Resource resource) {
061 this.resource = resource;
062 this.rule = rule;
063 }
064
065 public Resource getResource() {
066 return resource;
067 }
068
069 /**
070 * Sets the resource the violation applies to
071 *
072 * @return the current object
073 */
074 public Violation setResource(Resource resource) {
075 this.resource = resource;
076 return this;
077 }
078
079 public Rule getRule() {
080 return rule;
081 }
082
083 /**
084 * Sets the rule violated
085 *
086 * @return the current object
087 */
088 public Violation setRule(Rule rule) {
089 this.rule = rule;
090 return this;
091 }
092
093 public String getMessage() {
094 return message;
095 }
096
097 /**
098 * Sets the violation message
099 *
100 * @return the current object
101 */
102 public Violation setMessage(String message) {
103 this.message = message;
104 return this;
105 }
106
107 /**
108 * @see #setLineId(Integer)
109 */
110 public Integer getLineId() {
111 return lineId;
112 }
113
114 /**
115 * Sets the violation line. Note that numbering starts from 1.
116 *
117 * @return the current object
118 */
119 public Violation setLineId(Integer lineId) {
120 this.lineId = lineId;
121 return this;
122 }
123
124 /**
125 * @since 2.5
126 */
127 public RulePriority getSeverity() {
128 return severity;
129 }
130
131 /**
132 * For internal use only.
133 *
134 * @since 2.5
135 */
136 public Violation setSeverity(RulePriority severity) {
137 this.severity = severity;
138 return this;
139 }
140
141 /**
142 * @deprecated since 2.5 use {@link #getSeverity()} instead. See http://jira.codehaus.org/browse/SONAR-1829
143 */
144 @Deprecated
145 public RulePriority getPriority() {
146 return severity;
147 }
148
149 /**
150 * For internal use only
151 *
152 * @deprecated since 2.5 use {@link #setSeverity(RulePriority)} instead. See http://jira.codehaus.org/browse/SONAR-1829
153 */
154 @Deprecated
155 public Violation setPriority(RulePriority priority) {
156 this.severity = priority;
157 return this;
158 }
159
160 /**
161 * @see #setCost(Double)
162 * @since 2.4
163 */
164 public Double getCost() {
165 return cost;
166 }
167
168 /**
169 * The cost to fix a violation can't be precisely computed without this information. Let's take the following example : a rule forbids to
170 * have methods whose complexity is greater than 10. Without this field "cost", the same violation is created with a method whose
171 * complexity is 15 and a method whose complexity is 100. If the cost to fix one point of complexity is 0.05h, then 15mn is necessary to
172 * fix the method whose complexity is 15, and 3h5mn is required to fix the method whose complexity is 100.
173 *
174 * @since 2.4
175 */
176 public Violation setCost(Double d) {
177 this.cost = d;
178 return this;
179 }
180
181 /**
182 * @since 2.5
183 */
184 public Date getCreatedAt() {
185 return createdAt;
186 }
187
188 /**
189 * For internal use only
190 *
191 * @since 2.5
192 */
193 public Violation setCreatedAt(Date createdAt) {
194 this.createdAt = createdAt;
195 return this;
196 }
197
198 @Override
199 public boolean equals(Object obj) {
200 if (!(obj instanceof Violation)) {
201 return false;
202 }
203 if (this == obj) {
204 return true;
205 }
206 Violation other = (Violation) obj;
207 return new EqualsBuilder()
208 .append(rule, other.getRule())
209 .append(resource, other.getResource())
210 .isEquals();
211 }
212
213 @Override
214 public int hashCode() {
215 return new HashCodeBuilder(17, 37)
216 .append(getRule())
217 .append(getResource())
218 .toHashCode();
219 }
220
221 @Override
222 public String toString() {
223 return ReflectionToStringBuilder.toString(this);
224 }
225
226 public static Violation create(ActiveRule activeRule, Resource resource) {
227 return new Violation(activeRule.getRule()).setResource(resource);
228 }
229
230 public static Violation create(Rule rule, Resource resource) {
231 return new Violation(rule).setResource(resource);
232 }
233
234 }