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
28
29
30
31
32 public class LazyList<ElementType> implements List<ElementType>, Serializable {
33
34
35 private static final long serialVersionUID = -7741904523916701817L;
36
37
38 private List<ElementType> delegate = Collections.emptyList();
39
40
41 public boolean add(ElementType item) {
42 if (delegate.isEmpty()) {
43 delegate = Collections.singletonList(item);
44 return true;
45 } else {
46 delegate = buildList();
47 return delegate.add(item);
48 }
49 }
50
51
52 public void add(int index, ElementType element) {
53 delegate = buildList();
54 delegate.add(index, element);
55 }
56
57
58 public boolean addAll(Collection<? extends ElementType> collection) {
59 if(collection == null || collection.isEmpty()){
60 return false;
61 }
62 delegate = buildList();
63 return delegate.addAll(collection);
64 }
65
66
67 public boolean addAll(int index, Collection<? extends ElementType> collection) {
68 if(collection == null || collection.isEmpty()){
69 return false;
70 }
71
72 delegate = buildList();
73 return delegate.addAll(index, collection);
74 }
75
76
77 public void clear() {
78 delegate = Collections.emptyList();
79 }
80
81
82 public boolean contains(Object element) {
83 return delegate.contains(element);
84 }
85
86
87 public boolean containsAll(Collection<?> collection) {
88 return delegate.containsAll(collection);
89 }
90
91
92 public ElementType get(int index) {
93 return delegate.get(index);
94 }
95
96
97 public int indexOf(Object element) {
98 return delegate.indexOf(element);
99 }
100
101
102 public boolean isEmpty() {
103 return delegate.isEmpty();
104 }
105
106
107 public Iterator<ElementType> iterator() {
108 return delegate.iterator();
109 }
110
111
112 public int lastIndexOf(Object element) {
113 return delegate.lastIndexOf(element);
114 }
115
116
117 public ListIterator<ElementType> listIterator() {
118 return delegate.listIterator();
119 }
120
121
122 public ListIterator<ElementType> listIterator(int index) {
123 return delegate.listIterator(index);
124 }
125
126
127 public boolean remove(Object element) {
128 delegate = buildList();
129 return delegate.remove(element);
130 }
131
132
133 public ElementType remove(int index) {
134 delegate = buildList();
135 return delegate.remove(index);
136 }
137
138
139 public boolean removeAll(Collection<?> collection) {
140 if(collection == null || collection.isEmpty()){
141 return false;
142 }
143
144 delegate = buildList();
145 return delegate.removeAll(collection);
146 }
147
148
149 public boolean retainAll(Collection<?> collection) {
150 delegate = buildList();
151 return delegate.retainAll(collection);
152 }
153
154
155 public ElementType set(int index, ElementType element) {
156 delegate = buildList();
157 return delegate.set(index, element);
158 }
159
160
161 public int size() {
162 return delegate.size();
163 }
164
165
166 public List<ElementType> subList(int fromIndex, int toIndex) {
167 return delegate.subList(fromIndex, toIndex);
168 }
169
170
171 public Object[] toArray() {
172 return delegate.toArray();
173 }
174
175
176 public <T> T[] toArray(T[] type) {
177 return delegate.toArray(type);
178 }
179
180
181
182
183
184
185 protected List<ElementType> buildList() {
186 if (delegate instanceof ArrayList) {
187 return delegate;
188 }
189
190 return new ArrayList<ElementType>(delegate);
191 }
192 }