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 mName;
043
044    /** the list of child Configurations */
045    private final List<Configuration> mChildren = Lists.newArrayList();
046
047    /** the map from attribute names to attribute values */
048    private final Map<String, String> mAttributeMap = Maps.newHashMap();
049
050    /** the map containing custom messages. */
051    private final Map<String, String> mMessages = Maps.newHashMap();
052
053    /**
054     * Instantiates a DefaultConfiguration.
055     * @param aName the name for this DefaultConfiguration.
056     */
057    public DefaultConfiguration(String aName)
058    {
059        mName = aName;
060    }
061
062    /** {@inheritDoc} */
063    public String[] getAttributeNames()
064    {
065        final Set<String> keySet = mAttributeMap.keySet();
066        return keySet.toArray(new String[keySet.size()]);
067    }
068
069    /** {@inheritDoc} */
070    public String getAttribute(String aName) throws CheckstyleException
071    {
072        if (!mAttributeMap.containsKey(aName)) {
073            // TODO: i18n
074            throw new CheckstyleException(
075                    "missing key '" + aName + "' in " + getName());
076        }
077        return mAttributeMap.get(aName);
078    }
079
080    /** {@inheritDoc} */
081    public Configuration[] getChildren()
082    {
083        return mChildren.toArray(
084            new Configuration[mChildren.size()]);
085    }
086
087    /** {@inheritDoc} */
088    public String getName()
089    {
090        return mName;
091    }
092
093    /**
094     * Makes a configuration a child of this configuration.
095     * @param aConfiguration the child configuration.
096     */
097    public void addChild(Configuration aConfiguration)
098    {
099        mChildren.add(aConfiguration);
100    }
101
102    /**
103     * Removes a child of this configuration.
104     * @param aConfiguration the child configuration to remove.
105     */
106    public void removeChild(final Configuration aConfiguration)
107    {
108        mChildren.remove(aConfiguration);
109    }
110
111    /**
112     * Adds an attribute to this configuration.
113     * @param aName the name of the attribute.
114     * @param aValue the value of the attribute.
115     */
116    public void addAttribute(String aName, String aValue)
117    {
118        final String current = mAttributeMap.put(aName, aValue);
119        if (null == current) {
120            mAttributeMap.put(aName, aValue);
121        }
122        else {
123            mAttributeMap.put(aName, current + "," + aValue);
124        }
125    }
126
127    /**
128     * Adds a custom message to this configuration.
129     * @param aKey the message key
130     * @param aValue the custom message pattern
131     */
132    public void addMessage(String aKey, String aValue)
133    {
134        mMessages.put(aKey, aValue);
135    }
136
137    /**
138     * Returns an unmodifiable map instance containing the custom messages
139     * for this configuration.
140     * @return unmodifiable map containing custom messages
141     */
142    public ImmutableMap<String, String> getMessages()
143    {
144        return ImmutableMap.copyOf(mMessages);
145    }
146}