001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2014  Oliver Burn
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////////////////////////////////////////////////////////////////////////////////
019package com.puppycrawl.tools.checkstyle.api;
020
021/**
022 * Representation of the comment block.
023 *
024 * @author o_sukhodolsky
025 */
026public class Comment implements TextBlock
027{
028    /** text of the comment. */
029    private final String[] text;
030
031    /** number of first line of the comment. */
032    private final int firstLine;
033
034    /** number of last line of the comment. */
035    private final int lastLine;
036
037    /** number of first column of the comment. */
038    private final int firstCol;
039
040    /** number of last column of the comment. */
041    private final int lastCol;
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    {
053        this.text = new String[text.length];
054        System.arraycopy(text, 0, this.text, 0, this.text.length);
055        firstLine = lastLine - this.text.length + 1;
056        this.lastLine = lastLine;
057        this.firstCol = firstCol;
058        this.lastCol = lastCol;
059    }
060
061    /** {@inheritDoc} */
062    @Override
063    public final String[] getText()
064    {
065        return text.clone();
066    }
067
068    /** {@inheritDoc} */
069    @Override
070    public final int getStartLineNo()
071    {
072        return firstLine;
073    }
074
075    /** {@inheritDoc} */
076    @Override
077    public final int getEndLineNo()
078    {
079        return lastLine;
080    }
081
082    /** {@inheritDoc} */
083    @Override
084    public int getStartColNo()
085    {
086        return firstCol;
087    }
088
089    /** {@inheritDoc} */
090    @Override
091    public int getEndColNo()
092    {
093        return lastCol;
094    }
095
096    /** {@inheritDoc} */
097    @Override
098    public boolean intersects(int startLineNo, int startColNo,
099                              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}