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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
package Torello.Java.ReadOnly;

import java.util.*;

import Torello.Java.Additional.RemoveUnsupportedIterator;

// This class generally takes calls to "headMap", "tailMap", "subMap" and "headSet", "tailSet",
// and "subSet" and wraps the returned Set's and Map's with a Read-Only Variant of the class.  This
// allows the Read-Only classes to enforce the "Immutability" into all of those little "extras"
// that are included with the java.util.* "Java Collections Framework"

class InterfaceBuilder
{
    // ********************************************************************************************
    // ********************************************************************************************
    // Main Converter Methods
    // ********************************************************************************************
    // ********************************************************************************************


    static <X> ReadOnlyCollection<X> toReadOnlyCollection(final java.util.Collection<X> c)
    {
        return new ReadOnlyCollection<>()
        {
            public int      size()                          { return c.size(); }
            public boolean  isEmpty()                       { return c.isEmpty(); }
            public boolean  contains(Object o)              { return c.contains(o); }
            public Object[] toArray()                       { return c.toArray(); }
            public <X> X[]  toArray(X[] a)                  { return c.toArray(a); }
            public boolean  containsAll(Collection<?> c2)   { return c.containsAll(c2); }

            public RemoveUnsupportedIterator<X> iterator()
            { return new RemoveUnsupportedIterator<>(c.iterator()); }


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

            public int hashCode()
            { return c.hashCode(); }

            public boolean equals(Object object)
            { return ROHelpers.roCollectionEq(object, this); }
        };
    }

    static <X> ReadOnlyList<X> toReadOnlyList(final java.util.List<X> l)
    {
        return new ReadOnlyList<>()
        {
            public int      size()                          { return l.size(); }
            public boolean  isEmpty()                       { return l.isEmpty(); }
            public boolean  contains(Object o)              { return l.contains(o); }
            public Object[] toArray()                       { return l.toArray(); }
            public <X> X[]  toArray(X[] a)                  { return l.toArray(a); }
            public boolean  containsAll(Collection<?> c)    { return l.containsAll(c); }
            public X        get(int index)                  { return l.get(index); }
            public int      indexOf(Object o)               { return l.indexOf(o); }
            public int      lastIndexOf(Object o)           { return l.lastIndexOf(o); }

            public ReadOnlyListIterator<X> listIterator()
            { return toReadOnlyListIterator(l.listIterator()); }

            public ReadOnlyListIterator<X> listIterator(int index)
            { return toReadOnlyListIterator(l.listIterator(index)); }

            public RemoveUnsupportedIterator<X> iterator()
            { return new RemoveUnsupportedIterator<>(l.iterator()); }

            public ReadOnlyList<X> subList(int fromIndex, int toIndex)
            { return toReadOnlyList(l.subList(fromIndex, toIndex)); }


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

            public int hashCode()
            { return l.hashCode(); }

            public boolean equals(Object object)
            { return ROHelpers.roListEq(this, object); }
        };
    }

    static <X> ReadOnlyListIterator<X> toReadOnlyListIterator(java.util.ListIterator<X> li)
    {
        return new ReadOnlyListIterator<>()
        {
            public boolean  hasNext()       { return li.hasNext(); }
            public X        next()          { return li.next(); }
            public boolean  hasPrevious()   { return li.hasPrevious(); }
            public X        previous()      { return li.previous(); }
            public int      nextIndex()     { return li.nextIndex(); }
            public int      previousIndex() { return li.previousIndex(); }
        };
    }

