001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2018 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.ArrayList;
023import java.util.HashMap;
024import java.util.List;
025import java.util.Map;
026import java.util.Set;
027
028import com.google.common.collect.ImmutableMap;
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 * @noinspection SerializableHasSerializationMethods
036 */
037public final class DefaultConfiguration implements Configuration {
038
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 = new ArrayList<>();
046
047    /** The map from attribute names to attribute values. */
048    private final Map<String, String> attributeMap = new HashMap<>();
049
050    /** The map containing custom messages. */
051    private final Map<String, String> messages = new HashMap<>();
052
053    /** The thread mode configuration. */
054    private final ThreadModeSettings threadModeSettings;
055
056    /**
057     * Instantiates a DefaultConfiguration.
058     * @param name the name for this DefaultConfiguration.
059     */
060    public DefaultConfiguration(String name) {
061        this(name, ThreadModeSettings.SINGLE_THREAD_MODE_INSTANCE);
062    }
063
064    /**
065     * Instantiates a DefaultConfiguration.
066     * @param name the name for this DefaultConfiguration.
067     * @param threadModeSettings the thread mode configuration.
068     */
069    public DefaultConfiguration(String name,
070        ThreadModeSettings threadModeSettings) {
071        this.name = name;
072        this.threadModeSettings = threadModeSettings;
073    }
074
075    @Override
076    public String[] getAttributeNames() {
077        final Set<String> keySet = attributeMap.keySet();
078        return keySet.toArray(new String[keySet.size()]);
079    }
080
081    @Override
082    public String getAttribute(String attributeName) throws CheckstyleException {
083        if (!attributeMap.containsKey(attributeName)) {
084            throw new CheckstyleException(
085                    "missing key '" + attributeName + "' in " + name);
086        }
087        return attributeMap.get(attributeName);
088    }
089
090    @Override
091    public Configuration[] getChildren() {
092        return children.toArray(
093            new Configuration[children.size()]);
094    }
095
096    @Override
097    public String getName() {
098        return name;
099    }
100
101    /**
102     * Makes a configuration a child of this configuration.
103     * @param configuration the child configuration.
104     */
105    public void addChild(Configuration configuration) {
106        children.add(configuration);
107    }
108
109    /**
110     * Removes a child of this configuration.
111     * @param configuration the child configuration to remove.
112     */
113    public void removeChild(final Configuration configuration) {
114        children.remove(configuration);
115    }
116
117    /**
118     * Adds an attribute to this configuration.
119     * @param attributeName the name of the attribute.
120     * @param value the value of the attribute.
121     */
122    public void addAttribute(String attributeName, String value) {
123        final String current = attributeMap.get(attributeName);
124        if (current == null) {
125            attributeMap.put(attributeName, value);
126        }
127        else {
128            attributeMap.put(attributeName, current + "," + value);
129        }
130    }
131
132    /**
133     * Adds a custom message to this configuration.
134     * @param key the message key
135     * @param value the custom message pattern
136     */
137    public void addMessage(String key, String value) {
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        return ImmutableMap.copyOf(messages);
149    }
150
151    /**
152     * Gets the thread mode configuration.
153     * @return the thread mode configuration.
154     */
155    public ThreadModeSettings getThreadModeSettings() {
156        return threadModeSettings;
157    }
158
159}