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
package Torello.Java.Additional;

import java.util.*;
import java.util.stream.*;
import java.util.function.Function;

import Torello.Java.FileNode;
import Torello.Java.FileNode.RetTypeChoice;

/**
 * Implementation of the <CODE>FileNode</CODE> class <CODE>'RetTypeChoice'</CODE>.
 * 
 * <EMBED CLASS="external-html" DATA-FILE-ID=VLB_NOTE>
 * <EMBED CLASS="external-html" DATA-FILE-ID=VLB_APPLY_SORT>
 * 
 * @param <ORIGINAL> This is the original 'type' of the container's contents, <I>before those
 * contents are transformed</I>.  In the case of {@link FileNode} and {@link RetTypeChoice}, this
 * shall always be type {@code FileNode}.
 * 
 * <BR /><BR /><B STYLE='color: red;'>IMPORTANT:</B> The {@code Sort} is performed
 * <B><I>after</I></B> the {@code Apply} transformation.
 * 
 * @param <TRANSFORMED> This is the 'type' of the contents of the container that is ultimately
 * returned to the user by this {@code 'VarList'} selection-choice.  Note that the salient point
 * is <I>the original container's content's type is transformed, and that the returned container's
 * content's type will not be the same as the input-type, after the transformation</I>
 * 
 * <BR /><BR />In the case of {@link FileNode} and {@link RetTypeChoice}, the usage of this builder
 * class is, sort-of, "a cute way of saying" {@link FileNode#toString()} and / or
 * {@link FileNode#getFullPathName()}) - <I>depending upon whether the user is requesting one of 
 * the <B>{@code 'FILENAME'}</B> variants, or one of the <B>{@code 'FULLPATH'}</B> variants</I>
 * 
 * <BR /><BR /><B>NOTE:</B>In the above two mentioned cases, type {@code <TRANSFORMED>} type will
 * be {@code java.lang.String}.  (While the {@code <ORIGINAL>} type is {@code 'FileNode'}).
 */
@SuppressWarnings("unchecked")
public class VarListBuilderWithApplyAndSort<ORIGINAL, TRANSFORMED>
{
    /**
     * This {@code VarList} instance has a {@code 'retrieve()'} method that will return a
     * {@code Vector} whose {@code Vector}-contents are of parametrized-type {@code 'TRANs'}.
     * The {@code Vector} returned will have already been sorted upon return.
     */
    public final VarList<Vector<TRANSFORMED>, ORIGINAL> VECTOR;

    /**
     * This {@code VarList} instance has a {@code 'retrieve()'} method that will return an
     * {@code ArrayList} whose {@code ArrayList}-contents are of parametrized-type
     * {@code 'TRANSFORMED'}.  The {@code ArrayList} returned will have already been sorted upon
     * return.
     */
    public final VarList<ArrayList<TRANSFORMED>, ORIGINAL> ARRAYLIST;

    /**
     * This {@code VarList} instance has a {@code 'retrieve()'} method that will return an
     * {@code Iterator} whose {@code Iterator}-contents are of parametrized-type
     * {@code 'TRANSFORMED'}.  The {@code Iterator} returned will have already been sorted upon
     * return.
     */
    public final VarList<Iterator<TRANSFORMED>, ORIGINAL> ITERATOR;

    /**
     * This {@code VarList} instance has a {@code 'retrieve()'} method that will return an
     * {@code array} whose {@code array}-contents are of parametrized-type {@code 'TRANSFORMED'}.
     * The {@code array} returned will have already been sorted upon return.
     */
    public final VarList<TRANSFORMED[], ORIGINAL> ARRAY;

    /**
     * This {@code VarList} instance has a {@code 'retrieve()'} method that will return a
     * {@code Stream} whose {@code Stream}-contents are of parametrized-type {@code 'TRANSFORMED'}.
     * The {@code Stream} returned will have already been sorted upon return.
     */
    public final VarList<Stream<TRANSFORMED>, ORIGINAL> STREAM;

    /**
     * Instances of {@code Stream.Builder} cannot be sorted.  Only a built {@code Stream}
     * may be sorted.  Therefore this parameter shall always be null.
     */
    public final VarList<Stream.Builder<TRANSFORMED>, ORIGINAL> STREAM_BUILDER = null;

    /**
     * This {@code VarList} instance has a {@code 'retrieve()'} method that will return a
     * {@code LinkedList} whose {@code LinkedList}-contents are of parametrized-type
     * {@code 'TRANSFORMED'}.  The {@code LinkedList} returned will have already been sorted upon
     * return.
     */
    public final VarList<LinkedList<TRANSFORMED>, ORIGINAL> LINKEDLIST;

