1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package com.puppycrawl.tools.checkstyle;
21
22 import java.util.ArrayList;
23 import java.util.HashMap;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.Set;
27
28 import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
29 import com.puppycrawl.tools.checkstyle.api.Configuration;
30 import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
31
32
33
34
35 public final class DefaultConfiguration implements Configuration {
36
37
38 private static final long serialVersionUID = 1157875385356127169L;
39
40
41 private static final Configuration[] EMPTY_CONFIGURATION_ARRAY = new Configuration[0];
42
43
44 private final String name;
45
46
47 private final List<Configuration> children = new ArrayList<>();
48
49
50 private final Map<String, String> propertyMap = new HashMap<>();
51
52
53 private final Map<String, String> messages = new HashMap<>();
54
55
56 private final ThreadModeSettings threadModeSettings;
57
58
59
60
61
62
63 public DefaultConfiguration(String name) {
64 this(name, ThreadModeSettings.SINGLE_THREAD_MODE_INSTANCE);
65 }
66
67
68
69
70
71
72
73 public DefaultConfiguration(String name,
74 ThreadModeSettings threadModeSettings) {
75 this.name = name;
76 this.threadModeSettings = threadModeSettings;
77 }
78
79 @Override
80 public String[] getAttributeNames() {
81 return getPropertyNames();
82 }
83
84 @Override
85 public String getAttribute(String attributeName) throws CheckstyleException {
86 return getProperty(attributeName);
87 }
88
89 @Override
90 public String[] getPropertyNames() {
91 final Set<String> keySet = propertyMap.keySet();
92 return keySet.toArray(CommonUtil.EMPTY_STRING_ARRAY);
93 }
94
95 @Override
96 public String getProperty(String propertyName) throws CheckstyleException {
97 if (!propertyMap.containsKey(propertyName)) {
98 throw new CheckstyleException(
99 "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
117
118
119
120 public void addChild(Configuration configuration) {
121 children.add(configuration);
122 }
123
124
125
126
127
128
129 public void removeChild(final Configuration configuration) {
130 children.remove(configuration);
131 }
132
133
134
135
136
137
138
139
140
141 @Deprecated(since = "8.45")
142 public void addAttribute(String attributeName, String value) {
143 addProperty(attributeName, value);
144 }
145
146
147
148
149
150
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
166
167
168
169
170 public void addMessage(String key, String value) {
171 messages.put(key, value);
172 }
173
174
175
176
177
178
179
180 @Override
181 public Map<String, String> getMessages() {
182 return new HashMap<>(messages);
183 }
184
185
186
187
188
189
190 public ThreadModeSettings getThreadModeSettings() {
191 return threadModeSettings;
192 }
193
194 }