    static <X, Y> ReadOnlyMap<X, Y> toReadOnlyMap(Map<X, Y> m)
    {
        return new ReadOnlyMap<>()
        {
            public int      size()                      { return m.size(); }
            public boolean  isEmpty()                   { return m.isEmpty(); }
            public boolean  containsKey(Object key)     { return m.containsKey(key); }
            public boolean  containsValue(Object value) { return m.containsValue(value); }
            public Y        get(Object key)             { return m.get(key); }

            public ReadOnlySet<X> keySet()
            { return toReadOnlySet(m.keySet()); }

            public ReadOnlyCollection<Y> values()
            { return toReadOnlyCollection(m.values()); }

            public ReadOnlySet<ReadOnlyMap.Entry<X, Y>> entrySet()
            { return ROHelpers.toReadOnlyEntrySet(m.entrySet()); }


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

            public int hashCode()
            { return m.hashCode(); }

            public boolean equals(Object object)
            { return ROHelpers.roMapEq(this, object); }
        };
    }

    static <X, Y> ReadOnlyNavigableMap<X, Y> toReadOnlyNavigableMap(NavigableMap<X, Y> nm)
    {
        return new ReadOnlyNavigableMap<>()
        {
            public ReadOnlyMap.Entry<X, Y>  lowerEntry(X key)
            { return new EntryImpl<>(nm.lowerEntry(key)); }

            public ReadOnlyMap.Entry<X, Y>  floorEntry(X key)
            { return new EntryImpl<>(nm.floorEntry(key)); }

            public ReadOnlyMap.Entry<X, Y>  ceilingEntry(X key)
            { return new EntryImpl<>(nm.ceilingEntry(key)); }

            public ReadOnlyMap.Entry<X, Y>  higherEntry(X key)
            { return new EntryImpl<>(nm.higherEntry(key)); }

            public ReadOnlyMap.Entry<X, Y>  firstEntry()
            { return new EntryImpl<>(nm.firstEntry()); }

            public ReadOnlyMap.Entry<X, Y>  lastEntry()
            { return new EntryImpl<>(nm.lastEntry()); }

            public X lowerKey(X key)    { return nm.lowerKey(key); }
            public X floorKey(X key)    { return nm.floorKey(key); }
            public X ceilingKey(X key)  { return nm.ceilingKey(key); }
            public X higherKey(X key)   { return nm.higherKey(key); }

            public ReadOnlyNavigableMap<X, Y> descendingMap()
            { return toReadOnlyNavigableMap(nm.descendingMap()); }

            public ReadOnlyNavigableSet<X> navigableKeySet()
            { return toReadOnlyNavigableSet(nm.navigableKeySet()); }

            public ReadOnlyNavigableSet<X> descendingKeySet()
            { return toReadOnlyNavigableSet(nm.descendingKeySet()); }

            public ReadOnlyNavigableMap<X, Y> subMap(
                    X fromKey, boolean fromInclusive,
                    X toKey,   boolean toInclusive
                )
            {
                return toReadOnlyNavigableMap
                    (nm.subMap(fromKey, fromInclusive, toKey, toInclusive));
            }

            public ReadOnlyNavigableMap<X, Y> headMap(X toKey, boolean inclusive)
            { return toReadOnlyNavigableMap(nm.headMap(toKey, inclusive)); }

            public ReadOnlyNavigableMap<X, Y> tailMap(X fromKey, boolean inclusive)
            { return toReadOnlyNavigableMap(nm.headMap(fromKey, inclusive)); }

            public ReadOnlySortedMap<X, Y> subMap(X fromKey, X toKey)
            { return toReadOnlySortedMap(nm.subMap(fromKey, toKey)); }

            public ReadOnlySortedMap<X, Y> headMap(X toKey)
            { return toReadOnlySortedMap(nm.headMap(toKey)); }

            public ReadOnlySortedMap<X, Y> tailMap(X fromKey)
            { return toReadOnlySortedMap(nm.tailMap(fromKey)); }


            // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
            // Copied from ReadOnlySortedMap
            // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***

            public Comparator<? super X> comparator()
            { return nm.comparator(); }

            public X firstKey() { return nm.firstKey(); }
            public X lastKey()  { return nm.lastKey(); }

            public ReadOnlySet<X> keySet()
            { return toReadOnlySet(nm.keySet()); }

            public ReadOnlyCollection<Y> values()
            { return toReadOnlyCollection(nm.values()); }

            public ReadOnlySet<ReadOnlyMap.Entry<X, Y>> entrySet()
            { return ROHelpers.toReadOnlyEntrySet(nm.entrySet()); }


            // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
            // Defined in ReadOnlyMap only
            // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***

            public Y get(Object key)
            { return nm.get(key); }

            public boolean containsValue(Object value)
            { return nm.containsValue(value); }

            public boolean containsKey(Object key)
            { return nm.containsKey(key); }

            public boolean isEmpty()
            { return nm.isEmpty(); }

            public int size()
            { return nm.size(); }


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

            public int hashCode()
            { return nm.hashCode(); }

            public boolean equals(Object other)
            { return ROHelpers.roMapEq(this, other); }
        };
    }

