1 ///////////////////////////////////////////////////////////////////////////////////////////////
2 // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3 // Copyright (C) 2001-2025 the original author or authors.
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 ///////////////////////////////////////////////////////////////////////////////////////////////
19
20 package com.puppycrawl.tools.checkstyle.bdd;
21
22 import java.util.Objects;
23 import java.util.regex.Pattern;
24
25 /**
26 * Represents a test input violation with line number and message.
27 *
28 * @param lineNo parsed violation line number
29 * @param message parsed violation message
30 */
31 public record TestInputViolation(int lineNo, String message)
32 implements Comparable<TestInputViolation> {
33
34 /** Pattern to match the symbol: "{". */
35 private static final Pattern OPEN_CURLY_PATTERN = Pattern.compile("\\{");
36
37 /** Pattern to match the symbol: "(". */
38 private static final Pattern OPEN_PAREN_PATTERN = Pattern.compile("\\(");
39
40 /** Pattern to match the symbol: ")". */
41 private static final Pattern CLOSE_PAREN_PATTERN = Pattern.compile("\\)");
42
43 /** Legacy getter for line number (backward compatibility). */
44 public int getLineNo() {
45 return lineNo;
46 }
47
48 /** Legacy getter for message (backward compatibility). */
49 public String getMessage() {
50 return message;
51 }
52
53 /**
54 * Creates regex string to match the violation message format.
55 *
56 * @return the regex string
57 */
58 public String toRegex() {
59 String regex = lineNo + ":(?:\\d+:)?\\s.*";
60 if (message != null) {
61 String rawMessage = message;
62 rawMessage = OPEN_CURLY_PATTERN.matcher(rawMessage).replaceAll("\\\\{");
63 rawMessage = OPEN_PAREN_PATTERN.matcher(rawMessage).replaceAll("\\\\(");
64 rawMessage = CLOSE_PAREN_PATTERN.matcher(rawMessage).replaceAll("\\\\)");
65 regex += rawMessage + ".*";
66 }
67 return regex;
68 }
69
70 @Override
71 public int compareTo(TestInputViolation other) {
72 final int result;
73 if (message != null && lineNo == other.lineNo) {
74 result = message.compareTo(other.message);
75 }
76 else {
77 result = Integer.compare(lineNo, other.lineNo);
78 }
79 return result;
80 }
81
82 @Override
83 public int hashCode() {
84 return Objects.hash(lineNo);
85 }
86
87 @Override
88 public boolean equals(Object object) {
89 return object instanceof TestInputViolation violation
90 && compareTo(violation) == 0;
91 }
92 }