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;
021
022import java.io.Serial;
023import java.io.Serializable;
024
025/**
026 * Thread mode settings for the checkstyle modules.
027 *
028 * @param checkerThreadsNumber the Checker threads number
029 * @param treeWalkerThreadsNumber the TreeWalker threads number
030 *
031 * @noinspectionreason SerializableHasSerializationMethods - we only need serialVersionUID
032 * to differentiate between threads
033 */
034public record ThreadModeSettings(int checkerThreadsNumber,
035 int treeWalkerThreadsNumber) implements Serializable {
036
037 /** A checker module name. */
038 public static final String CHECKER_MODULE_NAME = Checker.class.getSimpleName();
039
040 /** A multi thread checker module name. */
041 public static final String MULTI_THREAD_CHECKER_MODULE_NAME =
042 Checker.class.getSimpleName();
043
044 /** A three walker module name. */
045 public static final String TREE_WALKER_MODULE_NAME = TreeWalker.class.getSimpleName();
046
047 /** A multi thread three walker module name. */
048 public static final String MULTI_THREAD_TREE_WALKER_MODULE_NAME =
049 TreeWalker.class.getSimpleName();
050
051 /** A single thread mode settings instance. */
052 public static final ThreadModeSettings SINGLE_THREAD_MODE_INSTANCE =
053 new ThreadModeSettings(1, 1);
054
055 /** A unique serial version identifier. */
056 @Serial
057 private static final long serialVersionUID = 1L;
058
059 /**
060 * Resolves the module name according to the thread settings.
061 *
062 * @param name The original module name.
063 * @return resolved module name.
064 * @throws IllegalArgumentException when name is Checker or TreeWalker
065 */
066 public String resolveName(String name) {
067 if (checkerThreadsNumber > 1) {
068 if (CHECKER_MODULE_NAME.equals(name)) {
069 throw new IllegalArgumentException(
070 "Multi thread mode for Checker module is not implemented");
071 }
072 if (TREE_WALKER_MODULE_NAME.equals(name)) {
073 throw new IllegalArgumentException(
074 "Multi thread mode for TreeWalker module is not implemented");
075 }
076 }
077
078 return name;
079 }
080}