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