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
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; // For Java-Doc Only

/**
 * 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_SORT_APPLY>
 * 
 * @param <ORIGINAL> This is the original data-type that is to be inserted into the list, after
 * a simple transformation has been applied.  For class this class, the sort is applied before the
 * transformation.
 * 
 * <BR /><BR />Speaking from the perspective of {@link FileNode} and {@link RetTypeChoice}
 * the {@code 'ORIGINAL'} type is always computed to be {@code 'FileNode'}.
 * 
 * @param <TRANSFORMED> This is the data-type, after the transform has been applied.  The list
 * that's returned to the user will be a container / list whose contents have type
 * {@code 'TRANSFORMED'}.
 * 
 * <BR /><BR />In the case of {@link FileNode}, and {@link RetTypeChoice}, the
 * {@code 'TRANSFORMED'} type is usually {@code java.lang.String} - <I>and the {@code String}
 * contents are the <B>Full-Path File-Name</B>, or the <B>File-Name</B>, itself.</I>
 */
@SuppressWarnings("unchecked")
public class VarListBuilderWithSortAndApply<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 'TRANSFORMED'}.
     * 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 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 comparator This is used to sort the returned list.
     * <BR /><BR /><B><SPAN STYLE="color: red;">NOTICE:</SPAN></B> The {@code 'comparator'} is of 
     * parametrized-type {@code 'ORIGINAL'}.  This is because the sort shall occur <B>BEFORE</B>
     * the transform.
     * 
     * @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 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 VarListBuilderWithSortAndApply(
            Comparator<ORIGINAL> comparator, Function<ORIGINAL, TRANSFORMED> transformer,
            Class<TRANSFORMED> outputClass
        )
    {
        VECTOR      = new VECTOR_CLASS      (null, comparator, transformer);
        ARRAYLIST   = new ARRAYLIST_CLASS   (null, comparator, transformer);
        ITERATOR    = new ITERATOR_CLASS    (null, comparator, transformer);
        ARRAY       = new ARRAY_CLASS       (null, comparator, transformer, outputClass);
        STREAM      = new STREAM_CLASS      (null, comparator, transformer);
        // STREAM_BUILDER, always null, Stream.Builder cannot be sorted, only Stream
        LINKEDLIST  = new LINKEDLIST_CLASS  (null, comparator, transformer);
        STACK       = new STACK_CLASS       (null, comparator, transformer);
    }

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

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

        public VarList<Vector<TRANSFORMED>, ORIGINAL> appendTo(Vector<TRANSFORMED> v)
        { VarList.throwUOE2(); return null; }

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

        public Vector<TRANSFORMED> retrieve()
        { 
            v.sort(comparator);
            Vector<TRANSFORMED> ret = new Vector<>(v.size());
            v.forEach((ORIGINAL original) -> ret.add(transformer.apply(original)));
            return ret;
        }

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

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

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

        public VarList<ArrayList<TRANSFORMED>, ORIGINAL> appendTo(ArrayList<TRANSFORMED> al)
        { VarList.throwUOE2(); return null; }

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

        public ArrayList<TRANSFORMED> retrieve()
        { 
            al.sort(comparator);
            ArrayList<TRANSFORMED> ret = new ArrayList<>(al.size());
            al.forEach((ORIGINAL original) -> ret.add(transformer.apply(original)));
            return ret;
        }

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

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

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

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

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

        public Iterator<TRANSFORMED> retrieve()
        { 
            v.sort(comparator);
            Vector<TRANSFORMED> ret = new Vector<>(v.size());
            v.forEach((ORIGINAL original) -> ret.add(transformer.apply(original)));
            return ret.iterator();
        }

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

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

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

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

        public void insert(ORIGINAL original) { sb.accept(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).map((ORIGINAL original) ->
                transformer.apply(original)).toArray(arrayGenerator);
        }  

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

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

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

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

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

        public Stream<TRANSFORMED> retrieve()
        {
            return sb.build().sorted(comparator).map((ORIGINAL original) ->
                transformer.apply(original));
        }

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

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

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

        public VarList<LinkedList<TRANSFORMED>, ORIGINAL> appendTo(LinkedList<TRANSFORMED> ll)
        { VarList.throwUOE2(); return null; }

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

        public LinkedList<TRANSFORMED> retrieve()
        { 
            ll.sort(comparator);
            LinkedList<TRANSFORMED> ret = new LinkedList<>();
            ll.forEach((ORIGINAL original) -> ret.add(transformer.apply(original)));
            return ret;
        }

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

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

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

        public VarList<Stack<TRANSFORMED>, ORIGINAL> appendTo(Stack<TRANSFORMED> s)
        { VarList.throwUOE2(); return null; }

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

        public Stack<TRANSFORMED> retrieve()
        { 
            s.sort(comparator);
            Stack<TRANSFORMED> ret = new Stack<>();
            s.forEach((ORIGINAL original) -> ret.add(transformer.apply(original)));
            return ret;
        }

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