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