1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2019 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       * @param text the lines that make up the comment.
48       * @param firstCol number of the first column of the comment.
49       * @param lastLine number of the last line of the comment.
50       * @param lastCol number of the last column of the comment.
51       */
52      public Comment(final String[] text, final int firstCol,
53              final int lastLine, final int lastCol) {
54          this.text = text.clone();
55          startLineNo = lastLine - text.length + 1;
56          endLineNo = lastLine;
57          startColNo = firstCol;
58          endColNo = lastCol;
59      }
60  
61      @Override
62      public final String[] getText() {
63          return text.clone();
64      }
65  
66      @Override
67      public final int getStartLineNo() {
68          return startLineNo;
69      }
70  
71      @Override
72      public final int getEndLineNo() {
73          return endLineNo;
74      }
75  
76      @Override
77      public int getStartColNo() {
78          return startColNo;
79      }
80  
81      @Override
82      public int getEndColNo() {
83          return endColNo;
84      }
85  
86      @Override
87      public boolean intersects(int startLine, int startCol,
88                                int endLine, int endCol) {
89          // compute a single number for start and end
90          // to simplify conditional logic
91          final long multiplier = Integer.MAX_VALUE;
92          final long thisStart = startLineNo * multiplier + startColNo;
93          final long thisEnd = endLineNo * multiplier + endColNo;
94          final long inStart = startLine * multiplier + startCol;
95          final long inEnd = endLine * multiplier + endCol;
96  
97          return thisEnd >= inStart && inEnd >= thisStart;
98      }
99  
100     @Override
101     public String toString() {
102         return "Comment[text=" + Arrays.toString(text)
103                 + ", startLineNo=" + startLineNo
104                 + ", endLineNo=" + endLineNo
105                 + ", startColNo=" + startColNo
106                 + ", endColNo=" + endColNo + ']';
107     }
108 
109 }