forked from jooby-project/jooby
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashValue.java
More file actions
273 lines (238 loc) · 7.07 KB
/
HashValue.java
File metadata and controls
273 lines (238 loc) · 7.07 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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/**
* Jooby https://jooby.io
* Apache License Version 2.0 https://jooby.io/LICENSE.txt
* Copyright 2014 Edgar Espina
*/
package io.jooby.internal;
import io.jooby.Context;
import io.jooby.FileUpload;
import io.jooby.Formdata;
import io.jooby.ValueNode;
import java.util.function.Supplier;
import javax.annotation.Nonnull;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.StringJoiner;
import java.util.TreeMap;
import java.util.function.BiConsumer;
public class HashValue implements ValueNode, Formdata {
private static final Map<String, ValueNode> EMPTY = Collections.emptyMap();
private Context ctx;
private Map<String, ValueNode> hash = EMPTY;
private final String name;
public HashValue(Context ctx, String name, Supplier<Map<String, ValueNode>> mapSupplier) {
this.ctx = ctx;
this.name = name;
this.hash = mapSupplier.get();
}
public HashValue(Context ctx, String name) {
this.ctx = ctx;
this.name = name;
}
protected HashValue(Context ctx) {
this.ctx = ctx;
this.name = null;
}
@Override public String name() {
return name;
}
public void put(String path, String value) {
put(path, Collections.singletonList(value));
}
public void put(String path, ValueNode node) {
put(path, (name, scope) -> {
ValueNode existing = scope.get(name);
if (existing == null) {
scope.put(name, node);
} else {
ArrayValue list;
if (existing instanceof ArrayValue) {
list = (ArrayValue) existing;
} else {
list = new ArrayValue(ctx, name).add(existing);
scope.put(name, list);
}
list.add(node);
}
});
}
public void put(String path, Collection<String> values) {
put(path, (name, scope) -> {
for (String value : values) {
ValueNode existing = scope.get(name);
if (existing == null) {
scope.put(name, new SingleValue(ctx, name, decode(value)));
} else {
ArrayValue list;
if (existing instanceof ArrayValue) {
list = (ArrayValue) existing;
} else {
list = new ArrayValue(ctx, name).add(existing);
scope.put(name, list);
}
list.add(decode(value));
}
}
});
}
protected String decode(String value) {
return value;
}
private void put(String path, BiConsumer<String, Map<String, ValueNode>> consumer) {
// Locate node:
int nameStart = 0;
int nameEnd = path.length();
HashValue target = this;
for (int i = nameStart; i < nameEnd; i++) {
char ch = path.charAt(i);
if (ch == '.') {
String name = path.substring(nameStart, i);
nameStart = i + 1;
target = target.getOrCreateScope(name);
} else if (ch == '[') {
if (nameStart < i) {
String name = path.substring(nameStart, i);
target = target.getOrCreateScope(name);
}
nameStart = i + 1;
} else if (ch == ']') {
if (i + 1 < nameEnd) {
String name = path.substring(nameStart, i);
if (isNumber(name)) {
target.useIndexes();
}
nameStart = i + 1;
target = target.getOrCreateScope(name);
} else {
nameEnd = i;
}
}
}
String key = path.substring(nameStart, nameEnd);
if (isNumber(key)) {
target.useIndexes();
}
// Final node
consumer.accept(key, target.hash());
}
private void useIndexes() {
if (hash instanceof TreeMap) {
return;
}
TreeMap<String, ValueNode> ordered = new TreeMap<>();
ordered.putAll(hash);
hash.clear();
this.hash = ordered;
}
private boolean isNumber(String value) {
for (int i = 0; i < value.length(); i++) {
if (!Character.isDigit(value.charAt(i))) {
return false;
}
}
return true;
}
private Map<String, ValueNode> hash() {
if (hash == EMPTY) {
hash = new LinkedHashMap<>();
}
return hash;
}
/*package*/ HashValue getOrCreateScope(String name) {
return (HashValue) hash().computeIfAbsent(name, k -> new HashValue(ctx, k));
}
public ValueNode get(@Nonnull String name) {
ValueNode value = hash.get(name);
if (value == null) {
return new MissingValue(scope(name));
}
return value;
}
private String scope(String name) {
return this.name == null ? name : this.name + "." + name;
}
@Override public ValueNode get(@Nonnull int index) {
return get(Integer.toString(index));
}
public int size() {
return hash.size();
}
@Override public String value() {
StringJoiner joiner = new StringJoiner("&");
hash.forEach((k, v) -> {
Iterator<ValueNode> it = v.iterator();
while (it.hasNext()) {
ValueNode value = it.next();
String str = value instanceof FileUpload
? ((FileUpload) value).getFileName()
: value.toString();
joiner.add(k + "=" + str);
}
});
return joiner.toString();
}
@Override public Iterator<ValueNode> iterator() {
return hash.values().iterator();
}
@Nonnull @Override public List<String> toList() {
return toList(String.class);
}
@Nonnull @Override public Set<String> toSet() {
return toSet(String.class);
}
@Nonnull @Override public <T> List<T> toList(@Nonnull Class<T> type) {
return toCollection(type, new ArrayList<>());
}
@Nonnull @Override public <T> Set<T> toSet(@Nonnull Class<T> type) {
return toCollection(type, new LinkedHashSet<>());
}
@Nonnull @Override public <T> Optional<T> toOptional(@Nonnull Class<T> type) {
if (hash.isEmpty()) {
return Optional.empty();
}
return Optional.ofNullable(to(type));
}
@Nonnull @Override public <T> T to(@Nonnull Class<T> type) {
return ctx.convert(this, type);
}
@Override public Map<String, List<String>> toMultimap() {
Map<String, List<String>> result = new LinkedHashMap<>(hash.size());
Set<Map.Entry<String, ValueNode>> entries = hash.entrySet();
String scope = name == null ? "" : name + ".";
for (Map.Entry<String, ValueNode> entry : entries) {
ValueNode value = entry.getValue();
value.toMultimap().forEach((k, v) -> {
result.put(scope + k, v);
});
}
return result;
}
@Override public String toString() {
return hash.toString();
}
public void put(Map<String, Collection<String>> headers) {
for (Map.Entry<String, Collection<String>> entry : headers.entrySet()) {
put(entry.getKey(), entry.getValue());
}
}
private <T, C extends Collection<T>> C toCollection(@Nonnull Class<T> type, C collection) {
if (hash instanceof TreeMap) {
// indexes access, treat like a list
Collection<ValueNode> values = hash.values();
for (ValueNode value : values) {
collection.add(value.to(type));
}
} else {
collection.add(to(type));
}
return collection;
}
}