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

import static Torello.Java.C.BYELLOW;
import static Torello.Java.C.RESET;

import Torello.JavaDoc.Annotations.LinkJavaSource;
import Torello.JavaDoc.Annotations.JDHeaderBackgroundImg;

import Torello.Java.StorageWriter;
import Torello.Java.LFEC;

import Torello.Java.ReadOnly.ReadOnlyList;
import Torello.Java.ReadOnly.ROArrayListBuilder;

import javax.json.Json;
import javax.json.JsonObject;

import java.io.StringReader;
import java.util.Iterator;


/**
 * Contains a {@link ReadOnlyList} that has each domain defined in the API.  There are exactly two
 * API's that Google has created in its CDP (Chrome DevTools Protocol) System: the "Browser API",
 * and the "JavaScript API".
 * 
 * <EMBED CLASS='external-html' DATA-FILE-ID=AST_TREES>
 */
@JDHeaderBackgroundImg(EmbedTagFileID="AST_NODES_JDHBI")
public class API implements java.io.Serializable, Comparable<API>, Iterable<Domain>
{
    // ********************************************************************************************
    // ********************************************************************************************
    // STATIC FINAL FIELDS
    // ********************************************************************************************
    // ********************************************************************************************


    /** <EMBED CLASS='external-html' DATA-FILE-ID=SVUID> */
    protected static final long serialVersionUID = 1;


    // ********************************************************************************************
    // ********************************************************************************************
    // Constant & Final Instance Fields
    // ********************************************************************************************
    // ********************************************************************************************


    public final int id = IDManager.nextID();

    /** Contains the Json Specifiction File's name */
    public final String inFileName;

    /** This will either contain the {@code String "BrowserAPI"} or the {@code "JavaScriptAPI"}  */
    public final String name;

    /** The complete list of domain's in the API, extracted &amp; parsed from the Json File. */
    public final ReadOnlyList<Domain> domains;


    // ********************************************************************************************
    // ********************************************************************************************
    // Accessors:  There is No Linking, No Simplifying ==> therefore no Non-Constant Fields
    // ********************************************************************************************
    // ********************************************************************************************


    /**
     * This method merely iterates the list of domain's contained by this API, to find and return 
     * the domain whose name matches {@code 'domainName'}.
     * 
     * @param domainName the name of one of the domain's in this API, as a {@code java.lang.String}
     * 
     * @return The domain whose {@link Entity Domain.name} matches {@code 'domainName'}, or null if
     * no such domain exists.
     */
    public Domain findDomain(String domainName)
    {
        for (Domain domain : domains) if (domain.name.equals(domainName)) return domain;
        return null;
    }


    // ********************************************************************************************
    // ********************************************************************************************
    // Package Private Constructor
    // ********************************************************************************************
    // ********************************************************************************************


    // This parses a JSON Object File for the Browser-API and the Java-Script API
    API(
            final StorageWriter sw,
            final String        inFileName,
            final String        apiName
        )
    {
        this.inFileName = inFileName;
        this.name       = apiName;
        
        sw.println("Reading File: " + BYELLOW + inFileName + RESET);

        final String                        apiFile = LFEC.loadFile(inFileName);
        final StringReader                  sr      = new StringReader(apiFile);
        final JsonObject                    jo      = Json.createReader(sr).readObject();
        final ROArrayListBuilder<Domain>    roalb   = new ROArrayListBuilder<>();

        final Iterator<JsonObject> iter = jo
            .getJsonArray("domains")
            .getValuesAs(JsonObject.class)
            .iterator();

        while (iter.hasNext()) roalb.add(new Domain(this, iter.next()));

        roalb.sort((Domain d1, Domain d2) -> d1.name.compareTo(d2.name));

        this.domains = roalb.build();
    }


    // ********************************************************************************************
    // ********************************************************************************************
    // Comparable, java.lang.Object
    // ********************************************************************************************
    // ********************************************************************************************


    /**
     * Returns an {@code Iterator} that iterates the contents of the {@link #domains} list.
     * 
     * @see #domains
     * @see Torello.Java.Additional.RemoveUnsupportedIterator
     */
    public Iterator<Domain> iterator()
    { return this.domains.iterator(); }

    /**
     * Checks {@link #name} fields, using the standard {@code String.compareTo} to produce a
     * comparison value.
     */
    public int compareTo(final API other)
    {
        if (this == other) return 0;
        if (other == null) return 1;

        return this.name.compareTo(other.name);
    }

    /** Checks {@link #id this.id} against {@code other.id} for equality. */
    public boolean equals(final Object other)
    {
        if (other == null)              return false;
        if (! (other instanceof API))   return false;
        return this.id == ((API) other).id;
    }

    /** Utilizes {@link #id this.id} as a hash; it is a unique value across the AST Tree. */
    public int hashCode()
    { return id; }

    /** Generates a full {@code String} representation of {@code this} instance. */
    @LinkJavaSource(handle="StringAPI")
    public String toString()
    { return StringAPI.get(this); }
}