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