001///////////////////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
003// Copyright (C) 2001-2024 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     *
048     * @param text the lines that make up the comment.
049     * @param firstCol number of the first column of the comment.
050     * @param lastLine number of the last line of the comment.
051     * @param lastCol number of the last column of the comment.
052     */
053    public Comment(final String[] text, final int firstCol,
054            final int lastLine, final int lastCol) {
055        this.text = text.clone();
056        startLineNo = lastLine - text.length + 1;
057        endLineNo = lastLine;
058        startColNo = firstCol;
059        endColNo = lastCol;
060    }
061
062    @Override
063    public final String[] getText() {
064        return text.clone();
065    }
066
067    @Override
068    public final int getStartLineNo() {
069        return startLineNo;
070    }
071
072    @Override
073    public final int getEndLineNo() {
074        return endLineNo;
075    }
076
077    @Override
078    public int getStartColNo() {
079        return startColNo;
080    }
081
082    @Override
083    public int getEndColNo() {
084        return endColNo;
085    }
086
087    @Override
088    public boolean intersects(int startLine, int startCol,
089                              int endLine, int endCol) {
090        // compute a single number for start and end
091        // to simplify conditional logic
092        final long multiplier = Integer.MAX_VALUE;
093        final long thisStart = startLineNo * multiplier + startColNo;
094        final long thisEnd = endLineNo * multiplier + endColNo;
095        final long inStart = startLine * multiplier + startCol;
096        final long inEnd = endLine * multiplier + endCol;
097
098        return thisEnd >= inStart && inEnd >= thisStart;
099    }
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}