    static <X> ReadOnlyNavigableSet<X> toReadOnlyNavigableSet(NavigableSet<X> ns)
    {
        return new ReadOnlyNavigableSet<>()
        {
            public X lower(X x)     { return ns.lower(x); }
            public X floor(X x)     { return ns.floor(x); };
            public X ceiling(X x)   { return ns.ceiling(x); }
            public X higher(X x)    { return ns.higher(x); }

            public RemoveUnsupportedIterator<X> iterator()
            { return new RemoveUnsupportedIterator<>(ns.iterator()); }

            public ReadOnlyNavigableSet<X> descendingSet()
            { return toReadOnlyNavigableSet(ns.descendingSet()); }

            public RemoveUnsupportedIterator<X> descendingIterator()
            { return new RemoveUnsupportedIterator<>(ns.descendingIterator()); }

            public ReadOnlyNavigableSet<X> subSet(
                    X fromElement, boolean fromInclusive,
                    X toElement, boolean toInclusive
                )
            {
                return toReadOnlyNavigableSet
                    (ns.subSet(fromElement, fromInclusive, toElement, toInclusive));
            }

            public ReadOnlyNavigableSet<X> headSet(X toElement, boolean inclusive)
            { return toReadOnlyNavigableSet(ns.headSet(toElement, inclusive)); }

            public ReadOnlyNavigableSet<X> tailSet(X fromElement, boolean inclusive)
            { return toReadOnlyNavigableSet(ns.tailSet(fromElement, inclusive)); }

            public ReadOnlySortedSet<X> subSet(X fromElement, X toElement)
            { return toReadOnlySortedSet(ns.subSet(fromElement, toElement)); }

            public ReadOnlySortedSet<X> headSet(X toElement)
            { return toReadOnlySortedSet(ns.headSet(toElement)); }

            public ReadOnlySortedSet<X> tailSet(X fromElement)
            { return toReadOnlySortedSet(ns.tailSet(fromElement)); }


            // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
            // Block Copied from ReadOnlySortedSet
            // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***

            public Comparator<? super X> comparator()       { return ns.comparator(); }

            public X        first()                         { return ns.first(); }
            public X        last()                          { return ns.last(); }

            public int      size()                          { return ns.size(); }
            public boolean  isEmpty()                       { return ns.isEmpty(); }
            public boolean  contains(Object o)              { return ns.contains(o); }

            public Object[] toArray()                       { return ns.toArray(); }
            public <T> T[]  toArray(T[] a)                  { return ns.toArray(a); }
            public boolean  containsAll(Collection<?> c)    { return ns.containsAll(c); }


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

            public int hashCode()
            { return ns.hashCode(); }

            public boolean equals(Object other)
            { return ROHelpers.roSetEq(this, other); }
        };
    }

