1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.security.authorization.testwikis.internal.entities

File TypeFilteredCollection.java

 

Coverage histogram

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

Code metrics

8
21
7
2
129
69
13
0.62
3
3.5
1.86

Classes

Class Line # Actions
TypeFilteredCollection 36 9 0% 5 0
1.0100%
TypeFilteredCollection.TypeFilteredIterator 87 12 0% 8 5
0.761904876.2%
 

Contributing tests

This file is covered by 13 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 org.xwiki.security.authorization.testwikis.internal.entities;
21   
22    import java.util.AbstractCollection;
23    import java.util.Collection;
24    import java.util.Iterator;
25    import java.util.NoSuchElementException;
26   
27    /**
28    * An generic implementation for providing read-only collection of object assignable to type T from any collection
29    * of elements extending type T.
30    *
31    * @param <T> Type element of the source collection that are kept in the filtered collection.
32    *
33    * @version $Id: 2b86f5747ed71a15b6a5c7f01a365fb4de1cf8a4 $
34    * @since 5.0M2
35    */
 
36    public class TypeFilteredCollection<T> extends AbstractCollection<T>
37    {
38    /** The source collection. */
39    private final Collection<?> source;
40   
41    /** The type to filter. */
42    private final Class<T> type;
43   
44    /**
45    * Protected constructor.
46    * @param source the source collection to be filtered
47    * @param type the type to keep in the resulting collection
48    */
 
49  84 toggle TypeFilteredCollection(Collection<?> source, Class<T> type) {
50  84 this.source = source;
51  84 this.type = type;
52    }
53   
54    /**
55    * Create a new filtered collection based on a source collection and a type to filter.
56    * @param source the source collection to be filtered
57    * @param type the type to keep in the resulting collection
58    * @param <T> the type to keep in the resulting collection
59    * @return a read-only wrapped collection that contains only element of the given type.
60    */
 
61  84 toggle public static <T> TypeFilteredCollection<T> getNewInstance(Collection<?> source, Class<T> type) {
62  84 return new TypeFilteredCollection<T>(source, type);
63    }
64   
 
65  6 toggle @Override
66    public int size()
67    {
68  6 int count = 0;
69  6 for (Object entity : source) {
70  18 if (type.isAssignableFrom(entity.getClass())) {
71  16 count++;
72    }
73    }
74  6 return count;
75    }
76   
 
77  79 toggle @Override
78    public Iterator<T> iterator()
79    {
80  79 return new TypeFilteredIterator<T>();
81    }
82   
83    /**
84    * Specialized iterator for filtering on Type.
85    * @param <T> the type to keep during iteration.
86    */
 
87    class TypeFilteredIterator<T> implements Iterator<T>
88    {
89    /** Iterator on the source collection. */
90    private final Iterator<?> it = source.iterator();
91   
92    /** Temporary cache of the next available object during iteration. */
93    private Object next;
94   
 
95  153 toggle @Override
96    public boolean hasNext()
97    {
98  153 if (next == null) {
99  116 do {
100  116 try {
101  116 next = it.next();
102    } catch (NoSuchElementException e) {
103  79 next = null;
104    }
105  116 } while (next != null && !(type.isAssignableFrom(next.getClass())));
106    }
107  153 return next != null;
108    }
109   
 
110  37 toggle @Override
111    @SuppressWarnings("unchecked")
112    public T next()
113    {
114  37 if (hasNext()) {
115  37 T result = (T) next;
116  37 next = null;
117  37 return result;
118    } else {
119  0 throw new NoSuchElementException();
120    }
121    }
122   
 
123  0 toggle @Override
124    public void remove()
125    {
126  0 throw new UnsupportedOperationException();
127    }
128    }
129    }