001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2014  Oliver Burn
004//
005// This library is free software; you can redistribute it and/or
006// modify it under the terms of the GNU Lesser General Public
007// License as published by the Free Software Foundation; either
008// version 2.1 of the License, or (at your option) any later version.
009//
010// This library is distributed in the hope that it will be useful,
011// but WITHOUT ANY WARRANTY; without even the implied warranty of
012// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013// Lesser General Public License for more details.
014//
015// You should have received a copy of the GNU Lesser General Public
016// License along with this library; if not, write to the Free Software
017// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
018////////////////////////////////////////////////////////////////////////////////
019package com.puppycrawl.tools.checkstyle;
020
021import com.google.common.collect.ImmutableMap;
022
023import com.google.common.collect.Lists;
024import com.google.common.collect.Maps;
025import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
026import com.puppycrawl.tools.checkstyle.api.Configuration;
027
028import java.util.List;
029import java.util.Map;
030import java.util.Set;
031
032/**
033 * Default implementation of the Configuration interface.
034 * @author lkuehne
035 */
036public final class DefaultConfiguration implements Configuration
037{
038    /** Required for serialization. */
039    private static final long serialVersionUID = 1157875385356127169L;
040
041    /** The name of this configuration */
042    private final String name;
043
044    /** the list of child Configurations */
045    private final List<Configuration> children = Lists.newArrayList();
046
047    /** the map from attribute names to attribute values */
048    private final Map<String, String> attributeMap = Maps.newHashMap();
049
050    /** the map containing custom messages. */
051    private final Map<String, String> messages = Maps.newHashMap();
052
053    /**
054     * Instantiates a DefaultConfiguration.
055     * @param name the name for this DefaultConfiguration.
056     */
057    public DefaultConfiguration(String name)
058    {
059        this.name = name;
060    }
061
062    /** {@inheritDoc} */
063    @Override
064    public String[] getAttributeNames()
065    {
066        final Set<String> keySet = attributeMap.keySet();
067        return keySet.toArray(new String[keySet.size()]);
068    }
069
070    /** {@inheritDoc} */
071    @Override
072    public String getAttribute(String name) throws CheckstyleException
073    {
074        if (!attributeMap.containsKey(name)) {
075            // TODO: i18n
076            throw new CheckstyleException(
077                    "missing key '" + name + "' in " + getName());
078        }
079        return attributeMap.get(name);
080    }
081
082    /** {@inheritDoc} */
083    @Override
084    public Configuration[] getChildren()
085    {
086        return children.toArray(
087            new Configuration[children.size()]);
088    }
089
090    /** {@inheritDoc} */
091    @Override
092    public String getName()
093    {
094        return name;
095    }
096
097    /**
098     * Makes a configuration a child of this configuration.
099     * @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}