    /**
     * This {@code VarList} instance has a {@code 'retrieve()'} method that will return a
     * {@code TreeSet} whose {@code TreeSet}-contents are of parametrized-type
     * {@code 'TRANSFORMED'}.  The {@code TreeSet} returned will have already been sorted upon
     * return.
     */
    public final VarList<TreeSet<TRANSFORMED>, ORIGINAL> TREESET;

    /**
     * This {@code VarList} instance has a {@code 'retrieve()'} method that will return a
     * {@code Stack} whose {@code Stack}-contents are of parametrized-type {@code 'TRANSFORMED'}.
     * The {@code Stack} returned will have already been sorted upon return.
     */
    public final VarList<Stack<TRANSFORMED>, ORIGINAL> STACK;


    /**
     * Builds all 8 of the {@code VarList's} provided by this builder class.
     *
     * @param transformer This {@code transform} is used to convert the contents of the returned
     * list-type.  As noticed by its type, {@code 'Function<ORIGINAL, TRANSFORMED>'}, the final
     * type of the returned {@code VarList} will of type {@code 'TRANSFORMED'}, although while
     * building this {@code 'VarList'}, the elements that shall be inserted will be of type
     * {@code 'ORIGINAL'}.
     *
     * <BR /><BR />It is the job of the transformer parameter to convert the elements from type
     * {@code 'ORIGINAL'} to type {@code 'TRANSFORMED'}
     *
     * @param comparator This is used to sort the returned list.  Notice that this
     * {@code 'comparator'} shall sort list-elements of type {@code 'TRANSFORMED'}.  The list that
     * is ultimately returned by these {@code VarList's} will be of type {@code 'TRANSFORMED'}.
     *
     * <BR /><BR /><B><SPAN STYLE="color: red;">NOTICE</SPAN></B> The {@code 'comparator'} is of
     * parametrized-type {@code 'TRANSFORMED'}.  This is because the sort shall occur <B>AFTER</B> the
     * transform.
     *
     * @param outputClass The final list that is built will be of type {@code class U}.  Because
     * Java generics use "Type Erasure," this class must be obtained here.  It is  used for array
     * generation or array-construction.
     */
    public VarListBuilderWithApplyAndSort(
            Function<ORIGINAL, TRANSFORMED> transformer, Comparator<TRANSFORMED> comparator,
            Class<TRANSFORMED> outputClass
        )
    { 
        VECTOR          = new VECTOR_CLASS          (null, transformer, comparator, false);
        ARRAYLIST       = new ARRAYLIST_CLASS       (null, transformer, comparator, false);
        ITERATOR        = new ITERATOR_CLASS        (null, transformer, comparator);
        ARRAY           = new ARRAY_CLASS           (null, transformer, comparator, outputClass);
        STREAM          = new STREAM_CLASS          (null, transformer, comparator);
        // STREAM_BUILDER;     // Cannot sort a builder, always 'null'
        LINKEDLIST      = new LINKEDLIST_CLASS      (null, transformer, comparator, false);
        TREESET         = new TREESET_CLASS         (null, transformer, comparator, false);
        STACK           = new STACK_CLASS           (null, transformer, comparator, false);
    }

    private class VECTOR_CLASS implements VarList<Vector<TRANSFORMED>, ORIGINAL>
    {
        private       Vector<TRANSFORMED>               v;
        private final Function<ORIGINAL, TRANSFORMED>   transformer;
        private final Comparator<TRANSFORMED>           comparator;
        private final boolean                           append;

        public final VarList<Vector<TRANSFORMED>, ORIGINAL> create()
        { return append ? this : new VECTOR_CLASS(new Vector<>(), transformer, comparator, false); }

        public final VarList<Vector<TRANSFORMED>, ORIGINAL> appendTo(Vector<TRANSFORMED> v)
        { return new VECTOR_CLASS(v, transformer, comparator, true); }

        public void insert(ORIGINAL original) { v.add(transformer.apply(original)); }

        public final Vector<TRANSFORMED> retrieve() { v.sort(comparator); return v; }

        public VECTOR_CLASS(
                Vector<TRANSFORMED> v, Function<ORIGINAL, TRANSFORMED> transformer,
                Comparator<TRANSFORMED> comparator, boolean append
            )
        { this.v=v; this.transformer=transformer; this.comparator=comparator; this.append=append; }
    }

