File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\core\src\main\java\jenkins\model\lazy\BuildReferenceMapAdapter.java.html

1 package jenkins.model.lazy;
2
3 import groovy.util.MapEntry;
4 import hudson.util.AdaptedIterator;
5 import hudson.util.Iterators;
6
7 import javax.annotation.Nullable;
8 import java.lang.reflect.Array;
9 import java.util.ArrayList;
10 import java.util.Collection;
11 import java.util.Comparator;
12 import java.util.Iterator;
13 import java.util.List;
14 import java.util.Map;
15 import java.util.Set;
16 import java.util.SortedMap;
17
18 /**
19 * Take {@code SortedMap<Integer,BuildReference<R>>} and make it look like {@code SortedMap<Integer,R>}.
20 *
21 * When {@link BuildReference} lost the build object, we'll use {@link AbstractLazyLoadRunMap#getById(String)}
22 * to obtain one.
23 *
24 * @author Kohsuke Kawaguchi
25 */
26 class BuildReferenceMapAdapter<R> implements SortedMap<Integer,R> {
27 private final AbstractLazyLoadRunMap<R> loader;
28
29 private final SortedMap<Integer,BuildReference<R>> core;
30
31 BuildReferenceMapAdapter(AbstractLazyLoadRunMap<R> loader, SortedMap<Integer, BuildReference<R>> core) {
32 this.loader = loader;
33 this.core = core;
34 }
35
36 private R unwrap(@Nullable BuildReference<R> ref) {
37 if (ref==null) return null;
38
39 R v = ref.get();
40 if (v==null)
41 v = loader.getById(ref.id);
42 return v;
43 }
44
45 private BuildReference<R> wrap(@Nullable R value) {
46 if (value==null) return null;
47 return loader.createReference(value);
48 }
49
50
51
52
53 public Comparator<? super Integer> comparator() {
54 return core.comparator();
55 }
56
57 public SortedMap<Integer, R> subMap(Integer fromKey, Integer toKey) {
58 return new BuildReferenceMapAdapter<R>(loader,core.subMap(fromKey, toKey));
59 }
60
61 public SortedMap<Integer, R> headMap(Integer toKey) {
62 return new BuildReferenceMapAdapter<R>(loader,core.headMap(toKey));
63 }
64
65 public SortedMap<Integer, R> tailMap(Integer fromKey) {
66 return new BuildReferenceMapAdapter<R>(loader,core.tailMap(fromKey));
67 }
68
69 public Integer firstKey() {
70 return core.firstKey();
71 }
72
73 public Integer lastKey() {
74 return core.lastKey();
75 }
76
77 public Set<Integer> keySet() {
78 return core.keySet();
79 }
80
81 public Collection<R> values() {
82 return new CollectionAdapter(core.values());
83 }
84
85 public Set<Entry<Integer,R>> entrySet() {
86 return new SetAdapter(core.entrySet());
87 }
88
89 public int size() {
90 return core.size();
91 }
92
93 public boolean isEmpty() {
94 return core.isEmpty();
95 }
96
97 public boolean containsKey(Object key) {
98 return core.containsKey(key);
99 }
100
101 public boolean containsValue(Object value) {
102 return core.containsValue(value);
103 }
104
105 public R get(Object key) {
106 return unwrap(core.get(key));
107 }
108
109 public R put(Integer key, R value) {
110 return unwrap(core.put(key, wrap(value)));
111 }
112
113 public R remove(Object key) {
114 return unwrap(core.remove(key));
115 }
116
117 public void putAll(Map<? extends Integer, ? extends R> m) {
118 for (Entry<? extends Integer, ? extends R> e : m.entrySet())
119 put(e.getKey(), e.getValue());
120 }
121
122 public void clear() {
123 core.clear();
124 }
125
126 @Override
127 public boolean equals(Object o) {
128 return core.equals(o);
129 }
130
131 @Override
132 public int hashCode() {
133 return core.hashCode();
134 }
135
136 private class CollectionAdapter implements Collection<R> {
137 private final Collection<BuildReference<R>> core;
138
139 private CollectionAdapter(Collection<BuildReference<R>> core) {
140 this.core = core;
141 }
142
143 public int size() {
144 return core.size();
145 }
146
147 public boolean isEmpty() {
148 return core.isEmpty();
149 }
150
151 public boolean contains(Object o) {
152 // TODO: to properly pass this onto core, we need to wrap o into BuildReference but also needs to figure out ID.
153 throw new UnsupportedOperationException();
154 }
155
156 public Iterator<R> iterator() {
157 // silently drop null, as if we didn't have them in this collection in the first place
158 // this shouldn't be indistinguishable from concurrent modifications to the collection
159 return Iterators.removeNull(new AdaptedIterator<BuildReference<R>,R>(core.iterator()) {
160 protected R adapt(BuildReference<R> ref) {
161 return unwrap(ref);
162 }
163 });
164 }
165
166 public Object[] toArray() {
167 List<Object> list = new ArrayList<Object>();
168 for (R r : this)
169 list.add(r);
170 return list.toArray();
171 }
172
173 public <T> T[] toArray(T[] a) {
174 int size = size();
175 T[] r = a;
176 if (r.length>size)
177 r = (T[]) Array.newInstance(a.getClass().getComponentType(), size);
178
179 Iterator<R> itr = iterator();
180 int i=0;
181
182 while (itr.hasNext()) {
183 r[i++] = (T)itr.next();
184 }
185
186 return r;
187 }
188
189 public boolean add(R value) {
190 return core.add(wrap(value));
191 }
192
193 public boolean remove(Object o) {
194 // return core.remove(o);
195 // TODO: to properly pass this onto core, we need to wrap o into BuildReference but also needs to figure out ID.
196 throw new UnsupportedOperationException();
197 }
198
199 public boolean containsAll(Collection<?> c) {
200 for (Object o : c) {
201 if (!contains(o))
202 return false;
203 }
204 return true;
205 }
206
207 public boolean addAll(Collection<? extends R> c) {
208 boolean b=false;
209 for (R r : c) {
210 b |= add(r);
211 }
212 return b;
213 }
214
215 public boolean removeAll(Collection<?> c) {
216 boolean b=false;
217 for (Object o : c) {
218 b|=remove(o);
219 }
220 return b;
221 }
222
223 public boolean retainAll(Collection<?> c) {
224 // TODO: to properly pass this onto core, we need to wrap o into BuildReference but also needs to figure out ID.
225 throw new UnsupportedOperationException();
226 }
227
228 public void clear() {
229 core.clear();
230 }
231
232 @Override
233 public boolean equals(Object o) {
234 return core.equals(o);
235 }
236
237 @Override
238 public int hashCode() {
239 return core.hashCode();
240 }
241 }
242
243 private class SetAdapter implements Set<Entry<Integer, R>> {
244 private final Set<Entry<Integer, BuildReference<R>>> core;
245
246 private SetAdapter(Set<Entry<Integer, BuildReference<R>>> core) {
247 this.core = core;
248 }
249
250 public int size() {
251 return core.size();
252 }
253
254 public boolean isEmpty() {
255 return core.isEmpty();
256 }
257
258 public boolean contains(Object o) {
259 // TODO: to properly pass this onto core, we need to wrap o into BuildReference but also needs to figure out ID.
260 throw new UnsupportedOperationException();
261 }
262
263 public Iterator<Entry<Integer, R>> iterator() {
264 return Iterators.removeNull(new AdaptedIterator<Entry<Integer,BuildReference<R>>,Entry<Integer, R>>(core.iterator()) {
265 protected Entry<Integer, R> adapt(Entry<Integer, BuildReference<R>> e) {
266 return _unwrap(e);
267 }
268 });
269 }
270
271 public Object[] toArray() {
272 List<Object> list = new ArrayList<Object>();
273 for (Entry<Integer, R> r : this)
274 list.add(r);
275 return list.toArray();
276 }
277
278 public <T> T[] toArray(T[] a) {
279 int size = size();
280 T[] r = a;
281 if (r.length>size)
282 r = (T[]) Array.newInstance(a.getClass().getComponentType(), size);
283
284 Iterator<Entry<Integer, R>> itr = iterator();
285 int i=0;
286
287 while (itr.hasNext()) {
288 r[i++] = (T)itr.next();
289 }
290
291 return r;
292 }
293
294 public boolean add(Entry<Integer, R> value) {
295 return core.add(_wrap(value));
296 }
297
298 public boolean remove(Object o) {
299 // return core.remove(o);
300 // TODO: to properly pass this onto core, we need to wrap o into BuildReference but also needs to figure out ID.
301 throw new UnsupportedOperationException();
302 }
303
304 public boolean containsAll(Collection<?> c) {
305 for (Object o : c) {
306 if (!contains(o))
307 return false;
308 }
309 return true;
310 }
311
312 public boolean addAll(Collection<? extends Entry<Integer,R>> c) {
313 boolean b=false;
314 for (Entry<Integer,R> r : c) {
315 b |= add(r);
316 }
317 return b;
318 }
319
320 public boolean removeAll(Collection<?> c) {
321 boolean b=false;
322 for (Object o : c) {
323 b|=remove(o);
324 }
325 return b;
326 }
327
328 public boolean retainAll(Collection<?> c) {
329 // TODO: to properly pass this onto core, we need to wrap o into BuildReference but also needs to figure out ID.
330 throw new UnsupportedOperationException();
331 }
332
333 public void clear() {
334 core.clear();
335 }
336
337 @Override
338 public boolean equals(Object o) {
339 return core.equals(o);
340 }
341
342 @Override
343 public int hashCode() {
344 return core.hashCode();
345 }
346
347 private Entry<Integer,BuildReference<R>> _wrap(Entry<Integer,R> e) {
348 return new MapEntry(e.getKey(),wrap(e.getValue()));
349 }
350 private Entry<Integer, R> _unwrap(Entry<Integer, BuildReference<R>> e) {
351 R v = unwrap(e.getValue());
352 if (v==null)
353 return null;
354 return new MapEntry(e.getKey(), v);
355 }
356 }
357 }
358