View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2024 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  public final class TestInputViolation implements Comparable<TestInputViolation> {
26  
27      /** Pattern to match the symbol: "{". */
28      private static final Pattern OPEN_CURLY_PATTERN = Pattern.compile("\\{");
29  
30      /** Pattern to match the symbol: "(". */
31      private static final Pattern OPEN_PAREN_PATTERN = Pattern.compile("\\(");
32  
33      /** Pattern to match the symbol: ")". */
34      private static final Pattern CLOSE_PAREN_PATTERN = Pattern.compile("\\)");
35  
36      /** Parsed violation line number. */
37      private final int lineNo;
38  
39      /** Parsed violation message. */
40      private final String message;
41  
42      /**
43       * Creates a new {@code TestInputViolation} instance.
44       *
45       * @param lineNo the violation line number.
46       * @param message the violation message.
47       */
48      public TestInputViolation(int lineNo, String message) {
49          this.lineNo = lineNo;
50          this.message = message;
51      }
52  
53      /**
54       * Gets the violation line number.
55       *
56       * @return the violation line number.
57       */
58      public int getLineNo() {
59          return lineNo;
60      }
61  
62      /**
63       * Gets the violation message.
64       *
65       * @return the violation message.
66       */
67      public String getMessage() {
68          return message;
69      }
70  
71      /**
72       * Creates regex string to match the violation message format.
73       *
74       * @return the regex string.
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 }