001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2017 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
022/**
023 * Representation of the comment block.
024 *
025 * @author o_sukhodolsky
026 */
027public class Comment implements TextBlock {
028    /** Text of the comment. */
029    private final String[] text;
030
031    /** Number of first line of the comment. */
032    private final int startLineNo;
033
034    /** Number of last line of the comment. */
035    private final int endLineNo;
036
037    /** Number of first column of the comment. */
038    private final int startColNo;
039
040    /** Number of last column of the comment. */
041    private final int endColNo;
042
043    /**
044     * Creates new instance.
045     * @param text the lines that make up the comment.
046     * @param firstCol number of the first column of the comment.
047     * @param lastLine number of the last line of the comment.
048     * @param lastCol number of the last column of the comment.
049     */
050    public Comment(final String[] text, final int firstCol,
051            final int lastLine, final int lastCol) {
052        this.text = new String[text.length];
053        System.arraycopy(text, 0, this.text, 0, this.text.length);
054        startLineNo = lastLine - this.text.length + 1;
055        endLineNo = lastLine;
056        startColNo = firstCol;
057        endColNo = lastCol;
058    }
059
060    @Override
061    public final String[] getText() {
062        return text.clone();
063    }
064
065    @Override
066    public final int getStartLineNo() {
067        return startLineNo;
068    }
069
070    @Override
071    public final int getEndLineNo() {
072        return endLineNo;
073    }
074
075    @Override
076    public int getStartColNo() {
077        return startColNo;
078    }
079
080    @Override
081    public int getEndColNo() {
082        return endColNo;
083    }
084
085    @Override
086    public boolean intersects(int startLine, int startCol,
087                              int endLine, int endCol) {
088        // compute a single number for start and end
089        // to simplify conditional logic
090        final long multiplier = Integer.MAX_VALUE;
091        final long thisStart = startLineNo * multiplier + startColNo;
092        final long thisEnd = endLineNo * multiplier + endColNo;
093        final long inStart = startLine * multiplier + startCol;
094        final long inEnd = endLine * multiplier + endCol;
095
096        return thisEnd >= inStart && inEnd >= thisStart;
097    }
098
099    @Override
100    public String toString() {
101        final String separator = ":";
102        return "Comment[" + startLineNo + separator + startColNo + "-"
103            + endLineNo + separator + endColNo + "]";
104    }
105}