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[] mText;
030
031    /** number of first line of the comment. */
032    private final int mFirstLine;
033
034    /** number of last line of the comment. */
035    private final int mLastLine;
036
037    /** number of first column of the comment. */
038    private final int mFirstCol;
039
040    /** number of last column of the comment. */
041    private final int mLastCol;
042
043    /**
044     * Creates new instance.
045     * @param aText the lines that make up the comment.
046     * @param aFirstCol number of the first column of the comment.
047     * @param aLastLine number of the last line of the comment.
048     * @param aLastCol number of the last column of the comment.
049     */
050    public Comment(final String[] aText, final int aFirstCol,
051            final int aLastLine, final int aLastCol)
052    {
053        mText = new String[aText.length];
054        System.arraycopy(aText, 0, mText, 0, mText.length);
055        mFirstLine = aLastLine - mText.length + 1;
056        mLastLine = aLastLine;
057        mFirstCol = aFirstCol;
058        mLastCol = aLastCol;
059    }
060
061    /** {@inheritDoc} */
062    public final String[] getText()
063    {
064        return mText.clone();
065    }
066
067    /** {@inheritDoc} */
068    public final int getStartLineNo()
069    {
070        return mFirstLine;
071    }
072
073    /** {@inheritDoc} */
074    public final int getEndLineNo()
075    {
076        return mLastLine;
077    }
078
079    /** {@inheritDoc} */
080    public int getStartColNo()
081    {
082        return mFirstCol;
083    }
084
085    /** {@inheritDoc} */
086    public int getEndColNo()
087    {
088        return mLastCol;
089    }
090
091    /** {@inheritDoc} */
092    public boolean intersects(int aStartLineNo, int aStartColNo,
093                              int aEndLineNo, int aEndColNo)
094    {
095        // compute a single number for start and end
096        // to simpify conditional logic
097        final long multiplier = Integer.MAX_VALUE;
098        final long thisStart = mFirstLine * multiplier + mFirstCol;
099        final long thisEnd = mLastLine * multiplier + mLastCol;
100        final long inStart = aStartLineNo * multiplier + aStartColNo;
101        final long inEnd = aEndLineNo * multiplier + aEndColNo;
102
103        return !((thisEnd < inStart) || (inEnd < thisStart));
104    }
105
106    @Override
107    public String toString()
108    {
109        return "Comment[" + mFirstLine + ":" + mFirstCol + "-"
110            + mLastLine + ":" + mLastCol + "]";
111    }
112}