001/////////////////////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code and other text files for adherence to a set of rules. 003// Copyright (C) 2001-2026 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.io.Serial; 023import java.util.ArrayList; 024import java.util.HashMap; 025import java.util.List; 026import java.util.Map; 027import java.util.Set; 028 029import com.puppycrawl.tools.checkstyle.api.CheckstyleException; 030import com.puppycrawl.tools.checkstyle.api.Configuration; 031import com.puppycrawl.tools.checkstyle.utils.CommonUtil; 032 033/** 034 * Default implementation of the Configuration interface. 035 */ 036public final class DefaultConfiguration implements Configuration { 037 038 /** A unique serial version identifier. */ 039 @Serial 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(since = "8.45") 144 @SuppressWarnings("InlineMeSuggester") 145 public void addAttribute(String attributeName, String value) { 146 addProperty(attributeName, value); 147 } 148 149 /** 150 * Adds n property to this configuration. 151 * 152 * @param propertyName the name of the property. 153 * @param value the value of the property. 154 */ 155 public void addProperty(String propertyName, String value) { 156 final String current = propertyMap.get(propertyName); 157 final String newValue; 158 if (current == null) { 159 newValue = value; 160 } 161 else { 162 newValue = current + "," + value; 163 } 164 propertyMap.put(propertyName, newValue); 165 } 166 167 /** 168 * Adds a custom message to this configuration. 169 * 170 * @param key the message key 171 * @param value the custom message pattern 172 */ 173 public void addMessage(String key, String value) { 174 messages.put(key, value); 175 } 176 177 /** 178 * Returns an unmodifiable map instance containing the custom messages 179 * for this configuration. 180 * 181 * @return unmodifiable map containing custom messages 182 */ 183 @Override 184 public Map<String, String> getMessages() { 185 return new HashMap<>(messages); 186 } 187 188 /** 189 * Gets the thread mode configuration. 190 * 191 * @return the thread mode configuration. 192 */ 193 public ThreadModeSettings getThreadModeSettings() { 194 return threadModeSettings; 195 } 196 197}