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////////////////////////////////////////////////////////////////////////////////
019
020package com.puppycrawl.tools.checkstyle.checks.whitespace;
021
022import com.puppycrawl.tools.checkstyle.api.DetailAST;
023import com.puppycrawl.tools.checkstyle.api.TokenTypes;
024import com.puppycrawl.tools.checkstyle.checks.AbstractOptionCheck;
025
026/**
027 * <p>Checks the padding of an empty for iterator; that is whether a
028 * space is required at an empty for iterator, or such spaces are
029 * forbidden. No check occurs if there is a line wrap at the iterator, as in
030 * </p>
031 * <pre class="body">
032for (Iterator foo = very.long.line.iterator();
033      foo.hasNext();
034     )
035   </pre>
036 * <p>
037 * The policy to verify is specified using the {@link PadOption} class and
038 * defaults to {@link PadOption#NOSPACE}.
039 * </p>
040 * <p>
041 * An example of how to configure the check is:
042 * </p>
043 * <pre>
044 * &lt;module name="EmptyForIteratorPad"/&gt;
045 * </pre>
046 *
047 * @author Rick Giles
048 * @version 1.0
049 */
050public class EmptyForIteratorPadCheck
051    extends AbstractOptionCheck<PadOption>
052{
053    /**
054     * Sets the paren pad otion to nospace.
055     */
056    public EmptyForIteratorPadCheck()
057    {
058        super(PadOption.NOSPACE, PadOption.class);
059    }
060
061    @Override
062    public int[] getDefaultTokens()
063    {
064        return new int[] {TokenTypes.FOR_ITERATOR,
065        };
066    }
067
068    @Override
069    public void visitToken(DetailAST ast)
070    {
071        if (ast.getChildCount() == 0) {
072            //empty for iterator. test pad after semi.
073            final DetailAST semi = ast.getPreviousSibling();
074            final String line = getLines()[semi.getLineNo() - 1];
075            final int after = semi.getColumnNo() + 1;
076            //don't check if at end of line
077            if (after < line.length()) {
078                if ((PadOption.NOSPACE == getAbstractOption())
079                    && (Character.isWhitespace(line.charAt(after))))
080                {
081                    log(semi.getLineNo(), after, "ws.followed", ";");
082                }
083                else if ((PadOption.SPACE == getAbstractOption())
084                         && !Character.isWhitespace(line.charAt(after)))
085                {
086                    log(semi.getLineNo(), after, "ws.notFollowed", ";");
087                }
088            }
089        }
090    }
091}