    private class ARRAYLIST_CLASS implements VarList<ArrayList<TRANSFORMED>, ORIGINAL>
    {
        private       ArrayList<TRANSFORMED>            al;
        private final Function<ORIGINAL, TRANSFORMED>   transformer;
        private final Comparator<TRANSFORMED>           comparator;
        private final boolean                           append;

        public VarList<ArrayList<TRANSFORMED>, ORIGINAL> create()
        { return append ? this : new ARRAYLIST_CLASS(new ArrayList<>(), transformer, comparator, false); }

        public VarList<ArrayList<TRANSFORMED>, ORIGINAL> appendTo(ArrayList<TRANSFORMED> al)
        { return new ARRAYLIST_CLASS(al, transformer, comparator, true); }

        public void insert(ORIGINAL original) { al.add(transformer.apply(original)); }

        public ArrayList<TRANSFORMED> retrieve() { al.sort(comparator); return al; }

        public ARRAYLIST_CLASS(
                ArrayList<TRANSFORMED> al, Function<ORIGINAL, TRANSFORMED> transformer,
                Comparator<TRANSFORMED> comparator, boolean append
            )
        { this.al=al; this.transformer=transformer; this.comparator=comparator; this.append=append; }
    }

    private class ITERATOR_CLASS implements VarList<Iterator<TRANSFORMED>, ORIGINAL>
    {
        private       Vector<TRANSFORMED>               v;
        private final Function<ORIGINAL, TRANSFORMED>   transformer;
        private final Comparator<TRANSFORMED>           comparator;

        public VarList<Iterator<TRANSFORMED>, ORIGINAL> create()
        { return new ITERATOR_CLASS(new Vector<>(), transformer, comparator); }

        public VarList<Iterator<TRANSFORMED>, ORIGINAL> appendTo(Iterator<TRANSFORMED> s)
        { VarList.throwUOE("Iterator"); return null; }

        public void insert(ORIGINAL original) { v.add(transformer.apply(original)); }

        public Iterator<TRANSFORMED> retrieve() { v.sort(comparator); return v.iterator(); }

        public ITERATOR_CLASS(
                Vector<TRANSFORMED> v, Function<ORIGINAL, TRANSFORMED> transformer,
                Comparator<TRANSFORMED> comparator
            )
        { this.v=v; this.transformer=transformer; this.comparator=comparator; }
    }

    private class ARRAY_CLASS implements VarList<TRANSFORMED[], ORIGINAL>
    {
        private       Stream.Builder<TRANSFORMED>       sb;
        private final Function<ORIGINAL, TRANSFORMED>   transformer;
        private final Comparator<TRANSFORMED>           comparator;
        private final Class<TRANSFORMED>                outputClass;

        public VarList<TRANSFORMED[], ORIGINAL> create()
        { return new ARRAY_CLASS(Stream.builder(), transformer, comparator, outputClass); }

        public VarList<TRANSFORMED[], ORIGINAL> appendTo(TRANSFORMED[] a)
        { VarList.throwUOE("array"); return null; }

        public void insert(ORIGINAL original) { sb.accept(transformer.apply(original)); }

        public TRANSFORMED[] retrieve()
        {
            // This idea is straight out of hell.  It does work.  The "IntFunction<TRANSFORMED[]>"
            // is how java.util.stream.Stream deals with generic array creation.
            //
            // The part about java.lang.reflect.Array is copied from "Techie Delight".  It is how
            // to implement Generic Array Creation.

            java.util.function.IntFunction<TRANSFORMED[]> arrayGenerator = (int length) ->
                (TRANSFORMED[]) java.lang.reflect.Array.newInstance(outputClass, length);

            return sb.build().sorted(comparator).toArray(arrayGenerator);
        }

        public ARRAY_CLASS(
                Stream.Builder<TRANSFORMED> sb, Function<ORIGINAL, TRANSFORMED> transformer,
                Comparator<TRANSFORMED> comparator, Class<TRANSFORMED> outputClass
            )
        {
            this.sb=sb;
            this.transformer=transformer;
            this.comparator=comparator;
            this.outputClass=outputClass;
        }
    }

    private class STREAM_CLASS implements VarList<Stream<TRANSFORMED>, ORIGINAL>
    {
        private       Stream.Builder<TRANSFORMED>       sb;
        private final Function<ORIGINAL, TRANSFORMED>   transformer;
        private final Comparator<TRANSFORMED>           comparator;

