Class | Line # | Actions | |||||
---|---|---|---|---|---|---|---|
SafeIterator | 33 | 10 | 0% | 7 | 6 |
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 org.xwiki.script.internal.safe; | |
21 | ||
22 | import java.lang.reflect.Constructor; | |
23 | import java.util.Iterator; | |
24 | ||
25 | /** | |
26 | * Provide a public script access to an iterator. | |
27 | * | |
28 | * @param <E> the type of the iterated value | |
29 | * @param <I> the type of the Iterator | |
30 | * @version $Id: 05a93eaa42d2110750ceb74da7674a33b9e54328 $ | |
31 | * @since 4.1M1 | |
32 | */ | |
33 | public class SafeIterator<E, I extends Iterator<E>> extends AbstractSafeObject<I> implements Iterator<E> | |
34 | { | |
35 | /** | |
36 | * Safe implementation of the iterator elements. | |
37 | */ | |
38 | private Constructor< ? extends E> safeConstructor; | |
39 | ||
40 | /** | |
41 | * @param it the wrapped iterator | |
42 | * @param safeProvider the provider of instances safe for public scripts | |
43 | * @param safeConstructor the constructor used to create new safe wrapper for iterator elements | |
44 | */ | |
45 | 44 | public SafeIterator(I it, ScriptSafeProvider< ? > safeProvider, Constructor< ? extends E> safeConstructor) |
46 | { | |
47 | 44 | super(it, safeProvider); |
48 | ||
49 | 44 | this.safeConstructor = safeConstructor; |
50 | } | |
51 | ||
52 | /** | |
53 | * @param element the element to wrap | |
54 | * @return the wrapped element | |
55 | */ | |
56 | 44 | protected E safeElement(E element) |
57 | { | |
58 | 44 | if (this.safeConstructor != null) { |
59 | 0 | try { |
60 | 0 | return this.safeConstructor.newInstance(element, this.safeProvider); |
61 | } catch (Exception e) { | |
62 | 0 | return safe(element); |
63 | } | |
64 | } else { | |
65 | 44 | return safe(element); |
66 | } | |
67 | } | |
68 | ||
69 | // Iterator | |
70 | 176 | @Override |
71 | public boolean hasNext() | |
72 | { | |
73 | 176 | return getWrapped().hasNext(); |
74 | } | |
75 | ||
76 | 44 | @Override |
77 | public E next() | |
78 | { | |
79 | 44 | return safeElement(getWrapped().next()); |
80 | } | |
81 | ||
82 | 0 | @Override |
83 | public void remove() | |
84 | { | |
85 | 0 | throw new UnsupportedOperationException(FORBIDDEN); |
86 | } | |
87 | } |