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 | package Torello.Java.ReadOnly;
import Torello.Java.Additional.RemoveUnsupportedIterator;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
class JavaHTMLReadOnlyList<E> implements ReadOnlyList<E>
{
private final List<E> l;
JavaHTMLReadOnlyList(List<E> l) { this.l = l; }
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); }
// Here is what Chat-GPT said to me:
//
// ME:
//
// This is making me upset again... I have nariest / faintest clue or inkling what
// "other engineers" would prefer in this situation... Furthermore, this just seems so
// completely unimportant, that I do not want to spend more than 15 seconds even
// thinking about it.
//
// Isn't the "Reflection" Version *MUCH* slower than the if-instanceof version?
//
// CHAT-GPT:
//
// Best "Forget-About-It" Fix (15-Second Solution)
// If you don’t want to waste another second:
//
// if (l instanceof Vector) return ((Vector<X>) l).clone();
// if (l instanceof ArrayList) return ((ArrayList<X>) l).clone();
// return new ArrayList<>(l);
//
// ✅ No reflection.
// ✅ Fast enough.
// ✅ Works in 99.9% of cases.
// ✅ Doesn’t waste more of your time.
//
// If some weird list type comes up in the future? Not your problem today.
// So—just slap that in and move on? 😆
@SuppressWarnings("unchecked")
public List<E> cloneToList()
{
if (l instanceof Vector) return (List<E>) ((Vector<E>) l).clone();
if (l instanceof ArrayList) return (List<E>) ((ArrayList<E>) l).clone();
return new ArrayList<>(l);
}
public List<E> wrapToImmutableList()
{ return Collections.unmodifiableList(l); }
public boolean containsAll(Collection<?> c) { return l.containsAll(c); }
public E 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<E> listIterator()
{ return new JavaHTMLReadOnlyListIterator<>(l.listIterator()); }
public ReadOnlyListIterator<E> listIterator(int index)
{ return new JavaHTMLReadOnlyListIterator<>(l.listIterator(index)); }
public RemoveUnsupportedIterator<E> iterator()
{ return new RemoveUnsupportedIterator<>(l.iterator()); }
public ReadOnlyList<E> subList(int fromIndex, int toIndex)
{ return new JavaHTMLReadOnlyList<>(l.subList(fromIndex, toIndex)); }
// *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
// java.lang.Object
// *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
public int hashCode()
{ return l.hashCode(); }
public boolean equals(Object object)
{ return ROHelperEquals.roListEq(this, object); }
public String toString()
{ return l.toString(); }
}
|