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

import Torello.JSON.ReadJSON;
import Torello.JSON.RJArrIntoStream;

import Torello.Java.ReadOnly.ReadOnlyList;
import Torello.Java.ReadOnly.ReadOnlyArrayList;

import javax.json.JsonArray;
import javax.json.JsonObject;

/**
 * This checks for a Json Propety having the name {@code "enum"}.  If such a property does exists,
 * it's contents are extracted and converted into a {@link ReadOnlyList ReadOnlyList<String>}, and 
 * returned to the caller. 
 */
@Torello.JavaDoc.Annotations.StaticFunctional
@Torello.JavaDoc.Annotations.JDHeaderBackgroundImg(EmbedTagFileID="CONSTRUCTOR_JDHBI")
public class Helper$Enum
{
    private Helper$Enum() { }

    static ReadOnlyList<String> getEnumPropertyOrNull(final Entity THIS, final JsonObject jo)
    {
        // getJsonArray(JsonObject jo, String propertyName, boolean isOptional, boolean throwOnNull)
        final JsonArray enumArr = ReadJSON.getJsonArray(jo, "enum", true, true);

        if (enumArr == null) return null;

        final ReadOnlyList<String> enumVals = RJArrIntoStream 
            // strArr(JsonArray ja, String defaultValue, int FLAGS)
            .strArr(enumArr, null, 0)
            .sorted(String::compareTo)
            .collect(ReadOnlyArrayList.streamCollector());

        // All elements in "enum" must be **NON-NULL**
        for (int i = 0; i < enumVals.size(); i++)

            if (enumVals.get(i) == null) THIS.verifyThrow(
                "'enum'[" + i + "] must be a non null JSON string\n" +
                "'enum': " + enumVals.toString()
            );


        // Inside THIS Super-Class (Entity), the top-level 'type' Property WILL ONLY BE ANALYZED if
        // there is a JsonArray present which has name "enum".  The extraction of "type" is only
        // used for assertion-checks / error-checks
        // 
        // When "enum" exists, a "type" property must also exist within 'jo'.
        // Furthermore, rhe *ONLY* legitimate value it may have is the String-Literal "string"
        // 
        // Say that 3 times fast: A "JSON String Literal" whose only permitted value is the ASCII
        // sequence: "string"
        // 
        // ReadJSON Method:
        // getString(JsonObject jo, String propertyName, boolean isOptional, boolean throwOnNull)

        final String typeProp = ReadJSON.getString(jo, "type", false, true);

        if (! typeProp.equals("string")) THIS.verifyThrow(
            "The Entity being built has an 'enum' property, but its 'type' was not \"string\". " +
            "type: " + typeProp + "\n" +
            "'enum': " + enumVals.toString()
        );

        return enumVals;
    }
}