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