/* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.security.acls.afterinvocation;

import org.apache.commons.collections.iterators.ArrayIterator;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.lang.reflect.Array;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;


/**
 * A filter used to filter arrays.
 *
 * @author Ben Alex
 * @author Paulo Neves
 * @version $Id$
 */
class ArrayFilterer<T> implements Filterer<T> {
    //~ Static fields/initializers =====================================================================================

    protected static final Log logger = LogFactory.getLog(ArrayFilterer.class);

    //~ Instance fields ================================================================================================

    private Set<T> removeList;
    private T[] list;

    //~ Constructors ===================================================================================================

    ArrayFilterer(T[] list) {
        this.list = list;

        // Collect the removed objects to a HashSet so that
        // it is fast to lookup them when a filtered array
        // is constructed.
        removeList = new HashSet<T>();
    }

    //~ Methods ========================================================================================================

    /**
     *
     * @see org.springframework.security.acls.afterinvocation.Filterer#getFilteredObject()
     */
    @SuppressWarnings("unchecked")
    public T[] getFilteredObject() {
        // Recreate an array of same type and filter the removed objects.
        int originalSize = list.length;
        int sizeOfResultingList = originalSize - removeList.size();
        T[] filtered = (T[]) Array.newInstance(list.getClass().getComponentType(), sizeOfResultingList);

        for (int i = 0, j = 0; i < list.length; i++) {
            T object = list[i];

            if (!removeList.contains(object)) {
                filtered[j] = object;
                j++;
            }
        }

        if (logger.isDebugEnabled()) {
            logger.debug("Original array contained " + originalSize + " elements; now contains " + sizeOfResultingList
                + " elements");
        }

        return filtered;
    }

    /**
     *
     * @see org.springframework.security.acls.afterinvocation.Filterer#iterator()
     */
    @SuppressWarnings("unchecked")
    public Iterator<T> iterator() {
        return new ArrayIterator(list);
    }

    /**
     *
     * @see org.springframework.security.acls.afterinvocation.Filterer#remove(java.lang.Object)
     */
    public void remove(T object) {
        removeList.add(object);
    }
}

@@ -686,182 +686,126 @@
 ort 
-org.apache.commons.collections.iterators.ArrayIterator;%0Aimport org.apache.commons.logging.Log;%0Aimport org.apache.commons.logging.LogFactory;%0A%0Aimport java.lang.reflect.Array;%0A
+java.lang.reflect.Array;%0Aimport java.util.HashSet;%0Aimport java.util.Iterator;%0Aimport java.util.NoSuchElementException;
 %0Aimp
@@ -822,64 +822,97 @@
 til.
-Hash
 Set;%0A
+%0A
 import 
-java.util.Iterator;%0Aimport java.util.Set
+org.apache.commons.logging.Log;%0Aimport org.apache.commons.logging.LogFactory
 ;%0A%0A%0A
@@ -1402,16 +1402,22 @@
 private 
+final 
 Set%3CT%3E r
@@ -1439,16 +1439,22 @@
 private 
+final 
 T%5B%5D list
@@ -2989,113 +2989,525 @@
     
-@SuppressWarnings(%22unchecked%22)%0A
+public Iterator%3CT%3E iterator() %7B%0A        return new Iterator%3CT%3E() %7B%0A            private int index = 0;%0A%0A     
     
+   
 public 
-Iterator%3CT%3E iterator() %7B%0A        return new ArrayIterator(list)
+boolean hasNext() %7B%0A                return index %3C list.length;%0A            %7D%0A%0A            public T next() %7B%0A                if (hasNext() == false) %7B%0A                    throw new NoSuchElementException();%0A                %7D%0A                return list%5Bindex++%5D;%0A            %7D%0A%0A            public void remove() %7B%0A                throw new UnsupportedOperationException();%0A            %7D%0A        %7D
 ;%0A  

Remove dependency on commons collections (ArrayIterator).