001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2015 the original author or authors.
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////////////////////////////////////////////////////////////////////////////////
019
020package com.puppycrawl.tools.checkstyle;
021
022import java.util.List;
023import java.util.Map;
024import java.util.Set;
025
026import com.google.common.collect.ImmutableMap;
027import com.google.common.collect.Lists;
028import com.google.common.collect.Maps;
029import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
030import com.puppycrawl.tools.checkstyle.api.Configuration;
031
032/**
033 * Default implementation of the Configuration interface.
034 * @author lkuehne
035 */
036public final class DefaultConfiguration implements Configuration {
037    /** Required for serialization. */
038    private static final long serialVersionUID = 1157875385356127169L;
039
040    /** The name of this configuration. */
041    private final String name;
042
043    /** The list of child Configurations. */
044    private final List<Configuration> children = Lists.newArrayList();
045
046    /** The map from attribute names to attribute values. */
047    private final Map<String, String> attributeMap = Maps.newHashMap();
048
049    /** The map containing custom messages. */
050    private final Map<String, String> messages = Maps.newHashMap();
051
052    /**
053     * Instantiates a DefaultConfiguration.
054     * @param name the name for this DefaultConfiguration.
055     */
056    public DefaultConfiguration(String name) {
057        this.name = name;
058    }
059
060    @Override
061    public String[] getAttributeNames() {
062        final Set<String> keySet = attributeMap.keySet();
063        return keySet.toArray(new String[keySet.size()]);
064    }
065
066    @Override
067    public String getAttribute(String attributeName) throws CheckstyleException {
068        if (!attributeMap.containsKey(attributeName)) {
069            throw new CheckstyleException(
070                    "missing key '" + attributeName + "' in " + name);
071        }
072        return attributeMap.get(attributeName);
073    }
074
075    @Override
076    public Configuration[] getChildren() {
077        return children.toArray(
078            new Configuration[children.size()]);
079    }
080
081    @Override
082    public String getName() {
083        return name;
084    }
085
086    /**
087     * Makes a configuration a child of this configuration.
088     * @param configuration the child configuration.
089     */
090    public void addChild(Configuration configuration) {
091        children.add(configuration);
092    }
093
094    /**
095     * Removes a child of this configuration.
096     * @param configuration the child configuration to remove.
097     */
098    public void removeChild(final Configuration configuration) {
099        children.remove(configuration);
100    }
101
102    /**
103     * Adds an attribute to this configuration.
104     * @param attributeName the name of the attribute.
105     * @param value the value of the attribute.
106     */
107    public void addAttribute(String attributeName, String value) {
108        final String current = attributeMap.put(attributeName, value);
109        if (current == null) {
110            attributeMap.put(attributeName, value);
111        }
112        else {
113            attributeMap.put(attributeName, current + "," + value);
114        }
115    }
116
117    /**
118     * Adds a custom message to this configuration.
119     * @param key the message key
120     * @param value the custom message pattern
121     */
122    public void addMessage(String key, String value) {
123        messages.put(key, value);
124    }
125
126    /**
127     * Returns an unmodifiable map instance containing the custom messages
128     * for this configuration.
129     * @return unmodifiable map containing custom messages
130     */
131    @Override
132    public ImmutableMap<String, String> getMessages() {
133        return ImmutableMap.copyOf(messages);
134    }
135}