View Javadoc
1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2017 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.google.common.collect.ImmutableMap;
29  import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
30  import com.puppycrawl.tools.checkstyle.api.Configuration;
31  
32  /**
33   * Default implementation of the Configuration interface.
34   * @author lkuehne
35   * @noinspection SerializableHasSerializationMethods
36   */
37  public final class DefaultConfiguration implements Configuration {
38      private static final long serialVersionUID = 1157875385356127169L;
39  
40      /** The name of this configuration. */
41      private final String name;
42  
43      /** The list of child Configurations. */
44      private final List<Configuration> children = new ArrayList<>();
45  
46      /** The map from attribute names to attribute values. */
47      private final Map<String, String> attributeMap = new HashMap<>();
48  
49      /** The map containing custom messages. */
50      private final Map<String, String> messages = new HashMap<>();
51  
52      /** The thread mode configuration. */
53      private final ThreadModeSettings threadModeSettings;
54  
55      /**
56       * Instantiates a DefaultConfiguration.
57       * @param name the name for this DefaultConfiguration.
58       */
59      public DefaultConfiguration(String name) {
60          this(name, ThreadModeSettings.SINGLE_THREAD_MODE_INSTANCE);
61      }
62  
63      /**
64       * Instantiates a DefaultConfiguration.
65       * @param name the name for this DefaultConfiguration.
66       * @param threadModeSettings the thread mode configuration.
67       */
68      public DefaultConfiguration(String name,
69          ThreadModeSettings threadModeSettings) {
70          this.name = name;
71          this.threadModeSettings = threadModeSettings;
72      }
73  
74      @Override
75      public String[] getAttributeNames() {
76          final Set<String> keySet = attributeMap.keySet();
77          return keySet.toArray(new String[keySet.size()]);
78      }
79  
80      @Override
81      public String getAttribute(String attributeName) throws CheckstyleException {
82          if (!attributeMap.containsKey(attributeName)) {
83              throw new CheckstyleException(
84                      "missing key '" + attributeName + "' in " + name);
85          }
86          return attributeMap.get(attributeName);
87      }
88  
89      @Override
90      public Configuration[] getChildren() {
91          return children.toArray(
92              new Configuration[children.size()]);
93      }
94  
95      @Override
96      public String getName() {
97          return name;
98      }
99  
100     /**
101      * Makes a configuration a child of this configuration.
102      * @param configuration the child configuration.
103      */
104     public void addChild(Configuration configuration) {
105         children.add(configuration);
106     }
107 
108     /**
109      * Removes a child of this configuration.
110      * @param configuration the child configuration to remove.
111      */
112     public void removeChild(final Configuration configuration) {
113         children.remove(configuration);
114     }
115 
116     /**
117      * Adds an attribute to this configuration.
118      * @param attributeName the name of the attribute.
119      * @param value the value of the attribute.
120      */
121     public void addAttribute(String attributeName, String value) {
122         final String current = attributeMap.get(attributeName);
123         if (current == null) {
124             attributeMap.put(attributeName, value);
125         }
126         else {
127             attributeMap.put(attributeName, current + "," + value);
128         }
129     }
130 
131     /**
132      * Adds a custom message to this configuration.
133      * @param key the message key
134      * @param value the custom message pattern
135      */
136     public void addMessage(String key, String value) {
137         messages.put(key, value);
138     }
139 
140     /**
141      * Returns an unmodifiable map instance containing the custom messages
142      * for this configuration.
143      * @return unmodifiable map containing custom messages
144      */
145     @Override
146     public ImmutableMap<String, String> getMessages() {
147         return ImmutableMap.copyOf(messages);
148     }
149 
150     /**
151      * Gets the thread mode configuration.
152      * @return the thread mode configuration.
153      */
154     public ThreadModeSettings getThreadModeSettings() {
155         return threadModeSettings;
156     }
157 }