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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
package Torello.JavaDoc;

import Torello.Java.ReadOnly.ReadOnlyMap;
import Torello.Java.ReadOnly.ROTreeMapBuilder;
import Torello.Java.ReadOnly.ReadOnlyTreeMap;

import Torello.JDUInternal.Features.STATS.StatsInternal;

import java.util.Map;

/**
 * Maintains a suite of statistics about all Java project-wide source-code files.
 * 
 * <BR />As the Upgrade Processors are executed, this class maintains a few statistics about the
 * build, and produces the {@code Stats} HTML instance, which is subsequently linked to a
 * {@code 'Stats'} button on output Java Doc Web-Pages - <I>and also returned to the user after
 * calling the ugrader</I>.
 * 
 * <BR /><BR /><EMBED CLASS='external-html' DATA-FILE-ID=UPSTATS>
 */
public class Stats implements java.io.Serializable
{
    // ********************************************************************************************
    // ********************************************************************************************
    // Static-Constants
    // ********************************************************************************************
    // ********************************************************************************************

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


    // This is empty, and since it is read-pnly, the Generic-Parameters do not matter in any way
    // shape or form.

    @SuppressWarnings("rawtypes")
    private static final ReadOnlyMap EMPTY_MAP = ReadOnlyMap.of();


    // ********************************************************************************************
    // ********************************************************************************************
    // An Instance-Constant
    // ********************************************************************************************
    // ********************************************************************************************


    // Package-Level instances of 'Stats' will have their package-names saved here.
    // For the "Complete-Project" Stats instance, this will be null.

    public final String packageName;


    // ********************************************************************************************
    // ********************************************************************************************
    // ReadOnlyMap's
    // ********************************************************************************************
    // ********************************************************************************************


    // Each Package has it's own / recursive instance of 'Stats'
    // For the Package-Level instances of 'Stats' - this field will be null.

    public final ReadOnlyMap<String, Stats> packageStatsMap;

    // A Pointer to the "Project-Global Embed-Tags Map"
    public final ReadOnlyMap<String, String> globalTagsMap;

    // A Count-Total for the "Project-Global Embed-Tags Map"
    public final ReadOnlyMap<String, Integer> globalTagsCount;


    // A reference pointer to the "Package-Local Embed-Tag Map", (ID's ==> FileNames)
    // NOTE: For the top-level main 'Stats' instance, this will remain null

    public final ReadOnlyMap<String, String> packageTagsMap;


    // Keeps a count of the use of all "Package-Local Embed-Tags"
    // NOTE: For the top-level main 'Stats' instance, this will remain null

    public final ReadOnlyMap<String, Integer> packageTagsCount;


    // ********************************************************************************************
    // ********************************************************************************************
    // Stats Fields: BASIC
    // ********************************************************************************************
    // ********************************************************************************************


    /** Maintains A count of the total number of lines of {@code '.java'} files. */
    public final int numLines;

    /** Maintains A count of the total number of bytes of {@code '.java'} files. */
    public final int numBytes;

    /** Maintains A count of the total number of HiLited HTML {@code <DIV>} Elements. */
    public final int numHiLitedDivs;


    // ********************************************************************************************
    // ********************************************************************************************
    // Stats: Entity (METHOD, FIELD, CONSTRUCTOR, ANNOTATION_ELEM, ENUM_CONSTANT)
    // ********************************************************************************************
    // ********************************************************************************************


    /** Maintains A count of the total number of methods found during the upgrade. */
    public final int numMethods;

    /** Maintains A count of the total number of constructors found during the upgrade. */
    public final int numConstructors;

    /** Maintains A count of the total number of fields found during the upgrade. */
    public final int numFields;

    /** Maintains A count of the total number of annotation-elements found during the upgrade. */
    public final int numAnnotationElems;

    /** Maintains A count of the total number of enum-constants found during the upgrade. */
    public final int numEnumConstants;


    // ********************************************************************************************
    // ********************************************************************************************
    // Stats: HILITED-Entity (METHOD, FIELD, CONSTRUCTOR, ANNOTATION_ELEM, ENUM_CONSTANT)
    // ********************************************************************************************
    // ********************************************************************************************


    /** Maintains A count of the total number of method bodies hilited by the upgrader. */
    public final int numHiLitedMethods;

    /** Maintains A count of the total number of constructor bodies hilited by the upgrader. */
    public final int numHiLitedConstructors;

    /** Maintains A count of the total number of field declarations hilited by the upgrader. */
    public final int numHiLitedFields;

    /** A count of the total number of annotation-element declarations hilited by the upgrader. */
    public final int numHiLitedAnnotationElems;

    /** A count of the total number of enumeration-constant declarations hilited by the upgrader.*/
    public final int numHiLitedEnumConstants;


    // ********************************************************************************************
    // ********************************************************************************************
    // Stats: DOCUMENTED-Entity (METHOD, FIELD, CONSTRUCTOR, ANNOTATION_ELEM, ENUM_CONSTANT)
    // ********************************************************************************************
    // ********************************************************************************************


