001package Torello.JSON;
002
003import Torello.JavaDoc.Annotations.StaticFunctional;
004import Torello.JavaDoc.Annotations.JDHeaderBackgroundImg;
005import Torello.JavaDoc.Annotations.LinkJavaSource;
006import Torello.JavaDoc.Annotations.IntoHTMLTable;
007
008import static Torello.JavaDoc.Annotations.IntoHTMLTable.Background.GreenDither;
009import static Torello.JavaDoc.Annotations.IntoHTMLTable.Background.BlueDither;
010
011import Torello.Java.Function.IntIntTConsumer;
012import Torello.Java.Function.IntIntTFunc;
013
014import javax.json.Json;
015import javax.json.JsonArray;
016import javax.json.JsonString;
017import javax.json.JsonObject;
018
019import java.util.function.Consumer;
020import java.util.function.Function;
021
022/**
023 * Utilities for parsing Json Array's and sending the parsed values into a Java Consumer
024 * Functional-Interface.
025 * 
026 * <EMBED CLASS='external-html' DATA-FILE-ID=ALL_CLASSES_NOTE>
027 * <EMBED CLASS='external-html' DATA-FILE-ID=RJA_INTO_CONS>
028 * <EMBED CLASS='external-html' DATA-FILE-ID=JO_DESERIALIZE>
029 * <EMBED CLASS='external-html' DATA-FILE-ID=RJ_PT_CONS>
030 *
031 * @see Json
032 * @see JsonArray
033 */
034@StaticFunctional
035@JDHeaderBackgroundImg(EmbedTagFileID="RJA_JDHBI_CONS")
036public class RJArrIntoConsumer
037{
038    private RJArrIntoConsumer() { }
039
040
041    // ********************************************************************************************
042    // ********************************************************************************************
043    // Three methods which send an Object 'T' to a Consumer<T>
044    // ********************************************************************************************
045    // ********************************************************************************************
046
047
048    /** <EMBED CLASS='external-html' DATA-FILE-ID=RJ_ARR_CONS_OBJ1_CONS1> */
049    @LinkJavaSource(handle="EXTENDED_TYPES", name="FROM_CTOR")
050    @LinkJavaSource(handle="SETTINGS_REC_BUILDER", name="oneArgConsumer")
051    @IntoHTMLTable(
052        background=GreenDither,
053        title=
054            "Read a JsonArray of JsonObject's into a Consumer&lt;T&gt;, " +
055            "Class 'T' must have a JsonObject-Constructor"
056    )
057    public static <T> void objArr(
058            final JsonArray     ja,
059            final T             defaultValue,
060            final int           FLAGS,
061            final Class<T>      consumerClass,
062            final Consumer<T>   c
063        )
064    {
065        ProcessJsonArray.objToJava(
066            ja,
067            new SETTINGS_REC_BUILDER<T, Void>
068                (defaultValue, FLAGS, null, EXTENDED_TYPES.FROM_CTOR(consumerClass))
069                .oneArgConsumer(c)
070        );
071    }
072
073    /** <EMBED CLASS='external-html' DATA-FILE-ID=RJ_ARR_CONS_OBJ2_CONS1> */
074    @LinkJavaSource(handle="EXTENDED_TYPES", name="ONE_ARG_FUNC")
075    @LinkJavaSource(handle="SETTINGS_REC_BUILDER", name="oneArgConsumer")
076    @IntoHTMLTable(
077        background=BlueDither,
078        title=
079            "Read a JsonArray of JsonObject's into a Consumer&lt;T&gt;, " +
080            "Parameter 'objBuilder' must generate instances of 'T'"
081    )
082    public static <T> void objArr(
083            final JsonArray                 ja,
084            final T                         defaultValue,
085            final int                       FLAGS,
086            final Function<JsonObject, T>   objBuilder,
087            final Class<T>                  consumerClass,
088            final Consumer<T>               c
089        )
090    {
091        ProcessJsonArray.objToJava(
092            ja,
093            new SETTINGS_REC_BUILDER<T, Void>
094                (defaultValue, FLAGS, null, EXTENDED_TYPES.ONE_ARG_FUNC(objBuilder, consumerClass))
095                .oneArgConsumer(c)
096        );
097    }
098
099    /** <EMBED CLASS='external-html' DATA-FILE-ID=RJ_ARR_CONS_OBJ3_CONS1> */
100    @LinkJavaSource(handle="EXTENDED_TYPES", name="THREE_ARG_FUNC")
101    @LinkJavaSource(handle="SETTINGS_REC_BUILDER", name="oneArgConsumer")
102    @IntoHTMLTable(
103        background=GreenDither,
104        title=
105            "Read a JsonArray of JsonObject's into a Consumer&lt;T&gt;, " +
106            "Parameter 'objBuilder' must generate instances of 'T'"
107    )
108    public static <T> void objArr(
109            final JsonArray                     ja,
110            final T                             defaultValue,
111            final int                           FLAGS,
112            final IntIntTFunc<JsonObject, T>    objBuilder,
113            final Class<T>                      consumerClass,
114            final Consumer<T>                   c
115        )
116    {
117        ProcessJsonArray.objToJava(
118            ja,
119            new SETTINGS_REC_BUILDER<T, Void>
120                (defaultValue, FLAGS, null,
121                    EXTENDED_TYPES.THREE_ARG_FUNC(objBuilder, consumerClass))
122                .oneArgConsumer(c)
123        );
124    }
125
126
127    // ********************************************************************************************
128    // ********************************************************************************************
129    // Three methods which send an Object 'T' to an IntIntTConsumer<T> Variant
130    // ********************************************************************************************
131    // ********************************************************************************************
132
133
134    /** <EMBED CLASS='external-html' DATA-FILE-ID=RJ_ARR_CONS_OBJ1_CONS2> */
135    @LinkJavaSource(handle="EXTENDED_TYPES", name="FROM_CTOR")
136    @LinkJavaSource(handle="SETTINGS_REC_BUILDER", name="threeArgConsumer")
137    @IntoHTMLTable(
138        background=BlueDither,
139        title=
140            "Read a JsonArray of JsonObject's into an "+
141            "IntIntTConsumer&lt;T&gt;, Class 'T' must have a JsonObject-Constructor"
142    )
143    public static <T> void objArr2(
144            final JsonArray             ja,
145            final T                     defaultValue,
146            final int                   FLAGS,
147            final Class<T>              consumerClass,
148            final IntIntTConsumer<T>    c
149        )
150    {
151        ProcessJsonArray.objToJava(
152            ja,
153            new SETTINGS_REC_BUILDER<T, Void>
154                (defaultValue, FLAGS, null, EXTENDED_TYPES.FROM_CTOR(consumerClass))
155                .threeArgConsumer(c)
156        );
157    }
158
159    /** <EMBED CLASS='external-html' DATA-FILE-ID=RJ_ARR_CONS_OBJ2_CONS2> */
160    @LinkJavaSource(handle="EXTENDED_TYPES", name="ONE_ARG_FUNC")
161    @LinkJavaSource(handle="SETTINGS_REC_BUILDER", name="threeArgConsumer")
162    @IntoHTMLTable(
163        background=GreenDither,
164        title=
165            "Read a JsonArray of JsonObject's into an "+
166            "IntIntTConsumer&lt;T&gt;, Parameter 'objBuilder' must generate instances of 'T'"
167    )
168    public static <T> void objArr2(
169            final JsonArray                 ja,
170            final T                         defaultValue,
171            final int                       FLAGS,
172            final Function<JsonObject, T>   objBuilder,
173            final Class<T>                  consumerClass,
174            final IntIntTConsumer<T>        c
175        )
176    {
177        ProcessJsonArray.objToJava(
178            ja,
179            new SETTINGS_REC_BUILDER<T, Void>
180                (defaultValue, FLAGS, null, EXTENDED_TYPES.ONE_ARG_FUNC(objBuilder, consumerClass))
181                .threeArgConsumer(c)
182        );
183    }
184
185    /** <EMBED CLASS='external-html' DATA-FILE-ID=RJ_ARR_CONS_OBJ3_CONS2> */
186    @LinkJavaSource(handle="EXTENDED_TYPES", name="THREE_ARG_FUNC")
187    @LinkJavaSource(handle="SETTINGS_REC_BUILDER", name="threeArgConsumer")
188    @IntoHTMLTable(
189        background=BlueDither,
190        title=
191            "Read a JsonArray of JsonObject's into an IntIntTConsumer&lt;T&gt;, " +
192            "Parameter 'objBuilder' must generate instances of 'T'"
193    )
194    public static <T> void objArr2(
195            final JsonArray                     ja,
196            final T                             defaultValue,
197            final int                           FLAGS,
198            final IntIntTFunc<JsonObject, T>    objBuilder,
199            final Class<T>                      consumerClass,
200            final IntIntTConsumer<T>            c
201        )
202    {
203        ProcessJsonArray.objToJava(
204            ja,
205            new SETTINGS_REC_BUILDER<T, Void>
206                (defaultValue, FLAGS, null, EXTENDED_TYPES
207                    .THREE_ARG_FUNC(objBuilder, consumerClass))
208                .threeArgConsumer(c)
209        );
210    }
211
212
213    // ********************************************************************************************
214    // ********************************************************************************************
215    // java.lang.String ==> *BOTH* Consumer<T> *and* IntIntTConsumer<T>
216    // ********************************************************************************************
217    // ********************************************************************************************
218
219
220    /** <EMBED CLASS='external-html' DATA-FILE-ID=RJ_ARR_CONS_STR_CONS1> */
221    @LinkJavaSource(handle="BASIC_TYPES", name="STRING_REC")
222    @LinkJavaSource(handle="SETTINGS_REC_BUILDER", name="oneArgConsumer")
223    @IntoHTMLTable(
224        background=GreenDither,
225        title="Reads a JsonArray of String's into a Java Consumer&lt;String&gt;"
226    )
227    public static void strArr(
228            final JsonArray         ja, 
229            final String            defaultValue,
230            final int               FLAGS,
231            final Consumer<String>  c
232        )
233    {
234        ProcessJsonArray.strToJava(
235            ja,
236            new SETTINGS_REC_BUILDER<String, Void>
237                (defaultValue, FLAGS, null, BASIC_TYPES.STRING_REC())
238                .oneArgConsumer(c)
239        );
240    }
241
242    /** <EMBED CLASS='external-html' DATA-FILE-ID=RJ_ARR_CONS_STR_CONS2> */
243    @LinkJavaSource(handle="BASIC_TYPES", name="STRING_REC")
244    @LinkJavaSource(handle="SETTINGS_REC_BUILDER", name="threeArgConsumer")
245    @IntoHTMLTable(
246        background=BlueDither,
247        title="Reads a JsonArray of String's into an IntIntTConsumer&lt;String&gt;"
248    )
249    public static void strArr2(
250            final JsonArray                 ja, 
251            final String                    defaultValue,
252            final int                       FLAGS,
253            final IntIntTConsumer<String>   c
254        )
255    {
256        ProcessJsonArray.strToJava(
257            ja,
258            new SETTINGS_REC_BUILDER<String, Void>
259                (defaultValue, FLAGS, null, BASIC_TYPES.STRING_REC())
260                .threeArgConsumer(c)
261        );
262    }
263
264
265    // ********************************************************************************************
266    // ********************************************************************************************
267    // javax.json.JsonObject ==> *BOTH* Consumer<JsonObject> *and* IntIntTConsumer<JsonObject>
268    // ********************************************************************************************
269    // ********************************************************************************************
270
271
272    /** <EMBED CLASS='external-html' DATA-FILE-ID=RJ_ARR_CONS_JO_CONS1> */
273    @LinkJavaSource(handle="BASIC_TYPES", name="JSON_OBJECT_REC")
274    @LinkJavaSource(handle="SETTINGS_REC_BUILDER", name="oneArgConsumer")
275    @IntoHTMLTable(
276        background=GreenDither,
277        title="Reads a JsonArray of JsonObject's into a Java Consumer&lt;JsonObject&gt;"
278    )
279    public static void joArr(
280            final JsonArray             ja, 
281            final JsonObject            defaultValue,
282            final int                   FLAGS,
283            final Consumer<JsonObject>  c
284        )
285    {
286        ProcessJsonArray.joToJava(
287            ja,
288            new SETTINGS_REC_BUILDER<JsonObject, Void>
289                (defaultValue, FLAGS, null, BASIC_TYPES.JSON_OBJECT_REC())
290                .oneArgConsumer(c)
291        );
292    }
293
294    /** <EMBED CLASS='external-html' DATA-FILE-ID=RJ_ARR_CONS_JO_CONS2> */
295    @LinkJavaSource(handle="BASIC_TYPES", name="JSON_OBJECT_REC")
296    @LinkJavaSource(handle="SETTINGS_REC_BUILDER", name="threeArgConsumer")
297    @IntoHTMLTable(
298        background=BlueDither,
299        title="Reads a JsonArray of JsonObject's into an IntIntTConsumer&lt;JsonObject&gt;"
300    )
301    public static void joArr2(
302            final JsonArray                     ja, 
303            final JsonObject                    defaultValue,
304            final int                           FLAGS,
305            final IntIntTConsumer<JsonObject>   c
306        )
307    {
308        ProcessJsonArray.joToJava(
309            ja,
310            new SETTINGS_REC_BUILDER<JsonObject, Void>
311                (defaultValue, FLAGS, null, BASIC_TYPES.JSON_OBJECT_REC())
312                .threeArgConsumer(c)
313        );
314    }
315
316
317    // ********************************************************************************************
318    // ********************************************************************************************
319    // javax.json.JsonArray ==> *BOTH* Consumer<JsonArray> *and* IntIntTConsumer<JsonArray>
320    // ********************************************************************************************
321    // ********************************************************************************************
322
323
324    /** <EMBED CLASS='external-html' DATA-FILE-ID=RJ_ARR_CONS_JA_CONS1> */
325    @LinkJavaSource(handle="BASIC_TYPES", name="JSON_ARRAY_REC")
326    @LinkJavaSource(handle="SETTINGS_REC_BUILDER", name="oneArgConsumer")
327    @IntoHTMLTable(
328        background=GreenDither,
329        title="Reads a JsonArray of JsonObject's into a Java Consumer&lt;JsonArray&gt;"
330    )
331    public static void jaArr(
332            final JsonArray             ja, 
333            final JsonArray             defaultValue,
334            final int                   FLAGS,
335            final Consumer<JsonArray>   c
336        )
337    {
338        ProcessJsonArray.jaToJava(
339            ja,
340            new SETTINGS_REC_BUILDER<JsonArray, Void>
341                (defaultValue, FLAGS, null, BASIC_TYPES.JSON_ARRAY_REC())
342                .oneArgConsumer(c)
343        );
344    }
345
346    /** <EMBED CLASS='external-html' DATA-FILE-ID=RJ_ARR_CONS_JA_CONS2> */
347    @LinkJavaSource(handle="BASIC_TYPES", name="JSON_ARRAY_REC")
348    @LinkJavaSource(handle="SETTINGS_REC_BUILDER", name="threeArgConsumer")
349    @IntoHTMLTable(
350        background=BlueDither,
351        title="Reads a JsonArray of JsonArray's into an IntIntTConsumer&lt;JsonArray&gt;"
352    )
353    public static void jaArr2(
354            final JsonArray                     ja, 
355            final JsonArray                     defaultValue,
356            final int                           FLAGS,
357            final IntIntTConsumer<JsonArray>    c
358        )
359    {
360        ProcessJsonArray.jaToJava(
361            ja,
362            new SETTINGS_REC_BUILDER<JsonArray, Void>
363                (defaultValue, FLAGS, null, BASIC_TYPES.JSON_ARRAY_REC())
364                .threeArgConsumer(c)
365        );
366    }
367
368}