1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2019 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   * @noinspection SerializableHasSerializationMethods
35   */
36  public final class DefaultConfiguration implements Configuration {
37  
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 attribute names to attribute values. */
50      private final Map<String, String> attributeMap = 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       * @param name the name for this DefaultConfiguration.
61       */
62      public DefaultConfiguration(String name) {
63          this(name, ThreadModeSettings.SINGLE_THREAD_MODE_INSTANCE);
64      }
65  
66      /**
67       * Instantiates a DefaultConfiguration.
68       * @param name the name for this DefaultConfiguration.
69       * @param threadModeSettings the thread mode configuration.
70       */
71      public DefaultConfiguration(String name,
72          ThreadModeSettings threadModeSettings) {
73          this.name = name;
74          this.threadModeSettings = threadModeSettings;
75      }
76  
77      @Override
78      public String[] getAttributeNames() {
79          final Set<String> keySet = attributeMap.keySet();
80          return keySet.toArray(CommonUtil.EMPTY_STRING_ARRAY);
81      }
82  
83      @Override
84      public String getAttribute(String attributeName) throws CheckstyleException {
85          if (!attributeMap.containsKey(attributeName)) {
86              throw new CheckstyleException(
87                      "missing key '" + attributeName + "' in " + name);
88          }
89          return attributeMap.get(attributeName);
90      }
91  
92      @Override
93      public Configuration[] getChildren() {
94          return children.toArray(
95                  EMPTY_CONFIGURATION_ARRAY);
96      }
97  
98      @Override
99      public String getName() {
100         return name;
101     }
102 
103     /**
104      * Makes a configuration a child of this configuration.
105      * @param configuration the child configuration.
106      */
107     public void addChild(Configuration configuration) {
108         children.add(configuration);
109     }
110 
111     /**
112      * Removes a child of this configuration.
113      * @param configuration the child configuration to remove.
114      */
115     public void removeChild(final Configuration configuration) {
116         children.remove(configuration);
117     }
118 
119     /**
120      * Adds an attribute to this configuration.
121      * @param attributeName the name of the attribute.
122      * @param value the value of the attribute.
123      */
124     public void addAttribute(String attributeName, String value) {
125         final String current = attributeMap.get(attributeName);
126         if (current == null) {
127             attributeMap.put(attributeName, value);
128         }
129         else {
130             attributeMap.put(attributeName, current + "," + value);
131         }
132     }
133 
134     /**
135      * Adds a custom message to this configuration.
136      * @param key the message key
137      * @param value the custom message pattern
138      */
139     public void addMessage(String key, String value) {
140         messages.put(key, value);
141     }
142 
143     /**
144      * Returns an unmodifiable map instance containing the custom messages
145      * for this configuration.
146      * @return unmodifiable map containing custom messages
147      */
148     @Override
149     public Map<String, String> getMessages() {
150         return new HashMap<>(messages);
151     }
152 
153     /**
154      * Gets the thread mode configuration.
155      * @return the thread mode configuration.
156      */
157     public ThreadModeSettings getThreadModeSettings() {
158         return threadModeSettings;
159     }
160 
161 }