1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package com.puppycrawl.tools.checkstyle.bdd;
21
22 import java.util.Objects;
23 import java.util.regex.Pattern;
24
25 public final class TestInputViolation implements Comparable<TestInputViolation> {
26
27
28 private static final Pattern OPEN_CURLY_PATTERN = Pattern.compile("\\{");
29
30
31 private static final Pattern OPEN_PAREN_PATTERN = Pattern.compile("\\(");
32
33
34 private static final Pattern CLOSE_PAREN_PATTERN = Pattern.compile("\\)");
35
36
37 private final int lineNo;
38
39
40 private final String message;
41
42
43
44
45
46
47
48 public TestInputViolation(int lineNo, String message) {
49 this.lineNo = lineNo;
50 this.message = message;
51 }
52
53
54
55
56
57
58 public int getLineNo() {
59 return lineNo;
60 }
61
62
63
64
65
66
67 public String getMessage() {
68 return message;
69 }
70
71
72
73
74
75
76 public String toRegex() {
77 String regex = lineNo + ":(?:\\d+:)?\\s.*";
78 if (message != null) {
79 String rawMessage = message;
80 rawMessage = OPEN_CURLY_PATTERN.matcher(rawMessage).replaceAll("\\\\{");
81 rawMessage = OPEN_PAREN_PATTERN.matcher(rawMessage).replaceAll("\\\\(");
82 rawMessage = CLOSE_PAREN_PATTERN.matcher(rawMessage).replaceAll("\\\\)");
83 regex += rawMessage + ".*";
84 }
85 return regex;
86 }
87
88 @Override
89 public int compareTo(TestInputViolation testInputViolation) {
90 final int result;
91 if (message != null && lineNo == testInputViolation.lineNo) {
92 result = testInputViolation.message.compareTo(message);
93 }
94 else {
95 result = Integer.compare(lineNo, testInputViolation.lineNo);
96 }
97 return result;
98 }
99
100 @Override
101 public int hashCode() {
102 return Objects.hash(lineNo);
103 }
104
105 @Override
106 public boolean equals(Object object) {
107 return getClass() == object.getClass() && compareTo((TestInputViolation) object) == 0;
108 }
109 }