1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19 package org.apache.maven.plugins.checkstyle;
20
21 import java.util.Objects;
22 import java.util.StringJoiner;
23
24 /**
25 * Holds data about a single violation and represents the violation itself.
26 */
27 class Violation {
28
29 /**
30 * Indicates that a column is not set.
31 */
32 protected static final String NO_COLUMN = "-1";
33
34 /** The full qualified class name of the checkstyle rule */
35 private final String source;
36
37 /** The absolute path of the file containing the violation */
38 private final String file;
39
40 private final String line;
41
42 private String column = NO_COLUMN;
43
44 private final String severity;
45
46 private final String message;
47
48 private final String ruleName;
49
50 private final String category;
51
52 // Leaving out column, because there is no CHECKSTYLE:OFF support.
53
54 /**
55 * Creates a violation instance without a column set.
56 *
57 * @param source
58 * the fully qualified class name of the checkstyle rule
59 * @param file
60 * the absolute file path in which the violation occurred
61 * @param line
62 * the line in the file on which the violation occurred
63 * @param severity
64 * the severity of the violation
65 * @param message
66 * the message from checkstyle for this violation
67 * @param ruleName
68 * the rule name from which this violation was created
69 * @param category
70 * the category of the checkstyle violation
71 */
72 Violation(
73 String source,
74 String file,
75 String line,
76 String severity,
77 String message,
78 String ruleName,
79 String category) {
80 this.source = Objects.requireNonNull(source);
81 this.file = file;
82 this.line = line;
83 this.severity = Objects.requireNonNull(severity);
84 this.message = Objects.requireNonNull(message);
85 this.ruleName = Objects.requireNonNull(ruleName);
86 this.category = Objects.requireNonNull(category);
87 }
88
89 /**
90 * Returns the fully qualified class name of the checker rule.
91 *
92 * @return the fully qualified class name of the checker rule
93 */
94 protected String getSource() {
95 return source;
96 }
97
98 /**
99 * Returns the absolute file path to the checked file.
100 *
101 * @return the absolute file path to the checked file
102 */
103 protected String getFile() {
104 return file;
105 }
106
107 /**
108 * Returns the line in the checked file on which the violation occurred.
109 *
110 * @return the line in the checked file on which the violation occurred
111 */
112 protected String getLine() {
113 return line;
114 }
115
116 /**
117 * Returns the column in which the violation occurred, if available.
118 *
119 * @return the column in which the violation occurred, if available. Otherwise returns {@link #NO_COLUMN}.
120 */
121 protected String getColumn() {
122 return column;
123 }
124
125 /**
126 * Sets the column value for this violation to the given string value.
127 * @param column the column value to set. May be {@code null}, which will set it to the {@link #NO_COLUMN} value.
128 */
129 protected void setColumn(/* Nullable */ String column) {
130 if (column == null || column.length() < 1) {
131 this.column = NO_COLUMN;
132 } else {
133 this.column = column;
134 }
135 }
136
137 /**
138 * Returns the severity of the current violation.
139 *
140 * @return the severity of the current violation
141 */
142 protected String getSeverity() {
143 return severity;
144 }
145
146 /**
147 * Returns the message produced by checkstyle for the current violation.
148 *
149 * @return the message produced by checkstyle for the current violation
150 */
151 protected String getMessage() {
152 return message;
153 }
154
155 /**
156 * Returns the name of the rule which led to the current violation.
157 *
158 * @return the name of the rule which led to the current violation
159 */
160 protected String getRuleName() {
161 return ruleName;
162 }
163
164 /**
165 * Returns the category of the current violation.
166 *
167 * @return the category of the current violation
168 */
169 protected String getCategory() {
170 return category;
171 }
172
173 @Override
174 public boolean equals(Object other) {
175 if (this == other) {
176 return true;
177 }
178 if (!(other instanceof Violation)) {
179 return false;
180 }
181 Violation violation = (Violation) other;
182 return Objects.equals(line, violation.line)
183 && Objects.equals(column, violation.column)
184 && source.equals(violation.source)
185 && Objects.equals(file, violation.file)
186 && severity.equals(violation.severity)
187 && message.equals(violation.message)
188 && ruleName.equals(violation.ruleName)
189 && category.equals(violation.category);
190 }
191
192 @Override
193 public int hashCode() {
194 return Objects.hash(source, file, line, column, severity, message, ruleName, category);
195 }
196
197 @Override
198 public String toString() {
199 return new StringJoiner(", ", Violation.class.getSimpleName() + "[", "]")
200 .add("source='" + source + "'")
201 .add("file='" + file + "'")
202 .add("line=" + line)
203 .add("column=" + column)
204 .add("severity='" + severity + "'")
205 .add("message='" + message + "'")
206 .add("ruleName='" + ruleName + "'")
207 .add("category='" + category + "'")
208 .toString();
209 }
210 }