1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package com.xpn.xwiki.internal

File AbstractNotifyOnUpdateList.java

 

Coverage histogram

../../../../img/srcFileCovDistChart9.png
41% of files have more coverage

Code metrics

12
76
40
3
370
260
46
0.61
1.9
13.33
1.15

Classes

Class Line # Actions
AbstractNotifyOnUpdateList 35 59 0% 35 16
0.8484%
AbstractNotifyOnUpdateList.Itr 277 5 0% 4 0
1.0100%
AbstractNotifyOnUpdateList.ListItr 315 12 0% 7 8
0.5789473757.9%
 

Contributing tests

This file is covered by 41 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package com.xpn.xwiki.internal;
21   
22    import java.util.Collection;
23    import java.util.Iterator;
24    import java.util.List;
25    import java.util.ListIterator;
26   
27    /**
28    * Generic list implementation with method to call when updating the list. This is used for the attachment list in
29    * XWikiDocument.
30    *
31    * @version $Id: 178b90ad4a4471630df6b8c826299d6a8d47012e $
32    * @since 4.3M2
33    * @param <E> type parameter.
34    */
 
35    public abstract class AbstractNotifyOnUpdateList<E> implements List<E>
36    {
37   
38    /** The list to wrap. */
39    private final List<E> list;
40   
41    /**
42    * @param list the list to wrap.
43    */
 
44  99775 toggle protected AbstractNotifyOnUpdateList(List<E> list)
45    {
46  99775 this.list = list;
47    }
48   
49    /** Called when the list is updated. The method will be called at least once, but may be called several times. */
50    protected abstract void onUpdate();
51   
52    /**
53    * @param element the element that just been added to the list
54    * @since 9.10RC1
55    */
 
56  3709 toggle protected void added(E element)
57    {
58    // Should be overwritten by extending classes that need to know about new elements
59    }
60   
 
61  6 toggle @Override
62    public boolean add(E e)
63    {
64  6 boolean ret = this.list.add(e);
65  6 if (ret) {
66  6 onUpdate();
67  6 added(e);
68    }
69  6 return ret;
70    }
71   
 
72  975 toggle @Override
73    public void add(int index, E element)
74    {
75  975 this.list.add(index, element);
76  975 onUpdate();
77  975 added(element);
78    }
79   
 
80  5106 toggle @Override
81    public boolean addAll(Collection<? extends E> c)
82    {
83  5107 boolean ret = this.list.addAll(c);
84  5107 if (ret) {
85  736 onUpdate();
86  736 for (E e : c) {
87  1749 added(e);
88    }
89    }
90  5107 return ret;
91    }
92   
 
93  1 toggle @Override
94    public boolean addAll(int index, Collection<? extends E> c)
95    {
96  1 boolean ret = this.list.addAll(index, c);
97  1 if (ret) {
98  1 onUpdate();
99  1 for (E e : c) {
100  2 added(e);
101    }
102    }
103  1 return ret;
104    }
105   
 
106  5101 toggle @Override
107    public void clear()
108    {
109  5101 this.list.clear();
110  5101 onUpdate();
111    }
112   
 
113  14476 toggle @Override
114    public boolean contains(Object o)
115    {
116  14476 return this.list.contains(o);
117    }
118   
 
119  1 toggle @Override
120    public boolean containsAll(Collection<?> c)
121    {
122  1 return this.list.containsAll(c);
123    }
124   
 
125  91 toggle @Override
126    public boolean equals(Object o)
127    {
128  91 return this.list.equals(o);
129    }
130   
 
131  227 toggle @Override
132    public E get(int index)
133    {
134  227 return this.list.get(index);
135    }
136   
 
137  0 toggle @Override
138    public int hashCode()
139    {
140  0 return this.list.hashCode();
141    }
142   
 
143  0 toggle @Override
144    public int indexOf(Object o)
145    {
146  0 return this.list.indexOf(o);
147    }
148   
 
149  302 toggle @Override
150    public boolean isEmpty()
151    {
152  302 return this.list.isEmpty();
153    }
154   
 
155  131672 toggle @Override
156    public Iterator<E> iterator()
157    {
158  131672 return new Itr(this.list.iterator());
159    }
160   
 
161  0 toggle @Override
162    public int lastIndexOf(Object o)
163    {
164  0 return this.list.lastIndexOf(o);
165    }
166   
 
167  114 toggle @Override
168    public ListIterator<E> listIterator()
169    {
170  114 return new ListItr(this.list.listIterator());
171    }
172   
 
173  0 toggle @Override
174    public ListIterator<E> listIterator(int index)
175    {
176  0 return new ListItr(this.list.listIterator(index));
177    }
178   
 
179  0 toggle @Override
180    public E remove(int index)
181    {
182  0 E ret = this.list.remove(index);
183  0 onUpdate();
184  0 return ret;
185    }
186   
 
187  7 toggle @Override
188    public boolean remove(Object o)
189    {
190  7 boolean ret = this.list.remove(o);
191  7 if (ret) {
192  6 onUpdate();
193    }
194  7 return ret;
195    }
196   
 
197  2 toggle @Override
198    public boolean removeAll(Collection<?> c)
199    {
200  2 boolean ret = this.list.removeAll(c);
201  2 if (ret) {
202  1 onUpdate();
203    }
204  2 return ret;
205    }
206   
 
207  2 toggle @Override
208    public boolean retainAll(Collection<?> c)
209    {
210  2 boolean ret = this.list.retainAll(c);
211  2 if (ret) {
212  1 onUpdate();
213    }
214  2 return ret;
215    }
216   
 
217  976 toggle @Override
218    public E set(int index, E element)
219    {
220  976 E ret = this.list.set(index, element);
221  976 onUpdate();
222  976 added(element);
223  976 return ret;
224    }
225   
 
226  13530 toggle @Override
227    public int size()
228    {
229  13530 return this.list.size();
230    }
231   
 
232  1 toggle @Override
233    public List<E> subList(int fromIndex, int toIndex)
234    {
235  1 return new AbstractNotifyOnUpdateList<E>(this.list.subList(fromIndex, toIndex))
236    {
 
237  1 toggle @Override
238    public void onUpdate()
239    {
240  1 AbstractNotifyOnUpdateList.this.onUpdate();
241    }
242    };
243    }
244   
 
245  44 toggle @Override
246    public Object[] toArray()
247    {
248  44 return this.list.toArray();
249    }
250   
 
251  0 toggle @Override
252    public <T> T[] toArray(T[] a)
253    {
254  0 return this.list.toArray(a);
255    }
256   
257    /**
258    * {@inheritDoc}
259    * <p>
260    * NOTE: We override the default {@link Object#toString()} implementation in order to preserve backwards
261    * compatibility with code that was using {@link List#toString()} before we introduced
262    * {@link AbstractNotifyOnUpdateList}.
263    * </p>
264    *
265    * @see <a href="https://jira.xwiki.org/browse/XWIKI-9013">XWIKI-9013: Multiple Select List values are not correctly
266    * indexed by Lucene</a>
267    */
 
268  537 toggle @Override
269    public String toString()
270    {
271  537 return this.list.toString();
272    }
273   
274    /**
275    * Wrap iterator.
276    */
 
277    private class Itr implements Iterator<E>
278    {
279   
280    /** Wrapped iterator. */
281    private final Iterator<E> iterator;
282   
283    /**
284    * @param iterator wrapped iteratlr
285    */
 
286  131786 toggle Itr(Iterator<E> iterator)
287    {
288  131786 this.iterator = iterator;
289    }
290   
 
291  181322 toggle @Override
292    public boolean hasNext()
293    {
294  181322 return this.iterator.hasNext();
295    }
296   
 
297  36283 toggle @Override
298    public E next()
299    {
300  36283 return this.iterator.next();
301    }
302   
 
303  2 toggle @Override
304    public void remove()
305    {
306  2 this.iterator.remove();
307  2 onUpdate();
308    }
309   
310    }
311   
312    /**
313    * Wrap list iterator.
314    */
 
315    private class ListItr extends Itr implements ListIterator<E>
316    {
317   
318    /** The wrapped iterator. */
319    private final ListIterator<E> iterator;
320   
321    /**
322    * @param iterator wrapped iterator.
323    */
 
324  114 toggle ListItr(ListIterator<E> iterator)
325    {
326  114 super(iterator);
327  114 this.iterator = iterator;
328    }
329   
 
330  1 toggle @Override
331    public void add(E e)
332    {
333  1 this.iterator.add(e);
334  1 onUpdate();
335  1 added(e);
336    }
337   
 
338  0 toggle @Override
339    public int nextIndex()
340    {
341  0 return this.iterator.nextIndex();
342    }
343   
 
344  0 toggle @Override
345    public boolean hasPrevious()
346    {
347  0 return this.iterator.hasPrevious();
348    }
349   
 
350  0 toggle @Override
351    public E previous()
352    {
353  0 return this.iterator.previous();
354    }
355   
 
356  0 toggle @Override
357    public int previousIndex()
358    {
359  0 return this.iterator.previousIndex();
360    }
361   
 
362  1 toggle @Override
363    public void set(E e)
364    {
365  1 this.iterator.set(e);
366  1 onUpdate();
367  1 added(e);
368    }
369    }
370    }