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 /**
028 * A class that represents a violation. A violation happens when a resource does not respect a defined rule.
029 */
030 public class Violation {
031
032 private Resource resource;
033 private Rule rule;
034 private String message;
035 private RulePriority priority;
036 private Integer lineId;
037 private Double cost;
038
039 /**
040 * Creates of a violation from a rule. Will need to define the resource later on
041 * @deprecated since 2.3. Use the factory method create()
042 */
043 @Deprecated
044 public Violation(Rule rule) {
045 this.rule = rule;
046 }
047
048 /**
049 * Creates a fully qualified violation
050 *
051 * @param rule the rule that has been violated
052 * @param resource the resource the violation should be attached to
053 * @deprecated since 2.3. Use the factory method create()
054 */
055 @Deprecated
056 public Violation(Rule rule, Resource resource) {
057 this.resource = resource;
058 this.rule = rule;
059 }
060
061 public Resource getResource() {
062 return resource;
063 }
064
065 /**
066 * Sets the resource the violation applies to
067 *
068 * @return the current object
069 */
070 public Violation setResource(Resource resource) {
071 this.resource = resource;
072 return this;
073 }
074
075 public Rule getRule() {
076 return rule;
077 }
078
079 /**
080 * Sets the rule violated
081 *
082 * @return the current object
083 */
084 public Violation setRule(Rule rule) {
085 this.rule = rule;
086 return this;
087 }
088
089 public String getMessage() {
090 return message;
091 }
092
093 /**
094 * Sets the violation message
095 *
096 * @return the current object
097 */
098 public Violation setMessage(String message) {
099 this.message = message;
100 return this;
101 }
102
103 public Integer getLineId() {
104 return lineId;
105 }
106
107 /**
108 * Sets the violation line
109 *
110 * @return the current object
111 */
112 public Violation setLineId(Integer lineId) {
113 this.lineId = lineId;
114 return this;
115 }
116
117 public RulePriority getPriority() {
118 return priority;
119 }
120
121 /**
122 * Sets the violation priority
123 *
124 * @return the current object
125 * @deprecated since 2.3. The priority is set by the quality profile.
126 */
127 @Deprecated
128 public Violation setPriority(RulePriority priority) {
129 this.priority = priority;
130 return this;
131 }
132
133 /**
134 * @see <code>setCost()</code>
135 */
136 public Double getCost() {
137 return cost;
138 }
139
140 /**
141 * The cost to fix a violation can't be precisely computed without this information.
142 * Let's take the following example : a rule forbids to have methods whose complexity is greater than 10. Without this field "cost",
143 * the same violation is created with a method whose complexity is 15 and a method whose complexity is 100.
144 * If the cost to fix one point of complexity is 0.05h, then 15mn is necessary to fix the method whose complexity is 15,
145 * and 3h5mn is required to fix the method whose complexity is 100.
146 */
147 public Violation setCost(Double d) {
148 this.cost = d;
149 return this;
150 }
151
152 @Override
153 public boolean equals(Object obj) {
154 if (!(obj instanceof Violation)) {
155 return false;
156 }
157 if (this == obj) {
158 return true;
159 }
160 Violation other = (Violation) obj;
161 return new EqualsBuilder()
162 .append(rule, other.getRule())
163 .append(resource, other.getResource())
164 .isEquals();
165 }
166
167 @Override
168 public int hashCode() {
169 return new HashCodeBuilder(17, 37)
170 .append(getRule())
171 .append(getResource())
172 .toHashCode();
173 }
174
175 @Override
176 public String toString() {
177 return ReflectionToStringBuilder.toString(this);
178 }
179
180 public static Violation create(ActiveRule activeRule, Resource resource) {
181 return new Violation(activeRule.getRule()).setResource(resource);
182 }
183
184 public static Violation create(Rule rule, Resource resource) {
185 return new Violation(rule).setResource(resource);
186 }
187 }