001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2018 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 * @author Andrew Kuchev
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     * @param checkerThreadsNumber the Checker threads number
059     * @param treeWalkerThreadsNumber the TreeWalker threads number
060     */
061    public ThreadModeSettings(int checkerThreadsNumber, int treeWalkerThreadsNumber) {
062        this.checkerThreadsNumber = checkerThreadsNumber;
063        this.treeWalkerThreadsNumber = treeWalkerThreadsNumber;
064    }
065
066    /**
067     * Gets the number of threads for the Checker module.
068     * @return the number of threads for the Checker module.
069     */
070    public int getCheckerThreadsNumber() {
071        return checkerThreadsNumber;
072    }
073
074    /**
075     * Gets the number of threads for the TreeWalker module.
076     * @return the number of threads for the TreeWalker module.
077     */
078    public int getTreeWalkerThreadsNumber() {
079        return treeWalkerThreadsNumber;
080    }
081
082    /**
083     * Resolves the module name according to the thread settings.
084     * @param name The original module name.
085     * @return resolved module name.
086     */
087    public final String resolveName(String name) {
088        if (checkerThreadsNumber > 1) {
089            if (CHECKER_MODULE_NAME.equals(name)) {
090                throw new IllegalArgumentException(
091                        "Multi thread mode for Checker module is not implemented");
092            }
093            if (TREE_WALKER_MODULE_NAME.equals(name)) {
094                throw new IllegalArgumentException(
095                        "Multi thread mode for TreeWalker module is not implemented");
096            }
097        }
098
099        return name;
100    }
101
102}