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