001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2018 the original author or authors. 004// 005// This library is free software; you can redistribute it and/or 006// modify it under the terms of the GNU Lesser General Public 007// License as published by the Free Software Foundation; either 008// version 2.1 of the License, or (at your option) any later version. 009// 010// This library is distributed in the hope that it will be useful, 011// but WITHOUT ANY WARRANTY; without even the implied warranty of 012// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013// Lesser General Public License for more details. 014// 015// You should have received a copy of the GNU Lesser General Public 016// License along with this library; if not, write to the Free Software 017// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 018//////////////////////////////////////////////////////////////////////////////// 019 020package com.puppycrawl.tools.checkstyle.api; 021 022import java.util.Arrays; 023 024/** 025 * Representation of the comment block. 026 * 027 */ 028public class Comment implements TextBlock { 029 030 /** Text of the comment. */ 031 private final String[] text; 032 033 /** Number of first line of the comment. */ 034 private final int startLineNo; 035 036 /** Number of last line of the comment. */ 037 private final int endLineNo; 038 039 /** Number of first column of the comment. */ 040 private final int startColNo; 041 042 /** Number of last column of the comment. */ 043 private final int endColNo; 044 045 /** 046 * Creates new instance. 047 * @param text the lines that make up the comment. 048 * @param firstCol number of the first column of the comment. 049 * @param lastLine number of the last line of the comment. 050 * @param lastCol number of the last column of the comment. 051 */ 052 public Comment(final String[] text, final int firstCol, 053 final int lastLine, final int lastCol) { 054 this.text = text.clone(); 055 startLineNo = lastLine - text.length + 1; 056 endLineNo = lastLine; 057 startColNo = firstCol; 058 endColNo = lastCol; 059 } 060 061 @Override 062 public final String[] getText() { 063 return text.clone(); 064 } 065 066 @Override 067 public final int getStartLineNo() { 068 return startLineNo; 069 } 070 071 @Override 072 public final int getEndLineNo() { 073 return endLineNo; 074 } 075 076 @Override 077 public int getStartColNo() { 078 return startColNo; 079 } 080 081 @Override 082 public int getEndColNo() { 083 return endColNo; 084 } 085 086 @Override 087 public boolean intersects(int startLine, int startCol, 088 int endLine, int endCol) { 089 // compute a single number for start and end 090 // to simplify conditional logic 091 final long multiplier = Integer.MAX_VALUE; 092 final long thisStart = startLineNo * multiplier + startColNo; 093 final long thisEnd = endLineNo * multiplier + endColNo; 094 final long inStart = startLine * multiplier + startCol; 095 final long inEnd = endLine * multiplier + endCol; 096 097 return thisEnd >= inStart && inEnd >= thisStart; 098 } 099 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}