1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.opensaml.xml.util;
18
19 import java.io.Serializable;
20 import java.util.ArrayList;
21 import java.util.Collection;
22 import java.util.Collections;
23 import java.util.Iterator;
24 import java.util.List;
25 import java.util.ListIterator;
26
27 import net.jcip.annotations.NotThreadSafe;
28
29
30
31
32
33
34 @NotThreadSafe
35 public class LazyList<ElementType> implements List<ElementType>, Serializable {
36
37
38 private static final long serialVersionUID = -7741904523916701817L;
39
40
41 private List<ElementType> delegate = Collections.emptyList();
42
43
44 public boolean add(ElementType item) {
45 if (delegate.isEmpty()) {
46 delegate = Collections.singletonList(item);
47 return true;
48 } else {
49 delegate = buildList();
50 return delegate.add(item);
51 }
52 }
53
54
55 public void add(int index, ElementType element) {
56 delegate = buildList();
57 delegate.add(index, element);
58 }
59
60
61 public boolean addAll(Collection<? extends ElementType> collection) {
62 delegate = buildList();
63 return delegate.addAll(collection);
64 }
65
66
67 public boolean addAll(int index, Collection<? extends ElementType> collection) {
68 delegate = buildList();
69 return delegate.addAll(index, collection);
70 }
71
72
73 public void clear() {
74 delegate = Collections.emptyList();
75 }
76
77
78 public boolean contains(Object element) {
79 return delegate.contains(element);
80 }
81
82
83 public boolean containsAll(Collection<?> collections) {
84 return delegate.containsAll(collections);
85 }
86
87
88 public ElementType get(int index) {
89 return delegate.get(index);
90 }
91
92
93 public int indexOf(Object element) {
94 return delegate.indexOf(element);
95 }
96
97
98 public boolean isEmpty() {
99 return delegate.isEmpty();
100 }
101
102
103 public Iterator<ElementType> iterator() {
104 delegate = buildList();
105 return delegate.iterator();
106 }
107
108
109 public int lastIndexOf(Object element) {
110 return delegate.lastIndexOf(element);
111 }
112
113
114 public ListIterator<ElementType> listIterator() {
115 delegate = buildList();
116 return delegate.listIterator();
117 }
118
119
120 public ListIterator<ElementType> listIterator(int index) {
121 delegate = buildList();
122 return delegate.listIterator(index);
123 }
124
125
126 public boolean remove(Object element) {
127 delegate = buildList();
128 return delegate.remove(element);
129 }
130
131
132 public ElementType remove(int index) {
133 delegate = buildList();
134 return delegate.remove(index);
135 }
136
137
138 public boolean removeAll(Collection<?> collection) {
139 delegate = buildList();
140 return delegate.removeAll(collection);
141 }
142
143
144 public boolean retainAll(Collection<?> collection) {
145 delegate = buildList();
146 return delegate.retainAll(collection);
147 }
148
149
150 public ElementType set(int index, ElementType element) {
151 delegate = buildList();
152 return delegate.set(index, element);
153 }
154
155
156 public int size() {
157 return delegate.size();
158 }
159
160
161 public List<ElementType> subList(int fromIndex, int toIndex) {
162 delegate = buildList();
163 return delegate.subList(fromIndex, toIndex);
164 }
165
166
167 public Object[] toArray() {
168 return delegate.toArray();
169 }
170
171
172 public <T> T[] toArray(T[] type) {
173 return delegate.toArray(type);
174 }
175
176
177
178
179
180
181 protected List<ElementType> buildList() {
182 if (delegate instanceof ArrayList) {
183 return delegate;
184 }
185
186 return new ArrayList<ElementType>(delegate);
187 }
188
189
190 public String toString() {
191 return delegate.toString();
192 }
193
194
195 public int hashCode() {
196 return delegate.hashCode();
197 }
198
199
200 public boolean equals(Object obj) {
201 if (this == obj) {
202 return true;
203 }
204
205 if (obj == null || this.getClass() != obj.getClass()) {
206 return false;
207 }
208
209 return delegate.equals(((LazyList<?>) obj).delegate);
210 }
211 }