1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.script.internal.safe

File SafeCollection.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart0.png
83% of files have more coverage

Code metrics

6
26
15
1
161
98
19
0.73
1.73
15
1.27

Classes

Class Line # Actions
SafeCollection 34 26 0% 19 47
0.00%
 

Contributing tests

No tests hitting this source file were found.

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 org.xwiki.script.internal.safe;
21   
22    import java.lang.reflect.Constructor;
23    import java.util.Collection;
24    import java.util.Iterator;
25   
26    /**
27    * Provide a public script access to a collection.
28    *
29    * @param <E> the type of the elements in the list
30    * @param <C> the type of the Collection
31    * @version $Id: 0d3b0c960f677ea0e37960d76cc599a86b69767d $
32    * @since 4.1M1
33    */
 
34    public class SafeCollection<E, C extends Collection<E>> extends AbstractSafeObject<C> implements Collection<E>
35    {
36    /**
37    * Safe implementation of the iterator elements.
38    */
39    protected Constructor< ? extends E> safeConstructor;
40   
41    /**
42    * @param collection the wrapped list
43    * @param safeProvider the provider of instances safe for public scripts
44    * @param safeConstructor the cinstructor to use to create new instance of list elements
45    */
 
46  0 toggle public SafeCollection(C collection, ScriptSafeProvider< ? > safeProvider, Constructor< ? extends E> safeConstructor)
47    {
48  0 super(collection, safeProvider);
49   
50  0 this.safeConstructor = safeConstructor;
51    }
52   
53    /**
54    * @param element the element to wrap
55    * @return the wrapped element
56    */
 
57  0 toggle protected E safeElement(E element)
58    {
59  0 if (this.safeConstructor != null) {
60  0 try {
61  0 return this.safeConstructor.newInstance(element, this.safeProvider);
62    } catch (Exception e) {
63  0 return safe(element);
64    }
65    } else {
66  0 return safe(element);
67    }
68    }
69   
70    // List
71   
 
72  0 toggle @Override
73    public int size()
74    {
75  0 return getWrapped().size();
76    }
77   
 
78  0 toggle @Override
79    public boolean isEmpty()
80    {
81  0 return getWrapped().isEmpty();
82    }
83   
 
84  0 toggle @Override
85    public boolean contains(Object o)
86    {
87  0 return getWrapped().contains(o);
88    }
89   
 
90  0 toggle @Override
91    public Iterator<E> iterator()
92    {
93  0 return new SafeIterator<E, Iterator<E>>(getWrapped().iterator(), this.safeProvider, this.safeConstructor);
94    }
95   
 
96  0 toggle @Override
97    public Object[] toArray()
98    {
99  0 Object[] copy = getWrapped().toArray();
100   
101  0 for (int i = 0; i < copy.length; ++i) {
102  0 copy[i] = safe(copy[i]);
103    }
104   
105  0 return copy;
106    }
107   
 
108  0 toggle @Override
109    public <T> T[] toArray(T[] a)
110    {
111  0 T[] copy = getWrapped().toArray(a);
112   
113  0 for (int i = 0; i < copy.length; ++i) {
114  0 copy[i] = safe(copy[i]);
115    }
116   
117  0 return copy;
118    }
119   
 
120  0 toggle @Override
121    public boolean add(E e)
122    {
123  0 throw new UnsupportedOperationException(FORBIDDEN);
124    }
125   
 
126  0 toggle @Override
127    public boolean remove(Object o)
128    {
129  0 throw new UnsupportedOperationException(FORBIDDEN);
130    }
131   
 
132  0 toggle @Override
133    public boolean containsAll(Collection< ? > c)
134    {
135  0 return getWrapped().containsAll(c);
136    }
137   
 
138  0 toggle @Override
139    public boolean addAll(Collection< ? extends E> c)
140    {
141  0 throw new UnsupportedOperationException(FORBIDDEN);
142    }
143   
 
144  0 toggle @Override
145    public boolean removeAll(Collection< ? > c)
146    {
147  0 throw new UnsupportedOperationException(FORBIDDEN);
148    }
149   
 
150  0 toggle @Override
151    public boolean retainAll(Collection< ? > c)
152    {
153  0 throw new UnsupportedOperationException(FORBIDDEN);
154    }
155   
 
156  0 toggle @Override
157    public void clear()
158    {
159  0 throw new UnsupportedOperationException(FORBIDDEN);
160    }
161    }