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.collections.iterators.ArrayIterator;
19 import org.apache.commons.logging.Log;
20 import org.apache.commons.logging.LogFactory;
21
22 import java.lang.reflect.Array;
23
24 import java.util.HashSet;
25 import java.util.Iterator;
26 import java.util.Set;
27
28
29 /**
30 * A filter used to filter arrays.
31 *
32 * @author Ben Alex
33 * @author Paulo Neves
34 * @version $Id: ArrayFilterer.java 1784 2007-02-24 21:00:24Z luke_t $
35 */
36 class ArrayFilterer implements Filterer {
37 //~ Static fields/initializers =====================================================================================
38
39 protected static final Log logger =
40 LogFactory.getLog(BasicAclEntryAfterInvocationCollectionFilteringProvider.class);
41
42 //~ Instance fields ================================================================================================
43
44 private Set removeList;
45 private Object[] list;
46
47 //~ Constructors ===================================================================================================
48
49 ArrayFilterer(Object[] list) {
50 this.list = list;
51
52 // Collect the removed objects to a HashSet so that
53 // it is fast to lookup them when a filtered array
54 // is constructed.
55 removeList = new HashSet();
56 }
57
58 //~ Methods ========================================================================================================
59
60 /**
61 *
62 * @see org.acegisecurity.afterinvocation.Filterer#getFilteredObject()
63 */
64 public Object getFilteredObject() {
65 // Recreate an array of same type and filter the removed objects.
66 int originalSize = list.length;
67 int sizeOfResultingList = originalSize - removeList.size();
68 Object[] filtered = (Object[]) Array.newInstance(list.getClass().getComponentType(), sizeOfResultingList);
69
70 for (int i = 0, j = 0; i < list.length; i++) {
71 Object object = list[i];
72
73 if (!removeList.contains(object)) {
74 filtered[j] = object;
75 j++;
76 }
77 }
78
79 if (logger.isDebugEnabled()) {
80 logger.debug("Original array contained " + originalSize + " elements; now contains " + sizeOfResultingList
81 + " elements");
82 }
83
84 return filtered;
85 }
86
87 /**
88 *
89 * @see org.acegisecurity.afterinvocation.Filterer#iterator()
90 */
91 public Iterator iterator() {
92 return new ArrayIterator(list);
93 }
94
95 /**
96 *
97 * @see org.acegisecurity.afterinvocation.Filterer#remove(java.lang.Object)
98 */
99 public void remove(Object object) {
100 removeList.add(object);
101 }
102 }