001///////////////////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
003// Copyright (C) 2001-2025 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.FileStatefulCheck;
023import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
024import com.puppycrawl.tools.checkstyle.api.DetailAST;
025import com.puppycrawl.tools.checkstyle.api.TokenTypes;
026
027/**
028 * <div>
029 * Checks for the number of types declared at the <i>outer</i> (or <i>root</i>) level in a file.
030 * </div>
031 *
032 * <p>
033 * Rationale: It is considered good practice to only define one outer type per file.
034 * </p>
035 *
036 * @since 5.0
037 */
038@FileStatefulCheck
039public class OuterTypeNumberCheck extends AbstractCheck {
040
041    /**
042     * A key is pointing to the warning message text in "messages.properties"
043     * file.
044     */
045    public static final String MSG_KEY = "maxOuterTypes";
046
047    /** Specify the maximum number of outer types allowed. */
048    private int max = 1;
049    /** Tracks the current depth in types. */
050    private int currentDepth;
051    /** Tracks the number of outer types found. */
052    private int outerNum;
053
054    @Override
055    public int[] getDefaultTokens() {
056        return getRequiredTokens();
057    }
058
059    @Override
060    public int[] getAcceptableTokens() {
061        return getRequiredTokens();
062    }
063
064    @Override
065    public int[] getRequiredTokens() {
066        return new int[] {
067            TokenTypes.CLASS_DEF,
068            TokenTypes.INTERFACE_DEF,
069            TokenTypes.ENUM_DEF,
070            TokenTypes.ANNOTATION_DEF,
071            TokenTypes.RECORD_DEF,
072        };
073    }
074
075    @Override
076    public void beginTree(DetailAST ast) {
077        currentDepth = 0;
078        outerNum = 0;
079    }
080
081    @Override
082    public void finishTree(DetailAST ast) {
083        if (max < outerNum) {
084            log(ast, MSG_KEY, outerNum, max);
085        }
086    }
087
088    @Override
089    public void visitToken(DetailAST ast) {
090        if (currentDepth == 0) {
091            outerNum++;
092        }
093        currentDepth++;
094    }
095
096    @Override
097    public void leaveToken(DetailAST ast) {
098        currentDepth--;
099    }
100
101    /**
102     * Setter to specify the maximum number of outer types allowed.
103     *
104     * @param max the new number.
105     * @since 5.0
106     */
107    public void setMax(int max) {
108        this.max = max;
109    }
110
111}