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
package Torello.HTML.Tools.Images;

import Torello.Java.*;
import Torello.JavaDoc.LinkJavaSource;
// Needed for a JavaDoc Comment {@link ...}
import Torello.HTML.TagNode;

import java.net.URL;
import java.io.Serializable;
import java.util.Arrays;

/**
 * After downloading all of the user's requested images, the class {@link ImageScraper} returns an
 * instance of this class.
 * 
 * <EMBED CLASS='external-html' DATA-FILE-ID=RESULTS>
 * @see ImageScraper
 */
@Torello.JavaDoc.JDHeaderBackgroundImg(EmbedTagFileID="IMAGE_SCRAPER_CLASS")
public class Results implements Serializable, Cloneable
{
    /** <EMBED CLASS='external-html' DATA-FILE-ID=SVUID> */
    public static final long serialVersionUID = 1;


    // ********************************************************************************************
    // ********************************************************************************************
    // Public Array Fields: User may inspect these fields when an instance of 'Results' is returned
    // ********************************************************************************************
    // ********************************************************************************************


    /**
     * <EMBED CLASS='external-html' DATA-FILE-ID=RES_urls>
     * <EMBED CLASS='external-html' DATA-FILE-ID=RES_PARALLEL_NOTE>
     */
    public final URL[] urls;

    /**
     * <EMBED CLASS='external-html' DATA-FILE-ID=RES_b64EncodedImg>
     * <EMBED CLASS='external-html' DATA-FILE-ID=RES_PARALLEL_NOTE>
     */    
    public final boolean[] b64EncodedImg;

    /**
     * <EMBED CLASS='external-html' DATA-FILE-ID=RES_skipped>
     * <EMBED CLASS='external-html' DATA-FILE-ID=RES_PARALLEL_NOTE>
     */
    public final boolean[] skipped;

    /**
     * <EMBED CLASS='external-html' DATA-FILE-ID=RES_fileNames>
     * <EMBED CLASS='external-html' DATA-FILE-ID=RES_PARALLEL_NOTE>
     */
    public final String[] fileNames;

    /**
     * <EMBED CLASS='external-html' DATA-FILE-ID=RES_saveDirectories>
     * <EMBED CLASS='external-html' DATA-FILE-ID=RES_PARALLEL_NOTE>
     */
    public final String[] saveDirectories;

    /**
     * <EMBED CLASS='external-html' DATA-FILE-ID=RES_imageFormats>
     * <EMBED CLASS='external-html' DATA-DEFVAL=null DATA-SPEC="image-format"
     *      DATA-FILE-ID=RES_SKIPPED_NOTE>
     * 
     * <EMBED CLASS='external-html' DATA-FILE-ID=RES_PARALLEL_NOTE>
     */
    public final IF[] imageFormats;

    /**
     * <EMBED CLASS='external-html' DATA-FILE-ID=RES_exceptions>
     * <EMBED CLASS='external-html' DATA-FILE-ID=RES_PARALLEL_NOTE>
     */
    public final Exception[] exceptions;

    /**
     * <EMBED CLASS='external-html' DATA-FILE-ID=RES_sizes>
     * <EMBED CLASS='external-html' DATA-DEFVAL="-1" DATA-SPEC=size DATA-FILE-ID=RES_SKIPPED_NOTE>
     * <EMBED CLASS='external-html' DATA-FILE-ID=RES_PARALLEL_NOTE>
     */
    public final long[] sizes;

    /**
     * <EMBED CLASS='external-html' DATA-FILE-ID=RES_widths>
     * <EMBED CLASS='external-html' DATA-DEFVAL="-1" DATA-SPEC=width DATA-FILE-ID=RES_SKIPPED_NOTE>
     * <EMBED CLASS='external-html' DATA-FILE-ID=RES_PARALLEL_NOTE>
     */
    public final int[] widths;

    /**
     * <EMBED CLASS='external-html' DATA-FILE-ID=RES_heights>
     * <EMBED CLASS='external-html' DATA-DEFVAL="-1" DATA-SPEC=height
     *      DATA-FILE-ID=RES_SKIPPED_NOTE>
     * <EMBED CLASS='external-html' DATA-FILE-ID=RES_PARALLEL_NOTE>
     */
    public final int[] heights;


    // ********************************************************************************************
    // ********************************************************************************************
    // Some Package-Private Fields, Used here and by class ImageScraper
    // ********************************************************************************************
    // ********************************************************************************************


    // next result received array position.
    int pos = 0;

    // number of successfully saved images.
    int successCounter = 0;


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


    Results(int size)
    {
        // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
        // Create each of these arrays
        // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***

        urls                = new URL[size];
        b64EncodedImg       = new boolean[size];
        skipped             = new boolean[size];
        fileNames           = new String[size];
        saveDirectories     = new String[size];
        imageFormats        = new IF[size];
        exceptions          = new Exception[size];
        sizes               = new long[size];
        widths              = new int[size];
        heights             = new int[size];


        // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
        // Initialize each element of the above arrays
        // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***

        for (int i=0; i < size; i++)
        {
            urls[i]             = null;
            b64EncodedImg[i]    = false;
            skipped[i]          = false;
            fileNames[i]        = null;
            saveDirectories[i]  = null;
            imageFormats[i]     = null;
            exceptions[i]       = null;
            sizes[i]            = -1;
            widths[i]           = -1;
            heights[i]          = -1;
        }
    }


    // ********************************************************************************************
    // ********************************************************************************************
    // No Image Downloaded
    // ********************************************************************************************
    // ********************************************************************************************


    // Request static-builder generated an "Exception URL"
    // Called From: ImageScraper.loopBody(RECORD)

