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

import Torello.Java.StringParse;

import java.lang.reflect.Array;

import javax.json.JsonArray;
import javax.json.JsonValue;

import java.util.Objects;

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



public class ProcessMultiDimJsonArray
{
    private ProcessMultiDimJsonArray() { }

    /**
     * This class is invoked by the class {@link RJArrDimN}
     * 
     * @param <BASIC_TYPE> The "Component Type" of the Output-Array.
     * 
     * @param <STREAM_TYPE> The "Intermediate Stream Type", which is present before the conversion
     * to an Array.
     * 
     * @param <RETURN_ARR_TYPE> The actual Return-Type of the method.  This must be an Array-Class,
     * such as {@code int[][].class} or (in the case of Boxed-Type Arrays) {@code Integer[][]}.
     * 
     * @param ja Any instance of {@link JsonArray}.  The contents sof this array should match the 
     * dimensionality of the expected Output-Array Type, or an exception will likelyy throw.
     * 
     * @param rec An instance of {@link SettingsRec} that's been configured to return a 
     * multi-dimensional array.
     * 
     * @param retArrClass The class of the return array.
     * @return An instance of {@code 'retArrClass'}
     */
    public static <BASIC_TYPE, STREAM_TYPE, RETURN_ARR_TYPE> RETURN_ARR_TYPE jsonArrayToJava(
            final JsonArray                             ja,
            final SettingsRec<BASIC_TYPE, STREAM_TYPE>  rec,
            final Class<RETURN_ARR_TYPE>                retArrClass
        )
    {
        CHECK_ARRAY_CLASS(retArrClass, rec.CLASS);
        return jsonArrayToJava_INTERNAL(ja, rec, retArrClass);
    }

    @SuppressWarnings("unchecked")
    private static <BASIC_TYPE, STREAM_TYPE, RETURN_ARR_TYPE> RETURN_ARR_TYPE
        jsonArrayToJava_INTERNAL(
            final JsonArray                             ja,
            final SettingsRec<BASIC_TYPE, STREAM_TYPE>  rec,
            final Class<RETURN_ARR_TYPE>                retArrClass
        )
    {
        // If this is requesting a one-dimensional array, get it using the 1D-Generator,
        // and simply return that array.  This requires a cast because there is no way to prove
        // to the Java-Compiler that <T> is equal to any return-value at all.
        //
        // Remember that this is only guaranteed (it works!) because the helper methods are all
        // protected or private, and it has been guaranteed through rigorous testing, and
        // preventing the user from playing with it!

        if (StringParse.countCharacters(retArrClass.getSimpleName(), '[') == 1)
            return (RETURN_ARR_TYPE) rec.array1DGenerator.apply(ja);


        // Otherwise, this is not a single-dimension (1D) array.  Instead, the JsonArray needs
        // to be iterated, and this method called, recursively, on each of the sub-arrays.
        //
        // NOTE: 'compClass' will also be an array, but with one fewer dimensions

        final Class<?>          compClass   = retArrClass.getComponentType();
        final int               SIZE        = ja.size();
        final RETURN_ARR_TYPE   retArr      = (RETURN_ARR_TYPE) Array.newInstance(compClass, SIZE);

        JsonValue jv = null;

        for (int i=0; i < SIZE; i++)

            switch ((jv = ja.get(i)).getValueType())
            {
                // javax.json.JsonValue.ValueType.NULL
                case NULL: Array.set(retArr, i, null); break;

                // javax.json.JsonValue.ValueType.ARRAY (JsonArray)
                case ARRAY:

                    Array.set(
                        retArr,
                        i,
                        JSON_ARRAY_DIMN.jsonArrayToJava((JsonArray) jv, rec, compClass)
                    );

                    break;

                // javax.json.JsonValue.ValueType.TRUE, FALSE, NUMBER, STRING, OBJECT
                default:

                    if (rec.IN_NSAT)        Array.set(retArr, i, null);
                    else if (rec.S_NSAT)    continue;
                    else throw new JsonTypeArrException(ja, i, ARRAY, jv, retArrClass);
            }

        return retArr;
    }



    // ********************************************************************************************
    // ********************************************************************************************
    // One Helper
    // ********************************************************************************************
    // ********************************************************************************************


    /**
     * Check user input, and throws exceptions if the array-class has not been properly
     * chosen.
     * 
     * @param retArrClass This must be a primitive-array class, possibly of multiple dimensions
     * 
     * @param expectedRootClass The expected "root class".  For {@code int[][].class}, the root
     * class would be {@code int.class}.
     * 
     * @return Parameter {@code retArrClass} is the return value of this checker method
     * 
     * @throws IllegalArgumentExcetion If parameter retArrClass:
     * If calling retArrClass.isArray() returns FALSE
     * If the root-array type is not the appropriate type for the method that was called
     */
    protected static <T> Class<T> CHECK_ARRAY_CLASS(
            final Class<T> retArrClass,
            final Class<?> expectedRootClass
        )
    {
        Objects.requireNonNull(retArrClass, "Return-Array Type, Parameter 'retArrClass' is null");

        if (! retArrClass.isArray()) throw new IllegalArgumentException(
            "The class you have passed to parameter 'retArrClass' " +
            "[" + retArrClass.getSimpleName() + "], is not an array"
        );

        Class<?> componentClass = retArrClass.getComponentType();

        while (componentClass.isArray()) componentClass = componentClass.getComponentType();

        if (! expectedRootClass.equals(componentClass)) throw new IllegalArgumentException(
            "The class you have passed to parameter 'retArrClass' " +
            "[" + retArrClass.getSimpleName() + "], is not a(n) " +
            expectedRootClass.getSimpleName() + "-array."
        );

        return retArrClass;
    }
}