        public VarList<Stream<TRANSFORMED>, ORIGINAL> create()
        { return new STREAM_CLASS(Stream.builder(), transformer, comparator); }

        public VarList<Stream<TRANSFORMED>, ORIGINAL> appendTo(Stream<TRANSFORMED> s)
        { VarList.throwUOE("Stream"); return null; }

        public void insert(ORIGINAL original) { sb.accept(transformer.apply(original)); }

        public Stream<TRANSFORMED> retrieve() { return sb.build().sorted(comparator); }

        STREAM_CLASS(
                Stream.Builder<TRANSFORMED> sb, Function<ORIGINAL, TRANSFORMED> transformer,
                Comparator<TRANSFORMED> comparator
            )
        { this.sb=sb; this.transformer=transformer; this.comparator=comparator; }
    }

    private class LINKEDLIST_CLASS implements VarList<LinkedList<TRANSFORMED>, ORIGINAL>
    {
        private       LinkedList<TRANSFORMED>           ll;
        private final Function<ORIGINAL, TRANSFORMED>   transformer;
        private final Comparator<TRANSFORMED>           comparator;
        private final boolean                           append;

        public VarList<LinkedList<TRANSFORMED>, ORIGINAL> create()
        {
            return append
                ? this
                : new LINKEDLIST_CLASS(new LinkedList<>(), transformer, comparator, false);
        }

        public VarList<LinkedList<TRANSFORMED>, ORIGINAL> appendTo(LinkedList<TRANSFORMED> ll)
        { return new LINKEDLIST_CLASS(ll, transformer, comparator, true); }

        public void insert(ORIGINAL original) { ll.add(transformer.apply(original)); }

        public LinkedList<TRANSFORMED> retrieve() { ll.sort(comparator); return ll; }

        public LINKEDLIST_CLASS(
                LinkedList<TRANSFORMED> ll, Function<ORIGINAL, TRANSFORMED> transformer,
                Comparator<TRANSFORMED> comparator, boolean append
            )
        { this.ll=ll; this.transformer=transformer; this.comparator=comparator; this.append=append; }
    }

    private class TREESET_CLASS implements VarList<TreeSet<TRANSFORMED>, ORIGINAL>
    {
        private       TreeSet<TRANSFORMED>              ts;
        private final Function<ORIGINAL, TRANSFORMED>   transformer;
        private final Comparator<TRANSFORMED>           comparator;
        private final boolean                           append;

        public VarList<TreeSet<TRANSFORMED>, ORIGINAL> create()
        { return append ? this : new TREESET_CLASS(new TreeSet<>(), transformer, comparator, false); }

        public VarList<TreeSet<TRANSFORMED>, ORIGINAL> appendTo(TreeSet<TRANSFORMED> ts)
        { return new TREESET_CLASS(ts, transformer, comparator, true); }

        public void insert(ORIGINAL original) { ts.add(transformer.apply(original)); }

        public TreeSet<TRANSFORMED> retrieve() { return ts; }

        public TREESET_CLASS(
                TreeSet<TRANSFORMED> ts, Function<ORIGINAL, TRANSFORMED> transformer,
                Comparator<TRANSFORMED> comparator, boolean append
            )
        { this.ts=ts; this.transformer=transformer; this.comparator=comparator; this.append=append; }
    }

    private class STACK_CLASS implements VarList<Stack<TRANSFORMED>, ORIGINAL>
    {
        private       Stack<TRANSFORMED>                s;
        private final Function<ORIGINAL, TRANSFORMED>   transformer;
        private final Comparator<TRANSFORMED>           comparator;
        private final boolean                           append;

        public VarList<Stack<TRANSFORMED>, ORIGINAL> create()
        { return append ? this : new STACK_CLASS(new Stack<>(), transformer, comparator, false); }

        public VarList<Stack<TRANSFORMED>, ORIGINAL> appendTo(Stack<TRANSFORMED> s)
        { return new STACK_CLASS(s, transformer, comparator, true); }

        public void insert(ORIGINAL original) { s.push(transformer.apply(original)); }

        public Stack<TRANSFORMED> retrieve() { s.sort(comparator); return s; }

        public STACK_CLASS(
                Stack<TRANSFORMED> s, Function<ORIGINAL, TRANSFORMED> transformer,
                Comparator<TRANSFORMED> comparator, boolean append
            )
        { this.s=s; this.transformer=transformer; this.comparator=comparator; this.append=append; }
    }
}