View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2025 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.Serial;
23  import java.util.ArrayList;
24  import java.util.HashMap;
25  import java.util.List;
26  import java.util.Map;
27  import java.util.Set;
28  
29  import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
30  import com.puppycrawl.tools.checkstyle.api.Configuration;
31  import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
32  
33  /**
34   * Default implementation of the Configuration interface.
35   */
36  public final class DefaultConfiguration implements Configuration {
37  
38      /** A unique serial version identifier. */
39      @Serial
40      private static final long serialVersionUID = 1157875385356127169L;
41  
42      /** Constant for optimization. */
43      private static final Configuration[] EMPTY_CONFIGURATION_ARRAY = new Configuration[0];
44  
45      /** The name of this configuration. */
46      private final String name;
47  
48      /** The list of child Configurations. */
49      private final List<Configuration> children = new ArrayList<>();
50  
51      /** The map from property names to property values. */
52      private final Map<String, String> propertyMap = new HashMap<>();
53  
54      /** The map containing custom messages. */
55      private final Map<String, String> messages = new HashMap<>();
56  
57      /** The thread mode configuration. */
58      private final ThreadModeSettings threadModeSettings;
59  
60      /**
61       * Instantiates a DefaultConfiguration.
62       *
63       * @param name the name for this DefaultConfiguration.
64       */
65      public DefaultConfiguration(String name) {
66          this(name, ThreadModeSettings.SINGLE_THREAD_MODE_INSTANCE);
67      }
68  
69      /**
70       * Instantiates a DefaultConfiguration.
71       *
72       * @param name the name for this DefaultConfiguration.
73       * @param threadModeSettings the thread mode configuration.
74       */
75      public DefaultConfiguration(String name,
76          ThreadModeSettings threadModeSettings) {
77          this.name = name;
78          this.threadModeSettings = threadModeSettings;
79      }
80  
81      @Override
82      public String[] getAttributeNames() {
83          return getPropertyNames();
84      }
85  
86      @Override
87      public String getAttribute(String attributeName) throws CheckstyleException {
88          return getProperty(attributeName);
89      }
90  
91      @Override
92      public String[] getPropertyNames() {
93          final Set<String> keySet = propertyMap.keySet();
94          return keySet.toArray(CommonUtil.EMPTY_STRING_ARRAY);
95      }
96  
97      @Override
98      public String getProperty(String propertyName) throws CheckstyleException {
99          if (!propertyMap.containsKey(propertyName)) {
100             throw new CheckstyleException(
101                     "missing key '" + propertyName + "' in " + name);
102         }
103         return propertyMap.get(propertyName);
104     }
105 
106     @Override
107     public Configuration[] getChildren() {
108         return children.toArray(
109                 EMPTY_CONFIGURATION_ARRAY);
110     }
111 
112     @Override
113     public String getName() {
114         return name;
115     }
116 
117     /**
118      * Makes a configuration a child of this configuration.
119      *
120      * @param configuration the child configuration.
121      */
122     public void addChild(Configuration configuration) {
123         children.add(configuration);
124     }
125 
126     /**
127      * Removes a child of this configuration.
128      *
129      * @param configuration the child configuration to remove.
130      */
131     public void removeChild(final Configuration configuration) {
132         children.remove(configuration);
133     }
134 
135     /**
136      * Adds n property to this configuration.
137      *
138      * @param attributeName the name of the property.
139      * @param value the value of the property.
140      * @deprecated This shall be removed in future releases. Please use
141      *      {@code addProperty(String propertyName, String value)} instead.
142      */
143     @Deprecated(since = "8.45")
144     public void addAttribute(String attributeName, String value) {
145         addProperty(attributeName, value);
146     }
147 
148     /**
149      * Adds n property to this configuration.
150      *
151      * @param propertyName the name of the property.
152      * @param value the value of the property.
153      */
154     public void addProperty(String propertyName, String value) {
155         final String current = propertyMap.get(propertyName);
156         final String newValue;
157         if (current == null) {
158             newValue = value;
159         }
160         else {
161             newValue = current + "," + value;
162         }
163         propertyMap.put(propertyName, newValue);
164     }
165 
166     /**
167      * Adds a custom message to this configuration.
168      *
169      * @param key the message key
170      * @param value the custom message pattern
171      */
172     public void addMessage(String key, String value) {
173         messages.put(key, value);
174     }
175 
176     /**
177      * Returns an unmodifiable map instance containing the custom messages
178      * for this configuration.
179      *
180      * @return unmodifiable map containing custom messages
181      */
182     @Override
183     public Map<String, String> getMessages() {
184         return new HashMap<>(messages);
185     }
186 
187     /**
188      * Gets the thread mode configuration.
189      *
190      * @return the thread mode configuration.
191      */
192     public ThreadModeSettings getThreadModeSettings() {
193         return threadModeSettings;
194     }
195 
196 }