    static <X> ReadOnlySet<X> toReadOnlySet(Set<X> s)
    {
        return new ReadOnlySet<>()
        {
            public int      size()                          { return s.size(); }
            public boolean  isEmpty()                       { return s.isEmpty(); }
            public boolean  contains(Object o)              { return s.contains(o); }

            public Object[] toArray()                       { return s.toArray(); }
            public <T> T[]  toArray(T[] a)                  { return s.toArray(a); }
            public boolean  containsAll(Collection<?> c)    { return s.containsAll(c); }

            public RemoveUnsupportedIterator<X> iterator()
            { return new RemoveUnsupportedIterator<>(s.iterator()); }


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

            public boolean equals(Object other)
            { return ROHelpers.roSetEq(this, other); }

            public int hashCode()
            { return s.hashCode(); }
        };
    }

    static <X, Y> ReadOnlySortedMap<X, Y> toReadOnlySortedMap(SortedMap<X, Y> sm)
    {
        return new ReadOnlySortedMap<>()
        {
            public Comparator<? super X> comparator()
            { return sm.comparator(); }

            public ReadOnlySortedMap<X, Y> subMap(X fromKey, X toKey)
            { return toReadOnlySortedMap(sm.subMap(fromKey, toKey)); }

            public ReadOnlySortedMap<X, Y> headMap(X toKey)
            { return toReadOnlySortedMap(sm.headMap(toKey)); }

            public ReadOnlySortedMap<X, Y> tailMap(X fromKey)
            { return toReadOnlySortedMap(sm.tailMap(fromKey)); }

            public X firstKey() { return sm.firstKey(); }
            public X lastKey()  { return sm.lastKey(); }

            public ReadOnlySet<X> keySet()
            { return toReadOnlySet(sm.keySet()); }

            public ReadOnlyCollection<Y> values()
            { return toReadOnlyCollection(sm.values()); }

            public ReadOnlySet<ReadOnlyMap.Entry<X, Y>> entrySet()
            { return ROHelpers.toReadOnlyEntrySet(sm.entrySet()); }


            // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
            // Defined in ReadOnlyMap only
            // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***

            public Y get(Object key)
            { return sm.get(key); }

            public boolean containsValue(Object value)
            { return sm.containsValue(value); }

            public boolean containsKey(Object key)
            { return sm.containsKey(key); }

            public boolean isEmpty()
            { return sm.isEmpty(); }

            public int size()
            { return sm.size(); }


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

            public boolean equals(Object other)
            { return ROHelpers.roMapEq(this, other); }

            public int hashCode()
            { return sm.hashCode(); }
        };
    }

    static <X> ReadOnlySortedSet<X> toReadOnlySortedSet(SortedSet<X> ss)
    {
        return new ReadOnlySortedSet<>()
        {
            public Comparator<? super X> comparator()
            { return ss.comparator(); }

            public ReadOnlySortedSet<X> subSet(X fromElement, X toElement)
            { return toReadOnlySortedSet(ss.subSet(fromElement, toElement)); }

            public ReadOnlySortedSet<X> headSet(X toElement)
            { return toReadOnlySortedSet(ss.headSet(toElement)); }

            public ReadOnlySortedSet<X> tailSet(X fromElement)
            { return toReadOnlySortedSet(ss.tailSet(fromElement)); }

            public X first()    { return ss.first(); }
            public X last()     { return ss.last(); }


            // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
            // Block Copied from ReadOnlySet
            // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***

            public int      size()                          { return ss.size(); }
            public boolean  isEmpty()                       { return ss.isEmpty(); }
            public boolean  contains(Object o)              { return ss.contains(o); }

            public Object[] toArray()                       { return ss.toArray(); }
            public <T> T[]  toArray(T[] a)                  { return ss.toArray(a); }
            public boolean  containsAll(Collection<?> c)    { return ss.containsAll(c); }

            public RemoveUnsupportedIterator<X> iterator()
            { return new RemoveUnsupportedIterator<>(ss.iterator()); }


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

            public int hashCode()
            { return ss.hashCode(); }

            public boolean equals(Object other)
            { return ROHelpers.roSetEq(this, other); }
        };
    }
}