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.sizes;
021
022import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
023import com.puppycrawl.tools.checkstyle.api.DetailAST;
024import com.puppycrawl.tools.checkstyle.api.TokenTypes;
025
026/**
027 * Checks for the number of defined types at the "outer" level.
028 * @author oliverb
029 */
030public class OuterTypeNumberCheck extends AbstractCheck {
031
032    /**
033     * A key is pointing to the warning message text in "messages.properties"
034     * file.
035     */
036    public static final String MSG_KEY = "maxOuterTypes";
037
038    /** The maximum allowed number of outer types. */
039    private int max = 1;
040    /** Tracks the current depth in types. */
041    private int currentDepth;
042    /** Tracks the number of outer types found. */
043    private int outerNum;
044
045    @Override
046    public int[] getDefaultTokens() {
047        return getAcceptableTokens();
048    }
049
050    @Override
051    public int[] getAcceptableTokens() {
052        return new int[] {TokenTypes.CLASS_DEF, TokenTypes.INTERFACE_DEF,
053            TokenTypes.ENUM_DEF, TokenTypes.ANNOTATION_DEF, };
054    }
055
056    @Override
057    public int[] getRequiredTokens() {
058        return getAcceptableTokens();
059    }
060
061    @Override
062    public void beginTree(DetailAST ast) {
063        currentDepth = 0;
064        outerNum = 0;
065    }
066
067    @Override
068    public void finishTree(DetailAST ast) {
069        if (max < outerNum) {
070            log(ast, MSG_KEY, outerNum, max);
071        }
072    }
073
074    @Override
075    public void visitToken(DetailAST ast) {
076        if (currentDepth == 0) {
077            outerNum++;
078        }
079        currentDepth++;
080    }
081
082    @Override
083    public void leaveToken(DetailAST ast) {
084        currentDepth--;
085    }
086
087    /**
088     * Sets the maximum allowed number of outer types.
089     * @param max the new number.
090     */
091    public void setMax(int max) {
092        this.max = max;
093    }
094}