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
038 /**
039 * Creates of a violation from a rule. Will need to define the resource later on
040 */
041 public Violation(Rule rule) {
042 this.rule = rule;
043 }
044
045 /**
046 * Creates a fully qualified violation
047 *
048 * @param rule the rule that has been violated
049 * @param resource the resource the violation should be attached to
050 */
051 public Violation(Rule rule, Resource resource) {
052 this.resource = resource;
053 this.rule = rule;
054 }
055
056 public Resource getResource() {
057 return resource;
058 }
059
060 /**
061 * Sets the resource the violation applies to
062 *
063 * @return the current object
064 */
065 public Violation setResource(Resource resource) {
066 this.resource = resource;
067 return this;
068 }
069
070 public Rule getRule() {
071 return rule;
072 }
073
074 /**
075 * Sets the rule violated
076 *
077 * @return the current object
078 */
079 public Violation setRule(Rule rule) {
080 this.rule = rule;
081 return this;
082 }
083
084 public String getMessage() {
085 return message;
086 }
087
088 /**
089 * Sets the violation message
090 *
091 * @return the current object
092 */
093 public Violation setMessage(String message) {
094 this.message = message;
095 return this;
096 }
097
098 public Integer getLineId() {
099 return lineId;
100 }
101
102 /**
103 * Sets the violation line
104 *
105 * @return the current object
106 */
107 public Violation setLineId(Integer lineId) {
108 this.lineId = lineId;
109 return this;
110 }
111
112 public RulePriority getPriority() {
113 return priority;
114 }
115
116 /**
117 * Sets the violation priority
118 *
119 * @return the current object
120 */
121 public Violation setPriority(RulePriority priority) {
122 this.priority = priority;
123 return this;
124 }
125
126 @Override
127 public boolean equals(Object obj) {
128 if (!(obj instanceof Violation)) {
129 return false;
130 }
131 if (this == obj) {
132 return true;
133 }
134 Violation other = (Violation) obj;
135 return new EqualsBuilder()
136 .append(rule, other.getRule())
137 .append(resource, other.getResource())
138 .isEquals();
139 }
140
141 @Override
142 public int hashCode() {
143 return new HashCodeBuilder(17, 37)
144 .append(getRule())
145 .append(getResource())
146 .toHashCode();
147 }
148
149 @Override
150 public String toString() {
151 return ReflectionToStringBuilder.toString(this);
152 }
153 }