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