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.checks.coding;
020
021import com.puppycrawl.tools.checkstyle.api.DetailAST;
022import com.puppycrawl.tools.checkstyle.api.TokenTypes;
023import com.puppycrawl.tools.checkstyle.checks.CheckUtils;
024
025/**
026 * Restricts nested if-else blocks to a specified depth (default = 1).
027 *
028 * @author <a href="mailto:simon@redhillconsulting.com.au">Simon Harris</a>
029 */
030public final class NestedIfDepthCheck extends AbstractNestedDepthCheck
031{
032    /** default allowed nesting depth. */
033    private static final int DEFAULT_MAX = 1;
034
035    /** Creates new check instance with default allowed nesting depth. */
036    public NestedIfDepthCheck()
037    {
038        super(DEFAULT_MAX);
039    }
040
041    @Override
042    public int[] getDefaultTokens()
043    {
044        return new int[] {TokenTypes.LITERAL_IF};
045    }
046
047    @Override
048    public void visitToken(DetailAST ast)
049    {
050        switch (ast.getType()) {
051            case TokenTypes.LITERAL_IF:
052                visitLiteralIf(ast);
053                break;
054            default:
055                throw new IllegalStateException(ast.toString());
056        }
057    }
058
059    @Override
060    public void leaveToken(DetailAST ast)
061    {
062        switch (ast.getType()) {
063            case TokenTypes.LITERAL_IF:
064                leaveLiteralIf(ast);
065                break;
066            default:
067                throw new IllegalStateException(ast.toString());
068        }
069    }
070
071    /**
072     * Increases current nesting depth.
073     * @param literalIf node for if.
074     */
075    private void visitLiteralIf(DetailAST literalIf)
076    {
077        if (!CheckUtils.isElseIf(literalIf)) {
078            nestIn(literalIf, "nested.if.depth");
079        }
080    }
081
082    /**
083     * Decreases current nesting depth.
084     * @param literalIf node for if.
085     */
086    private void leaveLiteralIf(DetailAST literalIf)
087    {
088        if (!CheckUtils.isElseIf(literalIf)) {
089            nestOut();
090        }
091    }
092}