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
package Torello.JSON;

import static javax.json.JsonValue.ValueType.*;

import Torello.JavaDoc.Annotations.IntoHTMLTable;
import static Torello.JavaDoc.Annotations.IntoHTMLTable.Background.BlueDither;
import static Torello.JavaDoc.Annotations.IntoHTMLTable.Background.GreenDither;

import java.util.function.Predicate;
import java.util.function.ToIntFunction;
import java.util.function.ToLongFunction;
import java.util.function.ToDoubleFunction;

import Torello.Java.Function.ToByteFunction;
import Torello.Java.Function.ToShortFunction;
import Torello.Java.Function.ToFloatFunction;

import javax.json.JsonArray;
import javax.json.JsonObject;
import javax.json.JsonValue;
import javax.json.JsonString;
import javax.json.JsonNumber;

import java.util.function.Function;


/**
 * Utilities for parsing &amp; converting <B><I>{@link JsonString}'s</I>
 * <SPAN STYLE='color: red;'>or</SPAN> <I>{@link JsonNumber}'s</I></B> into Java
 * Boxed-Primitive Types.
 * 
 * <EMBED CLASS='external-html' DATA-FILE-ID=ALL_CLASSES_NOTE>
 * <EMBED CLASS='external-html' DATA-FILE-ID=RORP_PRIM_JSON>
 * <EMBED CLASS='external-html' DATA-FILE-ID=RORP_PRIM_PTABLE>
 * <EMBED CLASS='external-html' DATA-CH='java.lang.Character' DATA-FILE-ID=JAVA_LANG_CHAR>
 * 
 * @see JsonObject
 * @see JsonArray
 */
@Torello.JavaDoc.Annotations.StaticFunctional
public class RorPPrimJSON
{
    private RorPPrimJSON() { }


    // ****************************************************************************************
    // ****************************************************************************************
    // JsonArray index location to Boxed Primitive, ACCEPTS USER FLAGS
    // ****************************************************************************************
    // ****************************************************************************************


    /**
     * <EMBED CLASS='external-html' DATA-TYPE=int DATA-FILE-ID=RORP_PRIM_JA>
     * @see ReadPrimJSON#getInt(JsonArray, int)
     * @see ParsePrimJSON#parseInt(JsonArray, int, ToIntFunction)
     */
    @IntoHTMLTable(
        title=  "Read either a JsonNumber or a JsonString from a JsonArray, "+
                "and Translate to a Java 'int' Primitive",
        background=BlueDither
    )
    public static int getInteger(
            final JsonArray             ja,
            final int                   i,
            final ToIntFunction<String> optionalUserParser
        )
    {
        return ((i >= ja.size()) || (ja.get(i).getValueType() != STRING))
            ? ReadPrimJSON.getInt(ja, i)
            : ParsePrimJSON.parseInt(ja, i, optionalUserParser);
    }

    /**
     * <EMBED CLASS='external-html' DATA-TYPE=long DATA-FILE-ID=RORP_PRIM_JA>
     * @see ReadPrimJSON#getLong(JsonArray, int)
     * @see ParsePrimJSON#parseLong(JsonArray, int, ToLongFunction)
     */
    @IntoHTMLTable(
        title=  "Read either a JsonNumber or a JsonString from a JsonArray, "+
                "and Translate to a Java 'long' Primitive",
        background=GreenDither
    )
    public static long getLong(
            final JsonArray                 ja,
            final int                       i,
            final ToLongFunction<String>    optionalUserParser
        )
    {
        return ((i >= ja.size()) || (ja.get(i).getValueType() != STRING))
            ? ReadPrimJSON.getLong(ja, i)
            : ParsePrimJSON.parseLong(ja, i, optionalUserParser);
    }

    /**
     * <EMBED CLASS='external-html' DATA-TYPE=short DATA-FILE-ID=RORP_PRIM_JA>
     * @see ReadPrimJSON#getShort(JsonArray, int)
     * @see ParsePrimJSON#parseShort(JsonArray, int, ToShortFunction)
     */
    @IntoHTMLTable(
        title=  "Read either a JsonNumber or a JsonString from a JsonArray, "+
                "and Translate to a Java 'short' Primitive",
        background=BlueDither
    )
    public static short getShort(
            final JsonArray                 ja,
            final int                       i,
            final ToShortFunction<String>   optionalUserParser
        )
    {
        return ((i >= ja.size()) || (ja.get(i).getValueType() != STRING))
            ? ReadPrimJSON.getShort(ja, i)
            : ParsePrimJSON.parseShort(ja, i, optionalUserParser);
    }

