001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2020 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    private static final long serialVersionUID = 1L;
050
051    /** The checker threads number. */
052    private final int checkerThreadsNumber;
053    /** The tree walker threads number. */
054    private final int treeWalkerThreadsNumber;
055
056    /**
057     * Initializes the thread mode configuration.
058     *
059     * @param checkerThreadsNumber the Checker threads number
060     * @param treeWalkerThreadsNumber the TreeWalker threads number
061     */
062    public ThreadModeSettings(int checkerThreadsNumber, int treeWalkerThreadsNumber) {
063        this.checkerThreadsNumber = checkerThreadsNumber;
064        this.treeWalkerThreadsNumber = treeWalkerThreadsNumber;
065    }
066
067    /**
068     * Gets the number of threads for the Checker module.
069     *
070     * @return the number of threads for the Checker module.
071     */
072    public int getCheckerThreadsNumber() {
073        return checkerThreadsNumber;
074    }
075
076    /**
077     * Gets the number of threads for the TreeWalker module.
078     *
079     * @return the number of threads for the TreeWalker module.
080     */
081    public int getTreeWalkerThreadsNumber() {
082        return treeWalkerThreadsNumber;
083    }
084
085    /**
086     * Resolves the module name according to the thread settings.
087     *
088     * @param name The original module name.
089     * @return resolved module name.
090     * @throws IllegalArgumentException when name is Checker or TreeWalker
091     */
092    public final String resolveName(String name) {
093        if (checkerThreadsNumber > 1) {
094            if (CHECKER_MODULE_NAME.equals(name)) {
095                throw new IllegalArgumentException(
096                        "Multi thread mode for Checker module is not implemented");
097            }
098            if (TREE_WALKER_MODULE_NAME.equals(name)) {
099                throw new IllegalArgumentException(
100                        "Multi thread mode for TreeWalker module is not implemented");
101            }
102        }
103
104        return name;
105    }
106
107}