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 | package Torello.Browser;
import java.math.BigInteger;
import java.math.BigDecimal;
import javax.json.stream.JsonGenerator;
/**
* Yes, this is a great name for a class.
*
* <BR /><BR />
* This class can capable write all variety of numbers, and arrays of numbers to a provided
* {@link JsonGenerator} instance, no more no less.
*
* <BR /><BR /><DIV CLASS=JDHint>
* 💡 Java's (somewhat) standardized "Glass Fish" implementation is great, but is a little
* "unfinished".
* </DIV>
*/
@Torello.JavaDoc.Annotations.StaticFunctional
@Torello.JavaDoc.Annotations.JDHeaderBackgroundImg(EmbedTagFileID="INTERNAL_USE_JDHBI")
public class NumberCrap
{
private NumberCrap() { }
/**
* Writes a number to an output {@link JsonGenerator}.
*
* @param jGen The output Json stream used to properly format Json into a string
* @param paramName The Json property name for the number being written to the output
*
* @param paramValue Any one of the valid, supported types.
* <BR />👉 Please review the hilited code below, in order to view which types are supported
*
* @throws InvalidNumberTypeError
* <EMBED CLASS='external-html' DATA-FILE-ID=NumberCrap.InvalidNumberTypeError>
*/
public static void handleNumber(
final JsonGenerator jGen,
final String paramName,
final Number paramValue
)
{
if (paramValue == null) throw new InvalidNumberTypeError
("Parameter 'paramValue' was passed null. This should be a valid java.lang.Number");
if (paramName != null) switch (paramValue.getClass().getSimpleName())
{
case "Integer" : jGen.write(paramName, ((Integer) paramValue).intValue()); break;
case "Long" : jGen.write(paramName, ((Long) paramValue).longValue()); break;
case "Float" : jGen.write(paramName, ((Float) paramValue).floatValue()); break;
case "Double" : jGen.write(paramName, ((Double) paramValue).doubleValue()); break;
case "BigInteger" : jGen.write(paramName, ((BigInteger) paramValue)); break;
case "BigDecimal" : jGen.write(paramName, ((BigDecimal) paramValue)); break;
default : throw new InvalidNumberTypeError(
"Parameter 'paramValue' has an invalid Number-Type: " +
paramValue.getClass().getCanonicalName() + '\n'
);
}
else switch (paramValue.getClass().getSimpleName())
{
case "Integer" : jGen.write(((Integer) paramValue).intValue()); break;
case "Long" : jGen.write(((Long) paramValue).longValue()); break;
case "Float" : jGen.write(((Float) paramValue).floatValue()); break;
case "Double" : jGen.write(((Double) paramValue).doubleValue()); break;
case "BigInteger" : jGen.write(((BigInteger) paramValue)); break;
case "BigDecimal" : jGen.write(((BigDecimal) paramValue)); break;
default : throw new InvalidNumberTypeError(
"Parameter 'paramValue' has an invalid Number-Type: " +
paramValue.getClass().getCanonicalName() + '\n'
);
}
}
/**
* Writes a numeric array to an output {@link JsonGenerator}.
*
* @param jGen The output Json stream used to properly format Json into a string
* @param paramName The Json property name for the number being written to the output
*
* @param paramValue An array having one of the pre-selected types.
* <BR />👉 Please review the hilited code below, in order to view which types are supported
*
* @throws InvalidNumberTypeError
* <EMBED CLASS='external-html' DATA-FILE-ID=NumberCrap.InvalidNumberTypeError>
*/
public static void handleNumberArr(
final JsonGenerator jGen,
final String paramName,
final Object paramValue
)
{
if (paramValue == null) throw new InvalidNumberTypeError(
"Parameter 'paramValue' was passed null. This should be a valid, one dimensional " +
"array of numbers"
);
if (paramName == null) jGen.writeStartArray();
else jGen.writeStartArray(paramName);
final String typeAsStr = paramValue.getClass().getSimpleName();
switch (typeAsStr)
{
// *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
// primitive arrays
// *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
case "int[]" :
for (final int i : (int[]) paramValue) jGen.write(i);
break;
case "long[]" :
for (final long l : (long[]) paramValue) jGen.write(l);
break;
case "float[]" :
for (final float f : (float[]) paramValue) jGen.write(f);
break;
case "double[]" :
for (final double d : (double[]) paramValue) jGen.write(d);
break;
// *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
// boxed arrays
// *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
case "Integer[]" :
for (final Integer i : (Integer[]) paramValue)
if (i == null) jGen.writeNull();
else jGen.write(i.intValue());
break;
case "Long[]" :
for (final Long l : (Long[]) paramValue)
if (l == null) jGen.writeNull();
else jGen.write(l.longValue());
break;
case "Float[]" :
for (final Float f : (Float[]) paramValue)
if (f == null) jGen.writeNull();
else jGen.write(f.floatValue());
break;
case "Double[]" :
for (final Double d : (Double[]) paramValue)
if (d == null) jGen.writeNull();
else jGen.write(d.doubleValue());
break;
case "Number[]" :
for (final Number n : (Number[]) paramValue)
if (n == null) jGen.writeNull();
else handleNumber(jGen, null, n);
break;
// *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
// Other
// *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
default :
if (typeAsStr.endsWith("[]"))
throw new InvalidNumberTypeError(
"Input array-parameter 'paramValue' has an invalid class / type: " +
paramValue.getClass().getCanonicalName() + '\n' +
"This should be a one-dimensional array of numbers. Please see this " +
"class' source code for the complete list of valid array types."
);
else throw new InvalidNumberTypeError(
"Input array-parameter 'paramValue' isn't actually a one-dimensional array: " +
paramValue.getClass().getCanonicalName() + '\n'
);
}
jGen.writeEnd();
}
/**
* Writes any numeric array having any concrete implementation of the abstract Java Type
* {@code java.lang.Number} to an output {@link JsonGenerator}.
*
* @param jGen The output Json stream used to properly format Json into a string
* @param paramName The Json property name for the number being written to the output
*
* @param paramValue Any two-dimensional array of numbers having one of the pre-selected types
* <BR />👉 Please review the hilited code below, in order to view which types are supported
*
* @throws InvalidNumberTypeError
* <EMBED CLASS='external-html' DATA-FILE-ID=NumberCrap.InvalidNumberTypeError>
*/
public static void handleNumberArr2D(JsonGenerator jGen, String paramName, Object paramValue)
{
if (paramValue == null) throw new InvalidNumberTypeError(
"Parameter 'paramValue' was passed null. This should be a valid, two dimensional " +
"array of numbers"
);
jGen.writeStartArray(paramName);
final String typeAsStr = paramValue.getClass().getSimpleName();
switch (paramValue.getClass().getSimpleName())
{
// *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
// primitive arrays
// *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
case "int[][]" :
for (final int[] iArr : (int[][]) paramValue)
{
jGen.writeStartArray();
for (final int i : iArr) jGen.write(i);
jGen.writeEnd();
}
break;
case "long[][]" :
for (final long[] lArr : (long[][]) paramValue)
{
jGen.writeStartArray();
for (final long l : lArr) jGen.write(l);
jGen.writeEnd();
}
break;
case "float[][]" :
for (final float[] fArr : (float[][]) paramValue)
{
jGen.writeStartArray();
for (final float f : fArr) jGen.write(f);
jGen.writeEnd();
}
break;
case "double[][]" :
for (final double[] dArr : (double[][]) paramValue)
{
jGen.writeStartArray();
for (final double d : dArr) jGen.write(d);
jGen.writeEnd();
}
break;
// *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
// boxed arrays
// *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
case "Integer[][]" :
for (final Integer[] iArr : (Integer[][]) paramValue)
if (iArr == null) jGen.writeNull();
else
{
jGen.writeStartArray();
for (final Integer i : iArr)
if (i == null) jGen.writeNull();
else jGen.write(i.intValue());
jGen.writeEnd();
}
break;
case "Long[][]" :
for (final Long[] lArr : (Long[][]) paramValue)
if (lArr == null) jGen.writeNull();
else
{
jGen.writeStartArray();
for (final Long l : lArr)
if (l == null) jGen.writeNull();
else jGen.write(l.longValue());
jGen.writeEnd();
}
break;
case "Float[][]" :
for (final Float[] fArr : (Float[][]) paramValue)
if (fArr == null) jGen.writeNull();
else
{
jGen.writeStartArray();
for (final Float f : fArr)
if (f == null) jGen.writeNull();
else jGen.write(f.floatValue());
jGen.writeEnd();
}
break;
case "Double[][]" :
for (final Double[] dArr : (Double[][]) paramValue)
if (dArr == null) jGen.writeNull();
else
{
jGen.writeStartArray();
for (final Double d : dArr)
if (d == null) jGen.writeNull();
else jGen.write(d.doubleValue());
jGen.writeEnd();
}
break;
case "Number[][]" :
for (final Number[] nArr : (Number[][]) paramValue)
if (nArr == null) jGen.writeNull();
else handleNumberArr(jGen, null, nArr);
break;
// *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
// Other
// *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
default :
if (typeAsStr.endsWith("[]")) throw new InvalidNumberTypeError(
"Input array-parameter 'paramValue' has an invalid class / type: " +
paramValue.getClass().getCanonicalName() + '\n' +
"This should be a two-dimensional array of numbers. Please see this " +
"class' source code for the complete list of valid array types."
);
else throw new InvalidNumberTypeError(
"Input array-parameter 'paramValue' isn't actually a two-dimensional array: " +
'[' + typeAsStr + ']'
);
}
jGen.writeEnd();
}
}
|