    /**
     * <EMBED CLASS='external-html' DATA-TYPE=byte DATA-FILE-ID=RORP_PRIM_JA>
     * @see ReadPrimJSON#getByte(JsonArray, int)
     * @see ParsePrimJSON#parseByte(JsonArray, int, ToByteFunction)
     */
    @IntoHTMLTable(
        title=  "Read either a JsonNumber or a JsonString from a JsonArray, "+
                "and Translate to a Java 'byte' Primitive",
        background=GreenDither
    )
    public static byte getByte(
            final JsonArray                 ja,
            final int                       i,
            final ToByteFunction<String>    optionalUserParser
        )
    {
        return ((i >= ja.size()) || (ja.get(i).getValueType() != STRING))
            ? ReadPrimJSON.getByte(ja, i)
            : ParsePrimJSON.parseByte(ja, i, optionalUserParser);
    }

    /**
     * <EMBED CLASS='external-html' DATA-TYPE=double DATA-FILE-ID=RORP_PRIM_JA>
     * @see ReadPrimJSON#getDouble(JsonArray, int)
     * @see ParsePrimJSON#parseDouble(JsonArray, int, ToDoubleFunction)
     */
    @IntoHTMLTable(
        title=  "Read either a JsonNumber or a JsonString from a JsonArray, "+
                "and Translate to a Java 'double' Primitive",
        background=BlueDither
    )
    public static double getDouble(
            final JsonArray                 ja,
            final int                       i,
            final ToDoubleFunction<String>  optionalUserParser
        )
    {
        return ((i >= ja.size()) || (ja.get(i).getValueType() != STRING))
            ? ReadPrimJSON.getDouble(ja, i)
            : ParsePrimJSON.parseDouble(ja, i, optionalUserParser);
    }

    /**
     * <EMBED CLASS='external-html' DATA-TYPE=float DATA-FILE-ID=RORP_PRIM_JA>
     * @see ReadPrimJSON#getFloat(JsonArray, int)
     * @see ParsePrimJSON#parseFloat(JsonArray, int, ToFloatFunction)
     */
    @IntoHTMLTable(
        title=  "Read either a JsonNumber or a JsonString from a JsonArray, "+
                "and Translate to a Java 'float' Primitive",
        background=GreenDither
    )
    public static float getFloat(
            final JsonArray                 ja,
            final int                       i,
            final ToFloatFunction<String>   optionalUserParser
        )
    {
        return ((i >= ja.size()) || (ja.get(i).getValueType() != STRING))
            ? ReadPrimJSON.getFloat(ja, i)
            : ParsePrimJSON.parseFloat(ja, i, optionalUserParser);
    }

    /**
     * <EMBED CLASS='external-html' DATA-TYPE=boolean DATA-FILE-ID=RORP_PRIM_JA>
     * @see ReadPrimJSON#getBoolean(JsonArray, int)
     * @see ParsePrimJSON#parseBoolean(JsonArray, int, Predicate)
     */
    @IntoHTMLTable(
        title=  "Read either a Json TRUE, FALSE or a JsonString from a JsonArray, "+
                "and Translate to a Java 'boolean' Primitive",
        background=BlueDither
    )
    public static boolean getBoolean(
            final JsonArray         ja,
            final int               i,
            final Predicate<String> optionalUserParser
        )
    {
        return ((i >= ja.size()) ||  (ja.get(i).getValueType() != STRING))
            ? ReadPrimJSON.getBoolean(ja, i)
            : ParsePrimJSON.parseBoolean(ja, i, optionalUserParser);
    }


    // ****************************************************************************************
    // ****************************************************************************************
    // JsonObject Property to Boxed Primitive, ACCEPTS USER FLAGS
    // ****************************************************************************************
    // ****************************************************************************************


    /**
     * <EMBED CLASS='external-html' DATA-TYPE=int DATA-FILE-ID=RORP_PRIM_JO>
     * @see ReadPrimJSON#getInt(JsonObject, String)
     * @see ParsePrimJSON#parseInt(JsonObject, String, ToIntFunction)
     */
    @IntoHTMLTable(
        title=  "Read either a JsonNumber or a JsonString from a JsonObject, "+
                "and Translate to a Java 'int' Primitive",
        background=GreenDither
    )
    public static int getInteger(
            final JsonObject            jo,
            final String                propertyName,
            final ToIntFunction<String> optionalUserParser
        )
    {
        final JsonValue jv = jo.get(propertyName);

        return ((jv == null) || (jv.getValueType() != STRING))
            ? ReadPrimJSON.getInt(jo, propertyName)
            : ParsePrimJSON.parseInt(jo, propertyName, optionalUserParser);
    }

    /**
     * <EMBED CLASS='external-html' DATA-TYPE=long DATA-FILE-ID=RORP_PRIM_JO>
     * @see ReadPrimJSON#getLong(JsonObject, String)
     * @see ParsePrimJSON#parseLong(JsonObject, String, ToLongFunction)
     */
    @IntoHTMLTable(
        title=  "Read either a JsonNumber or a JsonString from a JsonObject, "+
                "and Translate to a Java 'long' Primitive",
        background=BlueDither
    )
    public static long getLong(
            final JsonObject                jo,
            final String                    propertyName,
            final ToLongFunction<String>    optionalUserParser
        )
    {
        final JsonValue jv = jo.get(propertyName);

        return ((jv == null) || (jv.getValueType() != STRING))
            ? ReadPrimJSON.getLong(jo, propertyName)
            : ParsePrimJSON.parseLong(jo, propertyName, optionalUserParser);
    }

