Repository navigation
Expand file tree
/
Copy pathMutant.java
More file actions
389 lines (348 loc) · 9.86 KB
/
Copy pathMutant.java
File metadata and controls
389 lines (348 loc) · 9.86 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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.jooby;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.SortedSet;
import com.google.common.primitives.Primitives;
import com.google.inject.TypeLiteral;
import com.google.inject.util.Types;
/**
* <p>
* A type safe {@link Mutant} useful for reading parameters/headers/session attributes, etc..
* </p>
*
* <pre>
* // str param
* String value = request.param("str").value();
*
* // optional str
* String value = request.param("str").value("defs");
*
* // int param
* int value = request.param("some").intValue();
*
* // optional int param
* Optional{@literal <}Integer{@literal >} value = request.param("some").toOptional(Integer.class);
* // list param
* List{@literal <}String{@literal >} values = request.param("some").toList(String.class);
*
* // file upload
* Upload upload = request.param("file").to(Upload.class);
* </pre>
*
* @author edgar
* @since 0.1.0
* @see Request#param(String)
* @see Request#header(String)
*/
public interface Mutant {
/**
* @return Get a boolean when possible.
*/
default boolean booleanValue() {
return to(boolean.class);
}
/**
* @param value Default value to use.
* @return Get a boolean.
*/
default boolean booleanValue(final boolean value) {
return toOptional(Boolean.class).orElse(value);
}
/**
* @return Get a byte when possible.
*/
default byte byteValue() {
return to(byte.class);
}
/**
* @param value Default value to use.
* @return Get a byte.
*/
default byte byteValue(final byte value) {
return toOptional(Byte.class).orElse(value);
}
/**
* @return Get a byte when possible.
*/
default char charValue() {
return to(char.class);
}
/**
* @param value Default value to use.
* @return Get a char.
*/
default char charValue(final char value) {
return toOptional(Character.class).orElse(value);
}
/**
* @return Get a short when possible.
*/
default short shortValue() {
return to(short.class);
}
/**
* @param value Default value to use.
* @return Get a short value.
*/
default short shortValue(final short value) {
return toOptional(Short.class).orElse(value);
}
/**
* @return Get an integer when possible.
*/
default int intValue() {
return to(int.class);
}
/**
* @param value Default value to use.
* @return Get an integer.
*/
default int intValue(final int value) {
return toOptional(Integer.class).orElse(value);
}
/**
* @return Get a long when possible.
*/
default long longValue() {
return to(long.class);
}
/**
* @param value Default value to use.
* @return Get a long.
*/
default long longValue(final long value) {
return toOptional(Long.class).orElse(value);
}
/**
* @return Get a string when possible.
*/
default String value() {
return to(String.class);
}
/**
* @param value Default value to use.
* @return Get a string.
*/
default String value(final String value) {
return toOptional().orElse(value);
}
/**
* @return Get a float when possible.
*/
default float floatValue() {
return to(float.class);
}
/**
* @param value Default value to use.
* @return Get a float.
*/
default float floatValue(final float value) {
return toOptional(Float.class).orElse(value);
}
/**
* @return Get a double when possible.
*/
default double doubleValue() {
return to(double.class);
}
/**
* @param value Default value to use.
* @return Get a double.
*/
default double doubleValue(final double value) {
return toOptional(Double.class).orElse(value);
}
/**
* @param type The enum type.
* @param <T> Enum type.
* @return Get an enum when possible.
*/
default <T extends Enum<T>> T toEnum(final Class<T> type) {
return to(type);
}
/**
* @param value Default value to use.
* @param <T> Enum type.
* @return Get an enum.
*/
@SuppressWarnings("unchecked")
default <T extends Enum<T>> T toEnum(final T value) {
Optional<T> optional = (Optional<T>) toOptional(value.getClass());
return optional.orElse(value);
}
/**
* @param type The element type.
* @param <T> List type.
* @return Get list of values when possible.
*/
@SuppressWarnings("unchecked")
default <T> List<T> toList(final Class<T> type) {
return (List<T>) to(TypeLiteral.get(Types.listOf(Primitives.wrap(type))));
}
/**
* @return Get list of values when possible.
*/
default List<String> toList() {
return toList(String.class);
}
/**
* @return Get set of values when possible.
*/
default Set<String> toSet() {
return toSet(String.class);
}
/**
* @param type The element type.
* @param <T> Set type.
* @return Get set of values when possible.
*/
@SuppressWarnings("unchecked")
default <T> Set<T> toSet(final Class<T> type) {
return (Set<T>) to(TypeLiteral.get(Types.setOf(Primitives.wrap(type))));
}
/**
* @return Get sorted set of values when possible.
*/
default SortedSet<String> toSortedSet() {
return toSortedSet(String.class);
}
/**
* @param type The element type.
* @param <T> Set type.
* @return Get sorted set of values when possible.
*/
@SuppressWarnings("unchecked")
default <T extends Comparable<T>> SortedSet<T> toSortedSet(final Class<T> type) {
return (SortedSet<T>) to(TypeLiteral.get(
Types.newParameterizedType(SortedSet.class, Primitives.wrap(type))
));
}
/**
* @return An optional string value.
*/
default Optional<String> toOptional() {
return toOptional(String.class);
}
/**
* @param type The optional type.
* @param <T> Optional type.
* @return Get an optional value when possible.
*/
@SuppressWarnings("unchecked")
default <T> Optional<T> toOptional(final Class<T> type) {
return (Optional<T>) to(TypeLiteral.get(
Types.newParameterizedType(Optional.class, Primitives.wrap(type))
));
}
/**
* Convert a form field to file {@link Upload}.
*
* @return A file {@link Upload}.
*/
default Upload toUpload() {
return to(Upload.class);
}
/**
* Convert a raw value to the given type.
*
* @param type The type to convert to.
* @param <T> Target type.
* @return Get a value when possible.
*/
default <T> T to(final Class<T> type) {
return to(TypeLiteral.get(type));
}
/**
* Convert a raw value to the given type.
*
* @param type The type to convert to.
* @param <T> Target type.
* @return Get a value when possible.
*/
<T> T to(TypeLiteral<T> type);
/**
* Convert a raw value to the given type. This method will temporary set {@link MediaType} before
* parsing a value, useful if a form field from a HTTP POST was send as json (or any other data).
*
* @param type The type to convert to.
* @param mtype A media type to hint a parser.
* @param <T> Target type.
* @return Get a value when possible.
*/
default <T> T to(final Class<T> type, final String mtype) {
return to(type, MediaType.valueOf(mtype));
}
/**
* Convert a raw value to the given type. This method will temporary set {@link MediaType} before
* parsing a value, useful if a form field from a HTTP POST was send as json (or any other data).
*
* @param type The type to convert to.
* @param mtype A media type to hint a parser.
* @param <T> Target type.
* @return Get a value when possible.
*/
default <T> T to(final Class<T> type, final MediaType mtype) {
return to(TypeLiteral.get(type), mtype);
}
/**
* Convert a raw value to the given type. This method will temporary set {@link MediaType} before
* parsing a value, useful if a form field from a HTTP POST was send as json (or any other data).
*
* @param type The type to convert to.
* @param mtype A media type to hint a parser.
* @param <T> Target type.
* @return Get a value when possible.
*/
default <T> T to(final TypeLiteral<T> type, final String mtype) {
return to(type, MediaType.valueOf(mtype));
}
/**
* Convert a raw value to the given type. This method will temporary set {@link MediaType} before
* parsing a value, useful if a form field from a HTTP POST was send as json (or any other data).
*
* @param type The type to convert to.
* @param mtype A media type to hint a parser.
* @param <T> Target type.
* @return Get a value when possible.
*/
<T> T to(TypeLiteral<T> type, MediaType mtype);
/**
* A map view of this mutant.
*
* If this mutant is the result of {@link Request#params()} the resulting map will have all the
* available parameter names.
*
* If the mutant is the result of {@link Request#param(String)} the resulting map will have just
* one entry, with the name as key.
*
* If the mutant is the result of {@link Request#body()} the resulting map will have just
* one entry, with a key of <code>body</code>.
*
* @return A map view of this mutant.
*/
Map<String, Mutant> toMap();
/**
* @return True if this mutant has a value (param, header, body, etc...).
*/
boolean isSet();
}