001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2021 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.puppycrawl.tools.checkstyle.api.CheckstyleException;
029import com.puppycrawl.tools.checkstyle.api.Configuration;
030import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
031
032/**
033 * Default implementation of the Configuration interface.
034 *
035 * @noinspection SerializableHasSerializationMethods
036 */
037public final class DefaultConfiguration implements Configuration {
038
039    /** A unique serial version identifier. */
040    private static final long serialVersionUID = 1157875385356127169L;
041
042    /** Constant for optimization. */
043    private static final Configuration[] EMPTY_CONFIGURATION_ARRAY = new Configuration[0];
044
045    /** The name of this configuration. */
046    private final String name;
047
048    /** The list of child Configurations. */
049    private final List<Configuration> children = new ArrayList<>();
050
051    /** The map from property names to property values. */
052    private final Map<String, String> propertyMap = new HashMap<>();
053
054    /** The map containing custom messages. */
055    private final Map<String, String> messages = new HashMap<>();
056
057    /** The thread mode configuration. */
058    private final ThreadModeSettings threadModeSettings;
059
060    /**
061     * Instantiates a DefaultConfiguration.
062     *
063     * @param name the name for this DefaultConfiguration.
064     */
065    public DefaultConfiguration(String name) {
066        this(name, ThreadModeSettings.SINGLE_THREAD_MODE_INSTANCE);
067    }
068
069    /**
070     * Instantiates a DefaultConfiguration.
071     *
072     * @param name the name for this DefaultConfiguration.
073     * @param threadModeSettings the thread mode configuration.
074     */
075    public DefaultConfiguration(String name,
076        ThreadModeSettings threadModeSettings) {
077        this.name = name;
078        this.threadModeSettings = threadModeSettings;
079    }
080
081    @Override
082    public String[] getAttributeNames() {
083        return getPropertyNames();
084    }
085
086    @Override
087    public String getAttribute(String attributeName) throws CheckstyleException {
088        return getProperty(attributeName);
089    }
090
091    @Override
092    public String[] getPropertyNames() {
093        final Set<String> keySet = propertyMap.keySet();
094        return keySet.toArray(CommonUtil.EMPTY_STRING_ARRAY);
095    }
096
097    @Override
098    public String getProperty(String propertyName) throws CheckstyleException {
099        if (!propertyMap.containsKey(propertyName)) {
100            throw new CheckstyleException(
101                    "missing key '" + propertyName + "' in " + name);
102        }
103        return propertyMap.get(propertyName);
104    }
105
106    @Override
107    public Configuration[] getChildren() {
108        return children.toArray(
109                EMPTY_CONFIGURATION_ARRAY);
110    }
111
112    @Override
113    public String getName() {
114        return name;
115    }
116
117    /**
118     * Makes a configuration a child of this configuration.
119     *
120     * @param configuration the child configuration.
121     */
122    public void addChild(Configuration configuration) {
123        children.add(configuration);
124    }
125
126    /**
127     * Removes a child of this configuration.
128     *
129     * @param configuration the child configuration to remove.
130     */
131    public void removeChild(final Configuration configuration) {
132        children.remove(configuration);
133    }
134
135    /**
136     * Adds n property to this configuration.
137     *
138     * @param attributeName the name of the property.
139     * @param value the value of the property.
140     * @deprecated This shall be removed in future releases. Please use
141     *      {@code addProperty(String propertyName, String value)} instead.
142     */
143    @Deprecated
144    public void addAttribute(String attributeName, String value) {
145        addProperty(attributeName, value);
146    }
147
148    /**
149     * Adds n property to this configuration.
150     *
151     * @param propertyName the name of the property.
152     * @param value the value of the property.
153     */
154    public void addProperty(String propertyName, String value) {
155        final String current = propertyMap.get(propertyName);
156        if (current == null) {
157            propertyMap.put(propertyName, value);
158        }
159        else {
160            propertyMap.put(propertyName, current + "," + value);
161        }
162    }
163
164    /**
165     * Adds a custom message to this configuration.
166     *
167     * @param key the message key
168     * @param value the custom message pattern
169     */
170    public void addMessage(String key, String value) {
171        messages.put(key, value);
172    }
173
174    /**
175     * Returns an unmodifiable map instance containing the custom messages
176     * for this configuration.
177     *
178     * @return unmodifiable map containing custom messages
179     */
180    @Override
181    public Map<String, String> getMessages() {
182        return new HashMap<>(messages);
183    }
184
185    /**
186     * Gets the thread mode configuration.
187     *
188     * @return the thread mode configuration.
189     */
190    public ThreadModeSettings getThreadModeSettings() {
191        return threadModeSettings;
192    }
193
194}