    /** Maintains A count of the total number of methods that were documented by Java Doc. */
    public final int numDocumentedMethods;

    /** Maintains A count of the total number of constructors that were documented by Java Doc. */
    public final int numDocumentedConstructors;

    /** Maintains A count of the total number of fields that were documented by Java Doc. */
    public final int numDocumentedFields;

    /** A count of the total number of annotation-elements that were documented by Java Doc. */
    public final int numDocumentedAnnotationElems;

    /** A count of the total number of annotation-elements that were documented by Java Doc. */
    public final int numDocumentedEnumConstants;


    // ********************************************************************************************
    // ********************************************************************************************
    // Stats: STATIC-Entity (METHOD, FIELD)
    // ********************************************************************************************
    // ********************************************************************************************


    /** Maintains A count of the total number of {@code static} methods found during the upgrade. */
    public final int numStaticMethods;

    /** Maintains A count of the total number of {@code static} fields found during the upgrade. */
    public final int numStaticFields;


    // ********************************************************************************************
    // ********************************************************************************************
    // Stats: FINAL-Entity (METHOD, FIELD, CONSTRUCTOR)
    // ********************************************************************************************
    // ********************************************************************************************


    /** Maintains A count of the total number of {@code final} methods found by the upgrade. */
    public final int numFinalMethods;

    /** Maintains A count of the total number of {@code final} constructors found by the upgrader. */
    public final int numFinalConstructors;

    /** Maintains A count of the total number of {@code final} fields found by the upgrader. */
    public final int numFinalFields;


    // ********************************************************************************************
    // ********************************************************************************************
    // Stats: PUBLIC-Entity (METHOD, FIELD, CONSTRUCTOR)
    // ********************************************************************************************
    // ********************************************************************************************


    /** Maintains A count of the total number of {@code public} methods found by the upgrader. */
    public final int numPublicMethods;

    /** Maintains A count of the total number of {@code public} constructors found by the upgrader.*/
    public final int numPublicConstructors;

    /** Maintains A count of the total number of {@code public} fields found by the upgrader. */
    public final int numPublicFields;


    // ********************************************************************************************
    // ********************************************************************************************
    // Stats: PROTECTED-Entity (METHOD, FIELD, CONSTRUCTOR)
    // ********************************************************************************************
    // ********************************************************************************************


    /** Maintains The total number of {@code protected} methods found during the upgrade. */
    public final int numProtectedMethods;

    /** Maintains The total number of {@code protected} constructors found during the upgrade. */
    public final int numProtectedConstructors;

    /** Maintains The total number of {@code protected} fields found during the upgrade. */
    public final int numProtectedFields;


    // ********************************************************************************************
    // ********************************************************************************************
    // Stats: PRIVATE-Entity (METHOD, FIELD, CONSTRUCTOR)
    // ********************************************************************************************
    // ********************************************************************************************


    /** Maintains The total number of {@code private} methods found during the upgrade. */
    public final int numPrivateMethods;

    /** Maintains The total number of {@code private} constructors found during the upgrade. */
    public final int numPrivateConstructors;

    /** Maintains The total number of {@code private} fields found during the upgrade. */
    public final int numPrivateFields;


    // ********************************************************************************************
    // ********************************************************************************************
    // Stats: OTHER-Entity (FIELD only)
    // ********************************************************************************************
    // ********************************************************************************************


    /** Maintains A count of the total number of {@code transient} fields found during the upgrade.*/
    public final int numTransientFields;

    /** Maintains A count of the total number of {@code volatile} fields found during the upgrade. */
    public final int numVolatileFields;


    // ********************************************************************************************
    // ********************************************************************************************
    // Constructor
    // ********************************************************************************************
    // ********************************************************************************************


    // At the very end of this constructor, there is an "EMPTY_MAP" assignment that uses a cast
    @SuppressWarnings("unchecked")

