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