| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||||||
| CollectionFilterer |
|
| 1.5;1.5 |
| 1 | /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited |
|
| 2 | * |
|
| 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
|
| 4 | * you may not use this file except in compliance with the License. |
|
| 5 | * You may obtain a copy of the License at |
|
| 6 | * |
|
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
|
| 8 | * |
|
| 9 | * Unless required by applicable law or agreed to in writing, software |
|
| 10 | * distributed under the License is distributed on an "AS IS" BASIS, |
|
| 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
| 12 | * See the License for the specific language governing permissions and |
|
| 13 | * limitations under the License. |
|
| 14 | */ |
|
| 15 | ||
| 16 | package org.acegisecurity.afterinvocation; |
|
| 17 | ||
| 18 | import org.apache.commons.logging.Log; |
|
| 19 | import org.apache.commons.logging.LogFactory; |
|
| 20 | ||
| 21 | import java.util.Collection; |
|
| 22 | import java.util.HashSet; |
|
| 23 | import java.util.Iterator; |
|
| 24 | import java.util.Set; |
|
| 25 | ||
| 26 | ||
| 27 | /** |
|
| 28 | * A filter used to filter Collections. |
|
| 29 | * |
|
| 30 | * @author Ben Alex |
|
| 31 | * @author Paulo Neves |
|
| 32 | * @version $Id: CollectionFilterer.java 1807 2007-05-10 02:25:15Z vishalpuri $ |
|
| 33 | */ |
|
| 34 | class CollectionFilterer implements Filterer { |
|
| 35 | //~ Static fields/initializers ===================================================================================== |
|
| 36 | ||
| 37 | 2 | protected static final Log logger = LogFactory.getLog(CollectionFilterer.class); |
| 38 | ||
| 39 | //~ Instance fields ================================================================================================ |
|
| 40 | ||
| 41 | private Collection collection; |
|
| 42 | ||
| 43 | // collectionIter offers significant performance optimisations (as |
|
| 44 | // per acegisecurity-developer mailing list conversation 19/5/05) |
|
| 45 | private Iterator collectionIter; |
|
| 46 | private Set removeList; |
|
| 47 | ||
| 48 | //~ Constructors =================================================================================================== |
|
| 49 | ||
| 50 | 5 | CollectionFilterer(Collection collection) { |
| 51 | 5 | this.collection = collection; |
| 52 | ||
| 53 | // We create a Set of objects to be removed from the Collection, |
|
| 54 | // as ConcurrentModificationException prevents removal during |
|
| 55 | // iteration, and making a new Collection to be returned is |
|
| 56 | // problematic as the original Collection implementation passed |
|
| 57 | // to the method may not necessarily be re-constructable (as |
|
| 58 | // the Collection(collection) constructor is not guaranteed and |
|
| 59 | // manually adding may lose sort order or other capabilities) |
|
| 60 | 5 | removeList = new HashSet(); |
| 61 | 5 | } |
| 62 | ||
| 63 | //~ Methods ======================================================================================================== |
|
| 64 | ||
| 65 | /** |
|
| 66 | * |
|
| 67 | * @see org.acegisecurity.afterinvocation.Filterer#getFilteredObject() |
|
| 68 | */ |
|
| 69 | public Object getFilteredObject() { |
|
| 70 | // Now the Iterator has ended, remove Objects from Collection |
|
| 71 | 5 | Iterator removeIter = removeList.iterator(); |
| 72 | ||
| 73 | 5 | int originalSize = collection.size(); |
| 74 | ||
| 75 | 22 | while (removeIter.hasNext()) { |
| 76 | 17 | collection.remove(removeIter.next()); |
| 77 | } |
|
| 78 | ||
| 79 | 5 | if (logger.isDebugEnabled()) { |
| 80 | 0 | logger.debug("Original collection contained " + originalSize + " elements; now contains " |
| 81 | + collection.size() + " elements"); |
|
| 82 | } |
|
| 83 | ||
| 84 | 5 | return collection; |
| 85 | } |
|
| 86 | ||
| 87 | /** |
|
| 88 | * |
|
| 89 | * @see org.acegisecurity.afterinvocation.Filterer#iterator() |
|
| 90 | */ |
|
| 91 | public Iterator iterator() { |
|
| 92 | 5 | collectionIter = collection.iterator(); |
| 93 | ||
| 94 | 5 | return collectionIter; |
| 95 | } |
|
| 96 | ||
| 97 | /** |
|
| 98 | * |
|
| 99 | * @see org.acegisecurity.afterinvocation.Filterer#remove(java.lang.Object) |
|
| 100 | */ |
|
| 101 | public void remove(Object object) { |
|
| 102 | 17 | removeList.add(object); |
| 103 | 17 | } |
| 104 | } |