View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2024 the original author or authors.
4   //
5   // This library is free software; you can redistribute it and/or
6   // modify it under the terms of the GNU Lesser General Public
7   // License as published by the Free Software Foundation; either
8   // version 2.1 of the License, or (at your option) any later version.
9   //
10  // This library is distributed in the hope that it will be useful,
11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  // Lesser General Public License for more details.
14  //
15  // You should have received a copy of the GNU Lesser General Public
16  // License along with this library; if not, write to the Free Software
17  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  ///////////////////////////////////////////////////////////////////////////////////////////////
19  
20  package com.puppycrawl.tools.checkstyle;
21  
22  import java.io.Serializable;
23  
24  /**
25   * Thread mode settings for the checkstyle modules.
26   *
27   * @noinspection SerializableHasSerializationMethods
28   * @noinspectionreason SerializableHasSerializationMethods - we only need serialVersionUID
29   *      to differentiate between threads
30   */
31  public class ThreadModeSettings implements Serializable {
32  
33      /** A checker module name. */
34      public static final String CHECKER_MODULE_NAME = Checker.class.getSimpleName();
35  
36      /** A multi thread checker module name. */
37      public static final String MULTI_THREAD_CHECKER_MODULE_NAME =
38              Checker.class.getSimpleName();
39  
40      /** A three walker module name. */
41      public static final String TREE_WALKER_MODULE_NAME = TreeWalker.class.getSimpleName();
42  
43      /** A multi thread three walker module name. */
44      public static final String MULTI_THREAD_TREE_WALKER_MODULE_NAME =
45              TreeWalker.class.getSimpleName();
46  
47      /** A single thread mode settings instance. */
48      public static final ThreadModeSettings SINGLE_THREAD_MODE_INSTANCE =
49              new ThreadModeSettings(1, 1);
50  
51      /** A unique serial version identifier. */
52      private static final long serialVersionUID = 1L;
53  
54      /** The checker threads number. */
55      private final int checkerThreadsNumber;
56      /** The tree walker threads number. */
57      private final int treeWalkerThreadsNumber;
58  
59      /**
60       * Initializes the thread mode configuration.
61       *
62       * @param checkerThreadsNumber the Checker threads number
63       * @param treeWalkerThreadsNumber the TreeWalker threads number
64       */
65      public ThreadModeSettings(int checkerThreadsNumber, int treeWalkerThreadsNumber) {
66          this.checkerThreadsNumber = checkerThreadsNumber;
67          this.treeWalkerThreadsNumber = treeWalkerThreadsNumber;
68      }
69  
70      /**
71       * Gets the number of threads for the Checker module.
72       *
73       * @return the number of threads for the Checker module.
74       */
75      public int getCheckerThreadsNumber() {
76          return checkerThreadsNumber;
77      }
78  
79      /**
80       * Gets the number of threads for the TreeWalker module.
81       *
82       * @return the number of threads for the TreeWalker module.
83       */
84      public int getTreeWalkerThreadsNumber() {
85          return treeWalkerThreadsNumber;
86      }
87  
88      /**
89       * Resolves the module name according to the thread settings.
90       *
91       * @param name The original module name.
92       * @return resolved module name.
93       * @throws IllegalArgumentException when name is Checker or TreeWalker
94       */
95      public final String resolveName(String name) {
96          if (checkerThreadsNumber > 1) {
97              if (CHECKER_MODULE_NAME.equals(name)) {
98                  throw new IllegalArgumentException(
99                          "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 }