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

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

/**
 * <CODE>VarListBuilderWithApplyAndSort&lt;T, U&gt; - Documentation.</CODE><BR /><BR />
 * <EMBED CLASS="external-html" DATA-FILE-ID="VLBWAASTU">
 */
@SuppressWarnings("unchecked")
public class VarListBuilderWithApplyAndSort<T, U>
{
    /**
     * This {@code VarList} instance has a {@code 'retrieve()'} method that will return a
     * {@code Vector} whose {@code Vector}-contents are of parametrized-type {@code 'U'}.
     * The {@code Vector} returned will have already been sorted upon return.
     */
    public final VarList<Vector<U>, T>          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 'U'}.
     * The {@code ArrayList} returned will have already been sorted upon return.
     */
    public final VarList<ArrayList<U>, T>       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 'U'}.
     * The {@code Iterator} returned will have already been sorted upon return.
     */
    public final VarList<Iterator<U>, T>        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 'U'}.
     * The {@code array} returned will have already been sorted upon return.
     */
    public final VarList<U[], T>                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 'U'}.
     * The {@code Stream} returned will have already been sorted upon return.
     */
    public final VarList<Stream<U>, T>          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<U>, T>  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 'U'}.
     * The {@code LinkedList} returned will have already been sorted upon return.
     */
    public final VarList<LinkedList<U>, T>      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 'U'}.
     * The {@code TreeSet} returned will have already been sorted upon return.
     */
    public final VarList<TreeSet<U>, T>         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 'U'}.
     * The {@code Stack} returned will have already been sorted upon return.
     */
    public final VarList<Stack<U>, T>           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<T, U>'}, the final type of the returned
     * {@code VarList} will of type {@code 'U'}, although while building this {@code 'VarList'},
     * the elements that shall be inserted will be of type {@code 'T'}.
     * <BR /><BR />It is the job of the transformer parameter to convert the elements from type
     * {@code 'T'} to type {@code 'U'}
     *
     * @param comparator This is used to sort the returned list.  Notice that this
     * {@code 'comparator'} shall sort list-elements of type {@code 'U'}.  The list that is
     * ultimately returned by these {@code VarList's} will be of type {@code 'U'} (because of the
     * transform).
     *
     * <BR /><BR /><B><SPAN STYLE="color: red;">NOTICE</SPAN></B> The {@code 'comparator'} is of
     * parametrized-type {@code 'U'}.  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<T, U> transformer, Comparator<U> comparator, Class<U> 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<U>, T>
    {
        private       Vector<U>             v;
        private final Function<T, U>        transformer;
        private final Comparator<U>         comparator;
        private final boolean               append;

        public final VarList<Vector<U>, T>  create()                { return append ? this : new VECTOR_CLASS(new Vector<>(), transformer, comparator, false); }
        public final VarList<Vector<U>, T>  appendTo(Vector<U> v)   { return new VECTOR_CLASS(v, transformer, comparator, true); }
        public void                         insert(T t)             { v.add(transformer.apply(t)); }
        public final Vector<U>              retrieve()              { v.sort(comparator); return v; }

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

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

        public VarList<ArrayList<U>, T>     create()                    { return append ? this : new ARRAYLIST_CLASS(new ArrayList<>(), transformer, comparator, false); }
        public VarList<ArrayList<U>, T>     appendTo(ArrayList<U> al)   { return new ARRAYLIST_CLASS(al, transformer, comparator, true); }
        public void                         insert(T t)                 { al.add(transformer.apply(t)); }
        public ArrayList<U>                 retrieve()                  { al.sort(comparator); return al; }

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

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

        public VarList<Iterator<U>, T>      create()            { return new ITERATOR_CLASS(new Vector<>(), transformer, comparator); }
        public VarList<Iterator<U>, T>  appendTo(Iterator<U> s) { VarList.throwUOE("Iterator"); return null; }
        public void                         insert(T t)         { v.add(transformer.apply(t)); }
        public Iterator<U>                  retrieve()          { v.sort(comparator); return v.iterator(); }

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

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

        // Class<U> clazz = null;
        public VarList<U[], T>              create()        { return new ARRAY_CLASS(Stream.builder(), transformer, comparator, outputClass); }
        public VarList<U[], T>              appendTo(U[] a) { VarList.throwUOE("array"); return null; }
        public void                         insert(T t)     { sb.accept(transformer.apply(t)); }
        public U[]                          retrieve()
        {
            // This idea is straight out of hell.  It does work.  The "IntFunction<U[]>" 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<U[]> arrayGenerator = (int length) -> (U[]) java.lang.reflect.Array.newInstance(outputClass, length);
            return sb.build().sorted(comparator).toArray(arrayGenerator);
        }

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

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

        public VarList<Stream<U>, T>        create()                { return new STREAM_CLASS(Stream.builder(), transformer, comparator); }
        public VarList<Stream<U>, T>        appendTo(Stream<U> s)   { VarList.throwUOE("Stream"); return null; }
        public void                         insert(T t)             { sb.accept(transformer.apply(t)); }
        public Stream<U>                    retrieve()              { return sb.build().sorted(comparator); }

        public STREAM_CLASS(Stream.Builder<U> sb, Function<T, U> transformer, Comparator<U> comparator)
        { this.sb=sb; this.transformer=transformer; this.comparator=comparator; }
    }

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

        public VarList<LinkedList<U>, T>    create()                    { return append ? this : new LINKEDLIST_CLASS(new LinkedList<>(), transformer, comparator, false); }
        public VarList<LinkedList<U>, T>    appendTo(LinkedList<U> ll)  { return new LINKEDLIST_CLASS(ll, transformer, comparator, true); }   
        public void                         insert(T t)                 { ll.add(transformer.apply(t)); }
        public LinkedList<U>                retrieve()                  { ll.sort(comparator); return ll; }

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

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

        public VarList<TreeSet<U>, T>       create()                { return append ? this : new TREESET_CLASS(new TreeSet<>(), transformer, comparator, false); }
        public VarList<TreeSet<U>, T>       appendTo(TreeSet<U> ts) { return new TREESET_CLASS(ts, transformer, comparator, true); }
        public void                         insert(T t)             { ts.add(transformer.apply(t)); }
        public TreeSet<U>                   retrieve()              { return ts; }

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

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

        public VarList<Stack<U>, T>         create()                { return append ? this : new STACK_CLASS(new Stack<>(), transformer, comparator, false); }
        public VarList<Stack<U>, T>         appendTo(Stack<U> s)    { return new STACK_CLASS(s, transformer, comparator, true); }
        public void                         insert(T t)             { s.push(transformer.apply(t)); }
        public Stack<U>                     retrieve()              { s.sort(comparator); return s; }

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