001/////////////////////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code and other text files for adherence to a set of rules. 003// Copyright (C) 2001-2024 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 */ 035public final class DefaultConfiguration implements Configuration { 036 037 /** A unique serial version identifier. */ 038 private static final long serialVersionUID = 1157875385356127169L; 039 040 /** Constant for optimization. */ 041 private static final Configuration[] EMPTY_CONFIGURATION_ARRAY = new Configuration[0]; 042 043 /** The name of this configuration. */ 044 private final String name; 045 046 /** The list of child Configurations. */ 047 private final List<Configuration> children = new ArrayList<>(); 048 049 /** The map from property names to property values. */ 050 private final Map<String, String> propertyMap = new HashMap<>(); 051 052 /** The map containing custom messages. */ 053 private final Map<String, String> messages = new HashMap<>(); 054 055 /** The thread mode configuration. */ 056 private final ThreadModeSettings threadModeSettings; 057 058 /** 059 * Instantiates a DefaultConfiguration. 060 * 061 * @param name the name for this DefaultConfiguration. 062 */ 063 public DefaultConfiguration(String name) { 064 this(name, ThreadModeSettings.SINGLE_THREAD_MODE_INSTANCE); 065 } 066 067 /** 068 * Instantiates a DefaultConfiguration. 069 * 070 * @param name the name for this DefaultConfiguration. 071 * @param threadModeSettings the thread mode configuration. 072 */ 073 public DefaultConfiguration(String name, 074 ThreadModeSettings threadModeSettings) { 075 this.name = name; 076 this.threadModeSettings = threadModeSettings; 077 } 078 079 @Override 080 public String[] getAttributeNames() { 081 return getPropertyNames(); 082 } 083 084 @Override 085 public String getAttribute(String attributeName) throws CheckstyleException { 086 return getProperty(attributeName); 087 } 088 089 @Override 090 public String[] getPropertyNames() { 091 final Set<String> keySet = propertyMap.keySet(); 092 return keySet.toArray(CommonUtil.EMPTY_STRING_ARRAY); 093 } 094 095 @Override 096 public String getProperty(String propertyName) throws CheckstyleException { 097 if (!propertyMap.containsKey(propertyName)) { 098 throw new CheckstyleException( 099 "missing key '" + propertyName + "' in " + name); 100 } 101 return propertyMap.get(propertyName); 102 } 103 104 @Override 105 public Configuration[] getChildren() { 106 return children.toArray( 107 EMPTY_CONFIGURATION_ARRAY); 108 } 109 110 @Override 111 public String getName() { 112 return name; 113 } 114 115 /** 116 * Makes a configuration a child of this configuration. 117 * 118 * @param configuration the child configuration. 119 */ 120 public void addChild(Configuration configuration) { 121 children.add(configuration); 122 } 123 124 /** 125 * Removes a child of this configuration. 126 * 127 * @param configuration the child configuration to remove. 128 */ 129 public void removeChild(final Configuration configuration) { 130 children.remove(configuration); 131 } 132 133 /** 134 * Adds n property to this configuration. 135 * 136 * @param attributeName the name of the property. 137 * @param value the value of the property. 138 * @deprecated This shall be removed in future releases. Please use 139 * {@code addProperty(String propertyName, String value)} instead. 140 */ 141 @Deprecated(since = "8.45") 142 public void addAttribute(String attributeName, String value) { 143 addProperty(attributeName, value); 144 } 145 146 /** 147 * Adds n property to this configuration. 148 * 149 * @param propertyName the name of the property. 150 * @param value the value of the property. 151 */ 152 public void addProperty(String propertyName, String value) { 153 final String current = propertyMap.get(propertyName); 154 final String newValue; 155 if (current == null) { 156 newValue = value; 157 } 158 else { 159 newValue = current + "," + value; 160 } 161 propertyMap.put(propertyName, newValue); 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}