    void tagNodeSRCError(Exception e)
    {
        skipped[pos]    = true;
        exceptions[pos] = e;

        pos++;
    }


    // User-Provided "Predicate<URL> skipURL"
    // Called From: ImageScraper.downloadImage(RECORD)

    void skippedURL(URL url)
    {
        urls[pos]       = url;
        skipped[pos]    = true;

        pos++;
    }


    // User-Provided "boolean skipBase64EncodedImages"
    // Called From: ImageScraper.convertB64Image(RECORD)

    void skipB64()
    {
        b64EncodedImg[pos]  = true;
        skipped[pos]        = true;

        pos++;
    }


    // Called from many places.  This method is the biggest of the Results-Reporters 
    //   * Exception-thrown
    //   * 'ImageInfo' instance hasn't been constructed yet

    void exceptionFail(URL url, Exception e)
    {
        urls[pos]           = url;
        b64EncodedImg[pos]  = (url == null);
        skipped[pos]        = true;
        exceptions[pos]     = e;

        pos++;
    }


    // ********************************************************************************************
    // ********************************************************************************************
    // "ImageInfo" instance available now: Image Successfully Downloaded and Converted to Array.
    // ********************************************************************************************
    // ********************************************************************************************


    // There are 3 different User-Provided Lambda-Target's that might throw exceptions
    // This is called from within "ImageScraper.RECORD.userLambdaEx(...)"

    void userLambdaException(ImageInfo imageInfo, Exception e)
    {
        skipped[pos]    = true;
        exceptions[pos] = e;

        copyImageInfo(imageInfo);
    }


    // The User Keep/Reject Predicate rejected this image
    // Called From: ImageScraper.handleImageByteArray(RECORD)

    void predicateReject(ImageInfo imageInfo)
    {
        skipped[pos] = true;
        copyImageInfo(imageInfo);
    }


    // Image was written to disk somewhere, or accepted by the Request.imageReceiver
    // Called From: ImageScraper.writeOrTransmit(RECORD)

    void success(ImageInfo imageInfo, String targetDirectory)
    {
        // Directory where the image was saved, if called by "ImageReceiver", this will be null
        saveDirectories[pos] = targetDirectory;

        copyImageInfo(imageInfo);

        // Only time this is ever incremented
        successCounter++;
    }


    // ********************************************************************************************
    // ********************************************************************************************
    // SMALL HELPER
    // ********************************************************************************************
    // ********************************************************************************************


    // Private Method, used in all 5 previous methods directly above here
    private void copyImageInfo(ImageInfo imageInfo)
    {
        urls[pos]           = imageInfo.url;
        b64EncodedImg[pos]  = imageInfo.isB64EncodedImage;
        imageFormats[pos]   = imageInfo.actualExtension;
        sizes[pos]          = imageInfo.imgByteArr.length;
        widths[pos]         = imageInfo.width;
        heights[pos]        = imageInfo.height;
        fileNames[pos]      = imageInfo.fileName() + '.' + imageInfo.actualExtension;

        pos++;
    }


    // ********************************************************************************************
    // ********************************************************************************************
    // interface java.lang.Cloneable
    // ********************************************************************************************
    // ********************************************************************************************


    /**
     * Generates a <B STYLE='color: red;'>Deep Copy</B> of {@code 'this'} instance.  This means
     * all internal arrays are also cloned / copied
     * 
     * @return A duplicate instance of this class, with all arrays having been copied.
     */
    public Results clone()
    { return new Results(this); }

    // Private Constructor, used only for the 'clone()' method
    private Results(Results r)
    {
        this.urls               = r.urls.clone();
        this.b64EncodedImg      = r.b64EncodedImg.clone();
        this.skipped            = r.skipped.clone();
        this.fileNames          = r.fileNames.clone();
        this.saveDirectories    = r.saveDirectories.clone();
        this.imageFormats       = r.imageFormats.clone();
        this.exceptions         = r.exceptions.clone();
        this.sizes              = r.sizes.clone();
        this.widths             = r.widths.clone();
        this.heights            = r.heights.clone();

        this.pos                = r.pos;
        this.successCounter     = r.successCounter;
    }


    // ********************************************************************************************
    // ********************************************************************************************
    // java.lang.Object
    // ********************************************************************************************
    // ********************************************************************************************


    /**
     * Checks whether {@code 'this'} instance is equal to another instance of class
     * {@code Results}.  This method performs a <B STYLE='color: red;'>Deep Equals</B> comparison
     * using the {@code equals(...)} method suite found in class' {@code java.util.Arrays}.
     * 
     * @param other This may be any Java Object, but only an instance class {@code 'Results'} has
     * any chance of being marked as <B STYLE='color: red;'>equal</B> to this instance.
     * 
     * @return {@code TRUE} if and only if {@code 'o'} has a type that's assignable to
     * {@code Results} - and if each of the internal arrays in this instance are equal to the
     * arrays in parameter {@code 'o'}.
     */
    @LinkJavaSource(handle="ResultsEquals")
    public boolean equals(Object other)
    { return ResultsEquals.equals(this, other); }

    /**
     * Returns a {@code java.lang.String} representation of {@code 'this'} instance
     * @return A Java {@code String} containing the data inside this class.
     */
    @LinkJavaSource(handle="ResultsToString")
    public String toString()
    { return ResultsToString.run(this); }

    /**
     * Java's hash-code requirement.  The code is computed by summing the first 10 {@link #sizes}
     * array elements.
     * 
     * @return A hash-code that may be used when storing this node in a java sorted-collection.
     */
    public int hashCode()
    {
       int sum = 0;

       for (int i=0; (i < 10) && (i < sizes.length); i++) sum += sizes[i];

       return sum;
    }
}