    Stats(
            final StatsInternal                 si,
            final ReadOnlyMap<String, Integer>  globalTagsCount
        )
    {
        // The Package for which 'this' Stats Instance is being constructed.
        // Note that if 'this' instance is the "Global" Stats-Instance, this field will be null !!

        this.packageName = si.packageName;


        // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
        // The Global <EMBED>-Tag Statistics Map (Tag-Name to FileName **AND** Tag-Name to Count)
        // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
        //
        // IMPORTANT:   In class "StatsInternal", the 'globalTagsCount' instance is already a
        //              "ReadOnlyMap".  This is because the Tags-Map just maps Tag-Names to
        //              File-Names, which means that this Map can be built before the JDU is even
        //              run.
        //
        // Since this map can be built inside the StatsInternal-Constructor, it can be stored as a
        // "ReadOnlyMap", rather than a java.util.Map;
        // 
        // Because StatsInternal.globalTagsMap is a ReadOnlyMap<String, String>, we can just copy
        // the reference right here.
        //
        // MAP-KEY:     <EMBED> Tag-Name
        // MAP-VALUE:   '.html' File-Name containing the external '.html'


        this.globalTagsMap = si.globalTagsMap;


        // This Map contains a count of <EMBED>-Tag names, to the number of times they are used.
        // Because these counts are computed as the JDU is executed, this must be a Read/Write 
        // java.util.Map.  Therefore, when we get here, it must be converted to a ReadOnlyMap.  
        //
        // The reference "si.globalTagsCount" cannot be passed to the end-user.  For things that do
        // not need to change, it is preferrable to give the end user a READ-ONLY instance, rather
        // than an instance out of java.util.
        //
        // Most importantly, there should be only ONE INSTANCE of the "Global Tags Count" that is
        // built PER PROJECT.  After one has been constructed, the same exact reference should be
        // passed to all of the individual, package-level, Stats instances.
        //
        // MAP-KEY:     <EMBED> Tag-Name
        // MAP-VALUE:   Number of times that Tag was used (across all packages)

        this.globalTagsCount = (globalTagsCount == null)
            ? new ReadOnlyTreeMap<>(si.globalTagsCount)
            : globalTagsCount;


        // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
        // Package-Level <EMBED>-Tag Statistics Map/Stuff
        // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
        // 
        // These may be null if 'this' is actually the global-instance.

        this.packageTagsMap = ((si.packageTagsMap != null) && (si.packageTagsMap.size() > 0))
            ? si.packageTagsMap
            : (ReadOnlyMap<String, String>) EMPTY_MAP;

        this.packageTagsCount =((si.packageTagsCount != null) && (si.packageTagsCount.size() > 0))
            ? new ReadOnlyTreeMap<>(si.packageTagsCount)
            : (ReadOnlyMap<String, Integer>) EMPTY_MAP;


        // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
        // The Statistics Fields
        // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***

        this.numLines                       = si.numLines;
        this.numBytes                       = si.numBytes;
        this.numHiLitedDivs                 = si.numHiLitedDivs;
    
        this.numMethods                     = si.numMethods;
        this.numConstructors                = si.numConstructors;
        this.numFields                      = si.numFields;
        this.numAnnotationElems             = si.numAnnotationElems;
        this.numEnumConstants               = si.numEnumConstants;
    
        this.numHiLitedMethods              = si.numHiLitedMethods;
        this.numHiLitedConstructors         = si.numHiLitedConstructors;
        this.numHiLitedFields               = si.numHiLitedFields;
        this.numHiLitedAnnotationElems      = si.numHiLitedAnnotationElems;
        this.numHiLitedEnumConstants        = si.numHiLitedEnumConstants;
    
        this.numDocumentedMethods           = si.numDocumentedMethods;
        this.numDocumentedConstructors      = si.numDocumentedConstructors;
        this.numDocumentedFields            = si.numDocumentedFields;
        this.numDocumentedAnnotationElems   = si.numDocumentedAnnotationElems;
        this.numDocumentedEnumConstants     = si.numDocumentedEnumConstants;
    
        this.numStaticMethods               = si.numStaticMethods;
        this.numStaticFields                = si.numStaticFields;

        this.numFinalMethods                = si.numFinalMethods;
        this.numFinalConstructors           = si.numFinalConstructors;
        this.numFinalFields                 = si.numFinalFields;
    
        this.numPublicMethods               = si.numPublicMethods;
        this.numPublicConstructors          = si.numPublicConstructors;
        this.numPublicFields                = si.numPublicFields;

        this.numProtectedMethods            = si.numProtectedMethods;
        this.numProtectedConstructors       = si.numProtectedConstructors;
        this.numProtectedFields             = si.numProtectedFields;
    
        this.numPrivateMethods              = si.numPrivateMethods;
        this.numPrivateConstructors         = si.numPrivateConstructors;
        this.numPrivateFields               = si.numPrivateFields;

        this.numTransientFields             = si.numTransientFields;
        this.numVolatileFields              = si.numVolatileFields;


        // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
        // The Package Stats-Tree Instances
        // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
        // 
        // This Field shall contain a map of "Package-Name" to "Package Stats-Instances"

        if (si.packageStatsMap == null)
        {
            this.packageStatsMap = null;
            return;
        }

        // public final Map<String, Stats> packageStatsMap;
        ROTreeMapBuilder<String, Stats> rotmb = new ROTreeMapBuilder<>();

        for (String key : si.packageStatsMap.keySet())
        {
            final StatsInternal siInner     = si.packageStatsMap.get(key);
            final Stats         statsInner  = new Stats(siInner, this.globalTagsCount);

            rotmb.put(key, statsInner);
        }

        this.packageStatsMap = rotmb.build();
    }


    // ********************************************************************************************
    // ********************************************************************************************
    // To String
    // ********************************************************************************************
    // ********************************************************************************************


    /**
     * Generates a {@code String} that enapsulates all of the counters / running-totals inside
     * this data statistic class.
     * 
     * @return A {@code String} representation of this class.  Only includes statistics about
     * use of methods, constructors, fields etc...  <B>DOES NOT INCLUDE</B> statistics regarding
     * the use of the {@code <EMBED CLASS='external-html' ...>} tags.
     */
    public String toString()
    { return Torello.JDUInternal.Features.STATS.S_ToString.get(this); }
}