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 * @author o_sukhodolsky
028 */
029public class Comment implements TextBlock {
030
031    /** Text of the comment. */
032    private final String[] text;
033
034    /** Number of first line of the comment. */
035    private final int startLineNo;
036
037    /** Number of last line of the comment. */
038    private final int endLineNo;
039
040    /** Number of first column of the comment. */
041    private final int startColNo;
042
043    /** Number of last column of the comment. */
044    private final int endColNo;
045
046    /**
047     * Creates new instance.
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 = new String[text.length];
056        System.arraycopy(text, 0, this.text, 0, this.text.length);
057        startLineNo = lastLine - this.text.length + 1;
058        endLineNo = lastLine;
059        startColNo = firstCol;
060        endColNo = lastCol;
061    }
062
063    @Override
064    public final String[] getText() {
065        return text.clone();
066    }
067
068    @Override
069    public final int getStartLineNo() {
070        return startLineNo;
071    }
072
073    @Override
074    public final int getEndLineNo() {
075        return endLineNo;
076    }
077
078    @Override
079    public int getStartColNo() {
080        return startColNo;
081    }
082
083    @Override
084    public int getEndColNo() {
085        return endColNo;
086    }
087
088    @Override
089    public boolean intersects(int startLine, int startCol,
090                              int endLine, int endCol) {
091        // compute a single number for start and end
092        // to simplify conditional logic
093        final long multiplier = Integer.MAX_VALUE;
094        final long thisStart = startLineNo * multiplier + startColNo;
095        final long thisEnd = endLineNo * multiplier + endColNo;
096        final long inStart = startLine * multiplier + startCol;
097        final long inEnd = endLine * multiplier + endCol;
098
099        return thisEnd >= inStart && inEnd >= thisStart;
100    }
101
102    @Override
103    public String toString() {
104        return "Comment[text=" + Arrays.toString(text)
105                + ", startLineNo=" + startLineNo
106                + ", endLineNo=" + endLineNo
107                + ", startColNo=" + startColNo
108                + ", endColNo=" + endColNo + ']';
109    }
110
111}