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
36 public final class DefaultConfiguration implements Configuration {
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> attributeMap = 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 public DefaultConfiguration(String name) {
63 this(name, ThreadModeSettings.SINGLE_THREAD_MODE_INSTANCE);
64 }
65
66
67
68
69
70
71 public DefaultConfiguration(String name,
72 ThreadModeSettings threadModeSettings) {
73 this.name = name;
74 this.threadModeSettings = threadModeSettings;
75 }
76
77 @Override
78 public String[] getAttributeNames() {
79 final Set<String> keySet = attributeMap.keySet();
80 return keySet.toArray(CommonUtil.EMPTY_STRING_ARRAY);
81 }
82
83 @Override
84 public String getAttribute(String attributeName) throws CheckstyleException {
85 if (!attributeMap.containsKey(attributeName)) {
86 throw new CheckstyleException(
87 "missing key '" + attributeName + "' in " + name);
88 }
89 return attributeMap.get(attributeName);
90 }
91
92 @Override
93 public Configuration[] getChildren() {
94 return children.toArray(
95 EMPTY_CONFIGURATION_ARRAY);
96 }
97
98 @Override
99 public String getName() {
100 return name;
101 }
102
103
104
105
106
107 public void addChild(Configuration configuration) {
108 children.add(configuration);
109 }
110
111
112
113
114
115 public void removeChild(final Configuration configuration) {
116 children.remove(configuration);
117 }
118
119
120
121
122
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
136
137
138
139 public void addMessage(String key, String value) {
140 messages.put(key, value);
141 }
142
143
144
145
146
147
148 @Override
149 public Map<String, String> getMessages() {
150 return new HashMap<>(messages);
151 }
152
153
154
155
156
157 public ThreadModeSettings getThreadModeSettings() {
158 return threadModeSettings;
159 }
160
161 }