001///////////////////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
003// Copyright (C) 2001-2024 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;
021
022import java.io.Serializable;
023
024/**
025 * Thread mode settings for the checkstyle modules.
026 *
027 * @noinspection SerializableHasSerializationMethods
028 * @noinspectionreason SerializableHasSerializationMethods - we only need serialVersionUID
029 *      to differentiate between threads
030 */
031public class ThreadModeSettings implements Serializable {
032
033    /** A checker module name. */
034    public static final String CHECKER_MODULE_NAME = Checker.class.getSimpleName();
035
036    /** A multi thread checker module name. */
037    public static final String MULTI_THREAD_CHECKER_MODULE_NAME =
038            Checker.class.getSimpleName();
039
040    /** A three walker module name. */
041    public static final String TREE_WALKER_MODULE_NAME = TreeWalker.class.getSimpleName();
042
043    /** A multi thread three walker module name. */
044    public static final String MULTI_THREAD_TREE_WALKER_MODULE_NAME =
045            TreeWalker.class.getSimpleName();
046
047    /** A single thread mode settings instance. */
048    public static final ThreadModeSettings SINGLE_THREAD_MODE_INSTANCE =
049            new ThreadModeSettings(1, 1);
050
051    /** A unique serial version identifier. */
052    private static final long serialVersionUID = 1L;
053
054    /** The checker threads number. */
055    private final int checkerThreadsNumber;
056    /** The tree walker threads number. */
057    private final int treeWalkerThreadsNumber;
058
059    /**
060     * Initializes the thread mode configuration.
061     *
062     * @param checkerThreadsNumber the Checker threads number
063     * @param treeWalkerThreadsNumber the TreeWalker threads number
064     */
065    public ThreadModeSettings(int checkerThreadsNumber, int treeWalkerThreadsNumber) {
066        this.checkerThreadsNumber = checkerThreadsNumber;
067        this.treeWalkerThreadsNumber = treeWalkerThreadsNumber;
068    }
069
070    /**
071     * Gets the number of threads for the Checker module.
072     *
073     * @return the number of threads for the Checker module.
074     */
075    public int getCheckerThreadsNumber() {
076        return checkerThreadsNumber;
077    }
078
079    /**
080     * Gets the number of threads for the TreeWalker module.
081     *
082     * @return the number of threads for the TreeWalker module.
083     */
084    public int getTreeWalkerThreadsNumber() {
085        return treeWalkerThreadsNumber;
086    }
087
088    /**
089     * Resolves the module name according to the thread settings.
090     *
091     * @param name The original module name.
092     * @return resolved module name.
093     * @throws IllegalArgumentException when name is Checker or TreeWalker
094     */
095    public final String resolveName(String name) {
096        if (checkerThreadsNumber > 1) {
097            if (CHECKER_MODULE_NAME.equals(name)) {
098                throw new IllegalArgumentException(
099                        "Multi thread mode for Checker module is not implemented");
100            }
101            if (TREE_WALKER_MODULE_NAME.equals(name)) {
102                throw new IllegalArgumentException(
103                        "Multi thread mode for TreeWalker module is not implemented");
104            }
105        }
106
107        return name;
108    }
109
110}