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.api;
21  
22  import java.util.Arrays;
23  
24  /**
25   * Representation of the comment block.
26   *
27   */
28  public class Comment implements TextBlock {
29  
30      /** Text of the comment. */
31      private final String[] text;
32  
33      /** Number of first line of the comment. */
34      private final int startLineNo;
35  
36      /** Number of last line of the comment. */
37      private final int endLineNo;
38  
39      /** Number of first column of the comment. */
40      private final int startColNo;
41  
42      /** Number of last column of the comment. */
43      private final int endColNo;
44  
45      /**
46       * Creates new instance.
47       *
48       * @param text the lines that make up the comment.
49       * @param firstCol number of the first column of the comment.
50       * @param lastLine number of the last line of the comment.
51       * @param lastCol number of the last column of the comment.
52       */
53      public Comment(final String[] text, final int firstCol,
54              final int lastLine, final int lastCol) {
55          this.text = text.clone();
56          startLineNo = lastLine - text.length + 1;
57          endLineNo = lastLine;
58          startColNo = firstCol;
59          endColNo = lastCol;
60      }
61  
62      @Override
63      public final String[] getText() {
64          return text.clone();
65      }
66  
67      @Override
68      public final int getStartLineNo() {
69          return startLineNo;
70      }
71  
72      @Override
73      public final int getEndLineNo() {
74          return endLineNo;
75      }
76  
77      @Override
78      public int getStartColNo() {
79          return startColNo;
80      }
81  
82      @Override
83      public int getEndColNo() {
84          return endColNo;
85      }
86  
87      @Override
88      public boolean intersects(int startLine, int startCol,
89                                int endLine, int endCol) {
90          // compute a single number for start and end
91          // to simplify conditional logic
92          final long multiplier = Integer.MAX_VALUE;
93          final long thisStart = startLineNo * multiplier + startColNo;
94          final long thisEnd = endLineNo * multiplier + endColNo;
95          final long inStart = startLine * multiplier + startCol;
96          final long inEnd = endLine * multiplier + endCol;
97  
98          return thisEnd >= inStart && inEnd >= thisStart;
99      }
100 
101     @Override
102     public String toString() {
103         return "Comment[text=" + Arrays.toString(text)
104                 + ", startLineNo=" + startLineNo
105                 + ", endLineNo=" + endLineNo
106                 + ", startColNo=" + startColNo
107                 + ", endColNo=" + endColNo + ']';
108     }
109 
110 }