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