-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfigValidator.java
More file actions
218 lines (181 loc) · 7.22 KB
/
Copy pathConfigValidator.java
File metadata and controls
218 lines (181 loc) · 7.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import java.util.*;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
/**
* Validates config to ensure there are no ambiguous config overrides
*/
final class ConfigValidator {
public static void main(String[] args) {
if (args.length != 1) {
System.out.println("Missing config values. Provide semi-colon separated config values.");
return;
}
Map<String, List<ParameterCollection>> parameterizedConfigs = new HashMap<>();
for (String config : args[0].split(Pattern.quote(";"))) {
String[] kv = config.trim().split(Pattern.quote(":"));
String[] kp = kv[0].trim().split(Pattern.quote("?"));
if (kp.length != 2) {
continue;
}
List<ParameterCollection> pcs = parameterizedConfigs.get(kp[0]);
if (pcs == null) {
pcs = new ArrayList<>();
parameterizedConfigs.put(kp[0], pcs);
}
pcs.add(ParameterCollection.parse(kp[1]));
}
System.out.println("Parsed config");
parameterizedConfigs.forEach((k, v) -> {
for (ParameterCollection pc : v) {
System.out.println(k + "?" + pc);
}
});
parameterizedConfigs.forEach((k, v) -> {
if (v.size() == 1) {
System.out.println("Single parameterized configs are not ambiguous");
return;
}
List<ParameterCollection> moreRequired = new ArrayList<>();
for (int i = 0; i < v.size(); ++i) {
for (int j = i + 1; j < v.size(); ++j) {
if (v.get(i).isAmbiguousTo(v.get(j))) {
ParameterCollection pc = ParameterCollection.getCombined(v.get(i), v.get(j));
if (v.contains(pc)) {
System.out.println("Disambiguating parameter collection already exists");
continue;
}
moreRequired.add(pc);
}
}
}
boolean noMoreRequired = true;
do {
for (ParameterCollection existing : v) {
Set<ParameterCollection> evenMoreRequired = new HashSet<>();
for (ParameterCollection added : moreRequired) {
if (existing.isAmbiguousTo(added)) {
ParameterCollection pc = ParameterCollection.getCombined(existing, added);
if (v.contains(pc)) {
System.out.println("Disambiguating parameter collection already exists");
continue;
}
noMoreRequired = false;
evenMoreRequired.add(pc);
}
}
if (evenMoreRequired.size() > 0) {
moreRequired.addAll(evenMoreRequired);
}
}
} while (noMoreRequired);
if (moreRequired.size() > 0) {
System.out.println("Configuration is ambiguous, please add following parameter combinations to fix it.");
for (ParameterCollection pc : moreRequired) {
System.out.println(k + "?" + pc);
}
}
});
}
private static class ParameterCollection {
private Set<Parameter> parameters;
private static ParameterCollection parse(String parameters) {
ParameterCollection pc = new ParameterCollection();
pc.parameters = new HashSet<>();
String[] pvs = parameters.split(Pattern.quote("&"));
for (String pv : pvs) {
String[] splitPV = pv.split(Pattern.quote("="));
Parameter p = new Parameter();
p.key = splitPV[0];
p.value = splitPV[1];
pc.parameters.add(p);
}
return pc;
}
private static ParameterCollection getCombined(ParameterCollection pc1, ParameterCollection pc2) {
ParameterCollection result = new ParameterCollection();
result.parameters = new HashSet<>(pc1.parameters);
result.parameters.addAll(pc2.parameters);
return result;
}
private boolean isAmbiguousTo(ParameterCollection other) {
boolean result = false;
for (Parameter myParam : this.parameters) {
boolean continueNext = false;
for (Parameter otherParam : other.parameters) {
if (myParam.key.equals(otherParam.key)) {
if (myParam.value.equals(otherParam.value)) {
continueNext = true;
break;
} else {
return false;
}
}
}
if (continueNext) {
continue;
}
result = true;
break;
}
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || !(obj instanceof ParameterCollection)) {
return false;
}
ParameterCollection other = (ParameterCollection) obj;
if (this.parameters == other.parameters) {
return true;
}
if (this.parameters == null || other.parameters == null) {
return false;
}
return this.parameters.containsAll(other.parameters)
&& other.parameters.containsAll(this.parameters);
}
@Override
public int hashCode() {
int result = 1;
if (this.parameters == null) {
return result;
}
return this.parameters.stream()
.map(Parameter::hashCode)
.reduce(result, (a, b) -> 31 * a + b);
}
@Override
public String toString() {
return String.join("&", this.parameters.stream().map(Parameter::toString).collect(Collectors.toList()));
}
private static class Parameter {
private String key;
private String value;
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || !(obj instanceof Parameter)) {
return false;
}
Parameter other = (Parameter) obj;
return this.key.equals(other.key) && this.value.equals(other.value);
}
@Override
public int hashCode() {
int result = 1;
result = 31 * result + this.key.hashCode();
result = 31 * result + this.value.hashCode();
return result;
}
@Override
public String toString() {
return String.format("%s=%s", key, value);
}
}
}
}