001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2015 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.checks.coding;
021
022import com.puppycrawl.tools.checkstyle.api.Check;
023import com.puppycrawl.tools.checkstyle.api.DetailAST;
024
025/**
026 * Abstract class which provides helpers functionality for nested checks.
027 * @deprecated Checkstyle will not support abstract checks anymore. Use {@link Check} instead.
028 * @author <a href="mailto:simon@redhillconsulting.com.au">Simon Harris</a>
029 * @noinspection AbstractClassNeverImplemented
030 */
031@Deprecated
032public abstract class AbstractNestedDepthCheck extends Check {
033    /** Maximum allowed nesting depth. */
034    private int max;
035    /** Current nesting depth. */
036    private int depth;
037
038    /**
039     * Creates new instance of checks.
040     * @param max default allowed nesting depth.
041     */
042    protected AbstractNestedDepthCheck(int max) {
043        this.max = max;
044    }
045
046    @Override
047    public final int[] getRequiredTokens() {
048        return getDefaultTokens();
049    }
050
051    @Override
052    public void beginTree(DetailAST rootAST) {
053        depth = 0;
054    }
055
056    /**
057     * Setter for maximum allowed nesting depth.
058     * @param max maximum allowed nesting depth.
059     */
060    public final void setMax(int max) {
061        this.max = max;
062    }
063
064    /**
065     * Increasing current nesting depth.
066     * @param ast note which increases nesting.
067     * @param messageId message id for logging error.
068     */
069    protected final void nestIn(DetailAST ast, String messageId) {
070        if (depth > max) {
071            log(ast, messageId, depth, max);
072        }
073        ++depth;
074    }
075
076    /** Decreasing current nesting depth. */
077    protected final void nestOut() {
078        --depth;
079    }
080}