001package Torello.Browser.JsonAST;
002
003import Torello.JSON.ReadJSON;
004import Torello.Java.Additional.Ret3;
005
006import Torello.Java.ReadOnly.ReadOnlyList;
007import Torello.Java.ReadOnly.ReadOnlyArrayList;
008import Torello.Java.ReadOnly.ROArrayListBuilder;
009
010import javax.json.JsonObject;
011import javax.json.JsonArray;
012
013/**
014 * Extracts the complete list of {@link TypeNode}, {@link CommandNode} and {@link EventNode} from 
015 * the {@link JsonObject} of a {@link Domain} definition.  This class parsed the {@code JsonArray}
016 * instances inside the JSON which have the names {@code "types", "commands", "events"}.
017 */
018@Torello.JavaDoc.Annotations.StaticFunctional
019@Torello.JavaDoc.Annotations.JDHeaderBackgroundImg(EmbedTagFileID="CONSTRUCTOR_JDHBI")
020public class Helper$GetTCELists 
021{
022    private Helper$GetTCELists() { }
023
024    static Ret3
025    <
026        ReadOnlyList<TypeNode>,
027        ReadOnlyList<CommandNode>,
028        ReadOnlyList<EventNode>
029    >
030        get(final Domain d, final JsonObject jo)
031    {
032        // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
033        // Types, Commands & Events
034        // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
035
036        final ReadOnlyList<TypeNode>    types;
037        final ReadOnlyList<CommandNode> commands;
038        final ReadOnlyList<EventNode>   events;
039
040
041        // ReadJSON.getJsonObject
042        //     (JsonObject jo, String propertyName, boolean isOptional, boolean throwOnNull)
043
044        final JsonArray typesJA     = ReadJSON.getJsonArray(jo, "types",    true, true);
045        final JsonArray commandsJA  = ReadJSON.getJsonArray(jo, "commands", true, true);
046        final JsonArray eventsJA    = ReadJSON.getJsonArray(jo, "events",   true, true);
047
048        final java.util.Comparator<Entity> sorterTCE = (Entity a, Entity b) ->
049            a.name.compareTo(b.name);
050
051
052        // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
053        // TYPES
054        // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
055
056        if (typesJA == null)
057            types = ReadOnlyArrayList.emptyROAL();
058        else
059        {
060            int                             i       = 0;
061            ROArrayListBuilder<TypeNode>    roalb   = new ROArrayListBuilder<>();
062
063            for (JsonObject typeJO : typesJA.getValuesAs(JsonObject.class))
064                roalb.add(new TypeNode(d, typeJO, i++));
065
066            roalb.sort(sorterTCE);
067            types = roalb.build();
068        }
069
070
071        // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
072        // COMMANDS
073        // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
074
075        if (commandsJA == null)
076            commands = ReadOnlyArrayList.emptyROAL();
077        else
078        {
079            int                             i       = 0;
080            ROArrayListBuilder<CommandNode> roalb   = new ROArrayListBuilder<>();
081
082            for (final JsonObject commandJO : commandsJA.getValuesAs(JsonObject.class))
083                roalb.add(new CommandNode(d, commandJO, i++));
084
085            roalb.sort(sorterTCE);
086            commands = roalb.build();
087        }
088
089
090        // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
091        // EVENTS
092        // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
093
094        if (eventsJA == null)
095            events = ReadOnlyArrayList.emptyROAL();
096        else
097        {
098            int                             i       = 0;
099            ROArrayListBuilder<EventNode>   roalb   = new ROArrayListBuilder<>();
100
101            for (final JsonObject eventJO : eventsJA.getValuesAs(JsonObject.class))
102                roalb.add(new EventNode(d, eventJO, i++));
103
104            roalb.sort(sorterTCE);
105            events = roalb.build();
106        }
107
108        return new Ret3<>(types, commands, events);
109    }
110}