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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443 | package Torello.JSON;
import javax.json.*;
import java.lang.reflect.Constructor;
import java.lang.reflect.Modifier;
import java.math.*;
import static javax.json.JsonValue.ValueType.*;
import static Torello.JSON.JFlag.*;
import java.util.Objects;
import java.util.function.Function;
/**
* Class which provides a series of helper functions for all JSON Type-Binding Reader
* Classes.
*
* <BR /><BR /><DIV CLASS=JDHint>
* 100% of the helper-methods that appear here are protected, and cannot be accessed
* outside of this package. They are included in the documentation solely for the purposes of
* (<I>if you happen to be interested</I>) letting you know how the JSON-Tools work. It is not
* intended that programmers would ever need to invoke, directly, any of the methods in this
* class!
* </DIV>
*/
@Torello.JavaDoc.Annotations.StaticFunctional
public class RJInternal
{
private RJInternal() { }
// ********************************************************************************************
// ********************************************************************************************
// "Helpers for the Helpers for the Helpers"
// ********************************************************************************************
// ********************************************************************************************
private static void throwAE_INFINITY
(JsonNumber jn, String primTypeName, boolean positiveOrNegative)
{
throw new ArithmeticException(
"When attempting to conver the JsonNumber [" + jn.toString() + "] to a " +
primTypeName + " primitive, the number had a magnitude that was too large: " +
(positiveOrNegative ? "Positive" : "Negative") + " Infinity was returned."
);
}
private static void throwAE_INFINITY(BigDecimal bd, String primTypeName)
{
throw new ArithmeticException(
"When attempting to conver the JsonNumber [" + bd.toString() + "] to a " +
primTypeName + " primitive, the number had a magnitude that was infinite."
);
}
private static void throwAE_PRECISION(BigDecimal bd, String primTypeName)
{
throw new ArithmeticException(
"When attempting to conver the JsonNumber [" + bd.toString() + "] to a " +
primTypeName + " primitive, the number had a loss of precision."
);
}
/*
* Converts a {@link JsonNumber} into a Java {@code double}
* @param jn Any {@link JsonNumber}
* @return java {@code double} primitive
* @throws ArithmeticException If infinity is returned from the call to
* {@code BigDecimal.doubleValue()}
* @see JsonNumber#bigDecimalValue()
*/
protected static double DOUBLE_WITH_CHECK(JsonNumber jn)
{ return DOUBLE_WITH_CHECK(jn.bigDecimalValue()); }
/**
* Converts a {@code BigDecimal} into a Java {@code double}
* @param bd Any {@code BigDecimal}
* @return Java {@code double} primitive
* @throws ArithmeticException If infinity is returned from the call to
* {@code code BigDecimal.doubleValue()}
*/
protected static double DOUBLE_WITH_CHECK(BigDecimal bd)
{
double ret = bd.doubleValue();
if (Double.isInfinite(ret)) throwAE_INFINITY(bd, "double");
if (BigDecimal.valueOf(ret).compareTo(bd) != 0) throwAE_PRECISION(bd, "double");
return ret;
}
/**
* Converts a {@link JsonNumber} into a Java {@code float}
* @param jn Any {@link JsonNumber}
* @return java {@code float} primitive
* @throws ArithmeticException If infinity is returned from the call to
* {@code BigDecimal.floatValue()}
* @see JsonNumber#bigDecimalValue()
*/
protected static float FLOAT_WITH_CHECK(JsonNumber jn)
{ return FLOAT_WITH_CHECK(jn.bigDecimalValue()); }
/**
* Converts a {@code BigDecimal} into a Java {@code float}
* @param bd Any {@code BigDecimal}
* @return Java {@code float} primitive
* @throws ArithmeticException If infinity is returned from the call to
* {@code code BigDecimal.floatValue()}
*/
protected static float FLOAT_WITH_CHECK(BigDecimal bd)
{
float ret = bd.floatValue();
if (Float.isInfinite(ret)) throwAE_INFINITY(bd, "float");
if (BigDecimal.valueOf(ret).compareTo(bd) != 0) throwAE_PRECISION(bd, "float");
return ret;
}
/**
* Converts a {@code long} into a Java {@code byte}
* @param l A long that has been produced by {@link JsonNumber#longValueExact()}
* @return A valid {@code byte} primitive
* @throws ArithmeticException If the {@code long} doesn't fit into a {@code byte}
*/
protected static byte BYTE_FROM_LONG(final long l)
{
if ((l < Byte.MIN_VALUE) || (l > Byte.MAX_VALUE))
throw new ArithmeticException("byte out of range: " + l);
return (byte) l;
}
/**
* Converts a {@code long} into a Java {@code short}
* @param l A long that has been produced by {@link JsonNumber#longValueExact()}
* @return A valid {@code short} primitive
* @throws ArithmeticException If the {@code long} doesn't fit into a {@code short}
*/
protected static short SHORT_FROM_LONG(final long l)
{
if ((l < Short.MIN_VALUE) || (l > Short.MAX_VALUE))
throw new ArithmeticException("short out of range: " + l);
return (short) l;
}
// ********************************************************************************************
// ********************************************************************************************
// FLAG-CHECKER METHODS another section of "Helpers for the Helpers ..."
// ********************************************************************************************
// ********************************************************************************************
/**
* Flag Checker for {@code IndexOutOfBoundsException}.
*
* <BR /><BR />Checks whether the relevant flags were set in the users {@code FLAGS} parameter,
* and either returns the appropriate value accordingly, or throws
* {@code IndexOutOfBoundsException}.
*
* <EMBED CLASS='external-html' DATA-FILE-ID=FLAG_PRECEDENCE>
*
* @param <T> If requested, the default-value is returned, and this is its type.
*
* @return Can return either the user-provided default-value, or null depending on whether a
* match was found in the user's request settings ({@code 'FLAGS'}).
*
* @throws IndexOutOfBoundsException If no flag was set specifying one of the two return-value
* options.
*
* @see JFlag#RETURN_NULL_ON_IOB
* @see JFlag#RETURN_DEFVAL_ON_IOB
* @see JFlag#RETURN_NULL_ON_ANY_ALL
* @see JFlag#RETURN_DEFVAL_ON_ANY_ALL
*/
protected static <T> T IOOBEX(JsonArray ja, int index, T defaultValue, int FLAGS)
{
if ((FLAGS & RETURN_NULL_ON_IOB) != 0) return null;
if ((FLAGS & RETURN_DEFVAL_ON_IOB) != 0) return defaultValue;
if ((FLAGS & RETURN_NULL_ON_ANY_ALL) != 0) return null;
if ((FLAGS & RETURN_DEFVAL_ON_ANY_ALL) != 0) return defaultValue;
ja.get(index); // Throws an IndexOutOfBoundsException
// If you have reached this statment, this method was not applied properly
throw new Torello.Java.UnreachableError();
}
/**
* Flag Checker for {@link JsonPropMissingException}
*
* <BR /><BR />Checks whether the relevant flags were set in the users {@code FLAGS} parameter,
* and either returns the appropriate value accordingly, or throws
* {@code JsonPropMissingException}.
*
* <EMBED CLASS='external-html' DATA-FILE-ID=FLAG_PRECEDENCE>
*
* @param <T> If requested, the default-value is returned, and this is its type.
*
* @return Can return either the user-provided default-value, or null depending on whether a
* match was found in the user's request settings ({@code 'FLAGS'}).
*
* @throws JsonPropMissingException If no flag was set specifying one of the two return-value
* options.
*
* @see JFlag#RETURN_NULL_ON_MISSING
* @see JFlag#RETURN_DEFVAL_ON_MISSING
* @see JFlag#RETURN_NULL_ON_ANY_ALL
* @see JFlag#RETURN_DEFVAL_ON_ANY_ALL
*/
protected static <T> T JPMEX(
JsonObject jo, String propertyName, T defaultValue, int FLAGS,
JsonValue.ValueType expectedType, Class<T> returnClass
)
{
if ((FLAGS & RETURN_NULL_ON_MISSING) != 0) return null;
if ((FLAGS & RETURN_DEFVAL_ON_MISSING) != 0) return defaultValue;
if ((FLAGS & RETURN_NULL_ON_ANY_ALL) != 0) return null;
if ((FLAGS & RETURN_DEFVAL_ON_ANY_ALL) != 0) return defaultValue;
throw new JsonPropMissingException(jo, propertyName, expectedType, returnClass);
}
/**
* Flag Checker for {@link JsonNullArrException}
*
* <BR /><BR />Checks whether the relevant flags were set in the users {@code FLAGS} parameter,
* and either returns the appropriate value accordingly, or throws
* {@code JsonNullArrException}.
*
* <EMBED CLASS='external-html' DATA-FILE-ID=FLAG_PRECEDENCE>
*
* @param <T> If requested, the default-value is returned, and this is its type.
*
* @return Can return either the user-provided default-value, or null depending on whether a
* match was found in the user's request settings ({@code 'FLAGS'}).
*
* @throws JsonNullArrException If no flag was set specifying one of the two return-value
* options.
*
* @see JFlag#RETURN_NULL_ON_NULL
* @see JFlag#RETURN_DEFVAL_ON_NULL
* @see JFlag#RETURN_NULL_ON_ANY_ALL
* @see JFlag#RETURN_DEFVAL_ON_ANY_ALL
*/
protected static <T> T JNAEX(
JsonArray ja, int index, T defaultValue, int FLAGS, JsonValue.ValueType expectedType,
Class<T> returnClass
)
{
if ((FLAGS & RETURN_NULL_ON_NULL) != 0) return null;
if ((FLAGS & RETURN_DEFVAL_ON_NULL) != 0) return defaultValue;
if ((FLAGS & RETURN_NULL_ON_ANY_ALL) != 0) return null;
if ((FLAGS & RETURN_DEFVAL_ON_ANY_ALL) != 0) return defaultValue;
throw new JsonNullArrException(ja, index, expectedType, returnClass);
}
/**
* Flag Checker for {@link JsonNullObjException}
*
* <BR /><BR />Checks whether the relevant flags were set in the users {@code FLAGS} parameter,
* and either returns the appropriate value accordingly, or throws
* {@code JsonNullObjException}.
*
* <EMBED CLASS='external-html' DATA-FILE-ID=FLAG_PRECEDENCE>
*
* @param <T> If requested, the default-value is returned, and this is its type.
*
* @return Can return either the user-provided default-value, or null depending on whether a
* match was found in the user's request settings ({@code 'FLAGS'}).
*
* @throws JsonNullObjException If no flag was set specifying one of the two return-value
* options.
*
* @see JFlag#RETURN_NULL_ON_NULL
* @see JFlag#RETURN_DEFVAL_ON_NULL
* @see JFlag#RETURN_NULL_ON_ANY_ALL
* @see JFlag#RETURN_DEFVAL_ON_ANY_ALL
*/
protected static <T> T JNOEX(
JsonObject jo, String propertyName, T defaultValue, int FLAGS,
JsonValue.ValueType expectedType, Class<T> returnClass
)
{
if ((FLAGS & RETURN_NULL_ON_NULL) != 0) return null;
if ((FLAGS & RETURN_DEFVAL_ON_NULL) != 0) return defaultValue;
if ((FLAGS & RETURN_NULL_ON_ANY_ALL) != 0) return null;
if ((FLAGS & RETURN_DEFVAL_ON_ANY_ALL) != 0) return defaultValue;
throw new JsonNullObjException(jo, propertyName, expectedType, returnClass);
}
/**
* Flag Checker for {@link JsonTypeArrException}
*
* <BR /><BR />Checks whether the relevant flags were set in the users {@code FLAGS} parameter,
* and either returns the appropriate value accordingly, or throws
* {@code JsonTypeArrException}.
*
* <EMBED CLASS='external-html' DATA-FILE-ID=FLAG_PRECEDENCE>
*
* @param <T> If requested, the default-value is returned, and this is its type.
*
* @return Can return either the user-provided default-value, or null depending on whether a
* match was found in the user's request settings ({@code 'FLAGS'}).
*
* @throws JsonTypeArrException If no flag was set specifying one of the two return-value
* options.
*
* @see JFlag#RETURN_NULL_ON_WRONG_JSONTYPE
* @see JFlag#RETURN_DEFVAL_ON_WRONG_JSONTYPE
* @see JFlag#RETURN_NULL_ON_ANY_ALL
* @see JFlag#RETURN_DEFVAL_ON_ANY_ALL
*/
protected static <T> T JTAEX(
JsonArray ja, int index, T defaultValue, int FLAGS, JsonValue.ValueType expectedType,
JsonValue retrievedValue, Class<T> returnClass
)
{
if ((FLAGS & RETURN_NULL_ON_WRONG_JSONTYPE) != 0) return null;
if ((FLAGS & RETURN_DEFVAL_ON_WRONG_JSONTYPE) != 0) return defaultValue;
if ((FLAGS & RETURN_NULL_ON_ANY_ALL) != 0) return null;
if ((FLAGS & RETURN_DEFVAL_ON_ANY_ALL) != 0) return defaultValue;
throw new JsonTypeArrException(ja, index, expectedType, retrievedValue, returnClass);
}
/**
* Flag Checker for {@link JsonTypeObjException}
*
* <BR /><BR />Checks whether the relevant flags were set in the users {@code FLAGS} parameter,
* and either returns the appropriate value accordingly, or throws
* {@code JsonNullObjException}.
*
* <EMBED CLASS='external-html' DATA-FILE-ID=FLAG_PRECEDENCE>
*
* @param <T> If requested, the default-value is returned, and this is its type.
*
* @return Can return either the user-provided default-value, or null depending on whether a
* match was found in the user's request settings ({@code 'FLAGS'}).
*
* @throws JsonNullObjException If no flag was set specifying one of the two return-value
* options.
*
* @see JFlag#RETURN_NULL_ON_WRONG_JSONTYPE
* @see JFlag#RETURN_DEFVAL_ON_WRONG_JSONTYPE
* @see JFlag#RETURN_NULL_ON_ANY_ALL
* @see JFlag#RETURN_DEFVAL_ON_ANY_ALL
*/
protected static <T> T JTOEX(
JsonObject jo, String propertyName, T defaultValue, int FLAGS,
JsonValue.ValueType expectedType, JsonValue retrievedValue, Class<T> returnClass
)
{
if ((FLAGS & RETURN_NULL_ON_WRONG_JSONTYPE) != 0) return null;
if ((FLAGS & RETURN_DEFVAL_ON_WRONG_JSONTYPE) != 0) return defaultValue;
if ((FLAGS & RETURN_NULL_ON_ANY_ALL) != 0) return null;
if ((FLAGS & RETURN_DEFVAL_ON_ANY_ALL) != 0) return defaultValue;
throw new JsonTypeObjException
(jo, propertyName, expectedType, retrievedValue, returnClass);
}
/**
* Flag Checker for {@link JsonStrParseArrException}
*
* <BR /><BR />Checks whether the relevant flags were set in the users {@code FLAGS} parameter,
* and either returns the appropriate value accordingly, or throws
* {@code JsonStrParseArrException}.
*
* <EMBED CLASS='external-html' DATA-FILE-ID=FLAG_PRECEDENCE>
*
* @param <T> If requested, the default-value is returned, and this is its type.
*
* @return Can return either the user-provided default-value, or null depending on whether a
* match was found in the user's request settings ({@code 'FLAGS'}).
*
* @throws JsonStrParseArrException If no flag was set specifying one of the two return-value
* options.
*
* @see JFlag#RETURN_NULL_ON_SPEX
* @see JFlag#RETURN_DEFVAL_ON_SPEX
* @see JFlag#RETURN_NULL_ON_ANY_ALL
* @see JFlag#RETURN_DEFVAL_ON_ANY_ALL
*/
protected static <T> T JSPAEX(
Exception e, JsonArray ja, int index, T defaultValue, int FLAGS,
JsonValue retrievedValue, Class<T> returnClass
)
{
if ((FLAGS & RETURN_NULL_ON_SPEX) != 0) return null;
if ((FLAGS & RETURN_DEFVAL_ON_SPEX) != 0) return defaultValue;
if ((FLAGS & RETURN_NULL_ON_ANY_ALL) != 0) return null;
if ((FLAGS & RETURN_DEFVAL_ON_ANY_ALL) != 0) return defaultValue;
throw new JsonStrParseArrException(e, ja, index, retrievedValue, returnClass);
}
/**
* Flag Checker for {@link JsonStrParseObjException}
*
* <BR /><BR />Checks whether the relevant flags were set in the users {@code FLAGS} parameter,
* and either returns the appropriate value accordingly, or throws
* {@code JsonStrParseObjException}.
*
* <EMBED CLASS='external-html' DATA-FILE-ID=FLAG_PRECEDENCE>
*
* @param <T> If requested, the default-value is returned, and this is its type.
*
* @return Can return either the user-provided default-value, or null depending on whether a
* match was found in the user's request settings ({@code 'FLAGS'}).
*
* @throws JsonStrParseObjException If no flag was set specifying one of the two return-value
* options.
*
* @see JFlag#RETURN_NULL_ON_SPEX
* @see JFlag#RETURN_DEFVAL_ON_SPEX
* @see JFlag#RETURN_NULL_ON_ANY_ALL
* @see JFlag#RETURN_DEFVAL_ON_ANY_ALL
*/
protected static <T> T JSPOEX(
Exception e, JsonObject jo, String propertyName, T defaultValue, int FLAGS,
JsonValue retrievedValue, Class<T> returnClass
)
{
if ((FLAGS & RETURN_NULL_ON_SPEX) != 0) return null;
if ((FLAGS & RETURN_DEFVAL_ON_SPEX) != 0) return defaultValue;
if ((FLAGS & RETURN_NULL_ON_ANY_ALL) != 0) return null;
if ((FLAGS & RETURN_DEFVAL_ON_ANY_ALL) != 0) return defaultValue;
throw new JsonStrParseObjException(e, jo, propertyName, retrievedValue, returnClass);
}
}
|