    /**
     * <EMBED CLASS='external-html' DATA-TYPE=short DATA-FILE-ID=RORP_PRIM_JO>
     * @see ReadPrimJSON#getShort(JsonObject, String)
     * @see ParsePrimJSON#parseShort(JsonObject, String, ToShortFunction)
     */
    @IntoHTMLTable(
        title=  "Read either a JsonNumber or a JsonString from a JsonObject, "+
                "and Translate to a Java 'short' Primitive",
        background=GreenDither
    )
    public static short getShort(
            final JsonObject                jo,
            final String                    propertyName,
            final ToShortFunction<String>   optionalUserParser
        )
    {
        final JsonValue jv = jo.get(propertyName);

        return ((jv == null) || (jv.getValueType() != STRING))
            ? ReadPrimJSON.getShort(jo, propertyName)
            : ParsePrimJSON.parseShort(jo, propertyName, optionalUserParser);
    }

    /**
     * <EMBED CLASS='external-html' DATA-TYPE=byte DATA-FILE-ID=RORP_PRIM_JO>
     * @see ReadPrimJSON#getByte(JsonObject, String)
     * @see ParsePrimJSON#parseByte(JsonObject, String, ToByteFunction)
     */
    @IntoHTMLTable(
        title=  "Read either a JsonNumber or a JsonString from a JsonObject, "+
                "and Translate to a Java 'byte' Primitive",
        background=BlueDither
    )
    public static byte getByte(
            final JsonObject                jo,
            final String                    propertyName,
            final ToByteFunction<String>    optionalUserParser
        )
    {
        final JsonValue jv = jo.get(propertyName);

        return ((jv == null) || (jv.getValueType() != STRING))
            ? ReadPrimJSON.getByte(jo, propertyName)
            : ParsePrimJSON.parseByte(jo, propertyName, optionalUserParser);
    }

    /**
     * <EMBED CLASS='external-html' DATA-TYPE=double DATA-FILE-ID=RORP_PRIM_JO>
     * @see ReadPrimJSON#getDouble(JsonObject, String)
     * @see ParsePrimJSON#parseDouble(JsonObject, String, ToDoubleFunction)
     */
    @IntoHTMLTable(
        title=  "Read either a JsonNumber or a JsonString from a JsonObject, "+
                "and Translate to a Java 'double' Primitive",
        background=GreenDither
    )
    public static double getDouble(
            final JsonObject                jo,
            final String                    propertyName,
            final ToDoubleFunction<String>  optionalUserParser
        )
    {
        final JsonValue jv = jo.get(propertyName);

        return ((jv == null) || (jv.getValueType() != STRING))
            ? ReadPrimJSON.getDouble(jo, propertyName)
            : ParsePrimJSON.parseDouble(jo, propertyName, optionalUserParser);
    }

    /**
     * <EMBED CLASS='external-html' DATA-TYPE=float DATA-FILE-ID=RORP_PRIM_JO>
     * @see ReadPrimJSON#getFloat(JsonObject, String)
     * @see ParsePrimJSON#parseFloat(JsonObject, String, ToFloatFunction)
     */
    @IntoHTMLTable(
        title=  "Read either a JsonNumber or a JsonString from a JsonObject, "+
                "and Translate to a Java 'float' Primitive",
        background=BlueDither
    )
    public static float getFloat(
            final JsonObject                jo,
            final String                    propertyName,
            final ToFloatFunction<String>   optionalUserParser
        )
    {
        final JsonValue jv = jo.get(propertyName);

        return ((jv == null) || (jv.getValueType() != STRING))
            ? ReadPrimJSON.getFloat(jo, propertyName)
            : ParsePrimJSON.parseFloat(jo, propertyName, optionalUserParser);
    }

    /**
     * <EMBED CLASS='external-html' DATA-TYPE=boolean DATA-FILE-ID=RORP_PRIM_JO>
     * @see ReadPrimJSON#getBoolean(JsonObject, String)
     * @see ParsePrimJSON#parseBoolean(JsonObject, String, Predicate)
     */
    @IntoHTMLTable(
        title=  "Read either a Json TRUE, FALSE or a JsonString from a JsonObject, " +
                "and Translate to a Java 'boolean' Primitive",
        background=GreenDither
    )
    public static boolean getBoolean(
            final JsonObject        jo,
            final String            propertyName,
            final Predicate<String> optionalUserParser
        )
    {
        final JsonValue jv = jo.get(propertyName);

        return ((jv == null) || (jv.getValueType() != STRING))
            ? ReadPrimJSON.getBoolean(jo, propertyName)
            : ParsePrimJSON.parseBoolean(jo, propertyName, optionalUserParser);
    }

}