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 return delegate.iterator();
105 }
106
107
108 public int lastIndexOf(Object element) {
109 return delegate.lastIndexOf(element);
110 }
111
112
113 public ListIterator<ElementType> listIterator() {
114 return delegate.listIterator();
115 }
116
117
118 public ListIterator<ElementType> listIterator(int index) {
119 return delegate.listIterator(index);
120 }
121
122
123 public boolean remove(Object element) {
124 delegate = buildList();
125 return delegate.remove(element);
126 }
127
128
129 public ElementType remove(int index) {
130 delegate = buildList();
131 return delegate.remove(index);
132 }
133
134
135 public boolean removeAll(Collection<?> collection) {
136 delegate = buildList();
137 return delegate.removeAll(collection);
138 }
139
140
141 public boolean retainAll(Collection<?> collection) {
142 delegate = buildList();
143 return delegate.retainAll(collection);
144 }
145
146
147 public ElementType set(int index, ElementType element) {
148 delegate = buildList();
149 return delegate.set(index, element);
150 }
151
152
153 public int size() {
154 return delegate.size();
155 }
156
157
158 public List<ElementType> subList(int fromIndex, int toIndex) {
159 return delegate.subList(fromIndex, toIndex);
160 }
161
162
163 public Object[] toArray() {
164 return delegate.toArray();
165 }
166
167
168 public <T> T[] toArray(T[] type) {
169 return delegate.toArray(type);
170 }
171
172
173
174
175
176
177 protected List<ElementType> buildList() {
178 if (delegate instanceof ArrayList) {
179 return delegate;
180 }
181
182 return new ArrayList<ElementType>(delegate);
183 }
184
185
186 public String toString() {
187 return delegate.toString();
188 }
189
190
191 public int hashCode() {
192 return delegate.hashCode();
193 }
194
195
196 public boolean equals(Object obj) {
197 if (this == obj) {
198 return true;
199 }
200
201 if (obj == null || this.getClass() != obj.getClass()) {
202 return false;
203 }
204
205 return delegate.equals(((LazyList<?>) obj).delegate);
206 }
207 }