1 ////////////////////////////////////////////////////////////////////////////////
2 // checkstyle: Checks Java source code for adherence to a set of rules.
3 // Copyright (C) 2001-2014 Oliver Burn
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 package com.puppycrawl.tools.checkstyle.api;
20
21 /**
22 * Representation of the comment block.
23 *
24 * @author o_sukhodolsky
25 */
26 public class Comment implements TextBlock
27 {
28 /** text of the comment. */
29 private final String[] text;
30
31 /** number of first line of the comment. */
32 private final int firstLine;
33
34 /** number of last line of the comment. */
35 private final int lastLine;
36
37 /** number of first column of the comment. */
38 private final int firstCol;
39
40 /** number of last column of the comment. */
41 private final int lastCol;
42
43 /**
44 * Creates new instance.
45 * @param text the lines that make up the comment.
46 * @param firstCol number of the first column of the comment.
47 * @param lastLine number of the last line of the comment.
48 * @param lastCol number of the last column of the comment.
49 */
50 public Comment(final String[] text, final int firstCol,
51 final int lastLine, final int lastCol)
52 {
53 this.text = new String[text.length];
54 System.arraycopy(text, 0, this.text, 0, this.text.length);
55 firstLine = lastLine - this.text.length + 1;
56 this.lastLine = lastLine;
57 this.firstCol = firstCol;
58 this.lastCol = lastCol;
59 }
60
61 /** {@inheritDoc} */
62 @Override
63 public final String[] getText()
64 {
65 return text.clone();
66 }
67
68 /** {@inheritDoc} */
69 @Override
70 public final int getStartLineNo()
71 {
72 return firstLine;
73 }
74
75 /** {@inheritDoc} */
76 @Override
77 public final int getEndLineNo()
78 {
79 return lastLine;
80 }
81
82 /** {@inheritDoc} */
83 @Override
84 public int getStartColNo()
85 {
86 return firstCol;
87 }
88
89 /** {@inheritDoc} */
90 @Override
91 public int getEndColNo()
92 {
93 return lastCol;
94 }
95
96 /** {@inheritDoc} */
97 @Override
98 public boolean intersects(int startLineNo, int startColNo,
99 int endLineNo, int endColNo)
100 {
101 // compute a single number for start and end
102 // to simplify conditional logic
103 final long multiplier = Integer.MAX_VALUE;
104 final long thisStart = firstLine * multiplier + firstCol;
105 final long thisEnd = lastLine * multiplier + lastCol;
106 final long inStart = startLineNo * multiplier + startColNo;
107 final long inEnd = endLineNo * multiplier + endColNo;
108
109 return !((thisEnd < inStart) || (inEnd < thisStart));
110 }
111
112 @Override
113 public String toString()
114 {
115 return "Comment[" + firstLine + ":" + firstCol + "-"
116 + lastLine + ":" + lastCol + "]";
117 }
118 }