/*
 *   @(#) $Id$
 *   
 *   Copyright 2004 The Apache Software Foundation
 *
 *   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.apache.ldap.server.authz.support;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

import javax.naming.Name;
import javax.naming.NamingException;
import javax.naming.directory.Attributes;

import org.apache.ldap.common.aci.ACITuple;
import org.apache.ldap.common.aci.AuthenticationLevel;
import org.apache.ldap.common.aci.UserClass;
import org.apache.ldap.server.interceptor.NextInterceptor;

public class MostSpecificUserClassFilter implements ACITupleFilter
{
    public Collection filter( Collection tuples, OperationScope scope, NextInterceptor next, Name userGroupName, Name userName, Attributes userEntry, AuthenticationLevel authenticationLevel, Name entryName, String attrId, Object attrValue, Attributes entry, Collection microOperations ) throws NamingException
    {
        if( tuples.size() <= 1 )
        {
            return tuples;
        }

        Collection filteredTuples = new ArrayList();
        
        // If there are any tuples matching the requestor with UserClasses
        // element name or thisEntry, discard all other tuples.
        for( Iterator i = tuples.iterator(); i.hasNext(); )
        {
            ACITuple tuple = ( ACITuple ) i.next();
            for( Iterator j = tuple.getUserClasses().iterator(); j.hasNext(); )
            {
                UserClass userClass = ( UserClass ) j.next();
                if( userClass instanceof UserClass.Name ||
                        userClass instanceof UserClass.ThisEntry )
                {
                    filteredTuples.add( tuple );
                    break;
                }
            }
        }
        
        if( filteredTuples.size() > 0 )
        {
            return filteredTuples;
        }
        
        // Otherwise if there are any tuples matching UserGroup,
        // discard all other tuples.
        for( Iterator i = tuples.iterator(); i.hasNext(); )
        {
            ACITuple tuple = ( ACITuple ) i.next();
            for( Iterator j = tuple.getUserClasses().iterator(); j.hasNext(); )
            {
                UserClass userClass = ( UserClass ) j.next();
                if( userClass instanceof UserClass.UserGroup )
                {
                    filteredTuples.add( tuple );
                    break;
                }
            }
        }
        
        if( filteredTuples.size() > 0 )
        {
            return filteredTuples;
        }

        // Otherwise if there are any tuples matching subtree,
        // discard all other tuples.
        for( Iterator i = tuples.iterator(); i.hasNext(); )
        {
            ACITuple tuple = ( ACITuple ) i.next();
            for( Iterator j = tuple.getUserClasses().iterator(); j.hasNext(); )
            {
                UserClass userClass = ( UserClass ) j.next();
                if( userClass instanceof UserClass.Subtree )
                {
                    filteredTuples.add( tuple );
                    break;
                }
            }
        }
        
        if( filteredTuples.size() > 0 )
        {
            tuples.clear();
            tuples.addAll( filteredTuples );
            return filteredTuples;
        }
        
        return tuples;
    }

}

@@ -3735,81 +3735,8 @@
   %7B%0A
-            tuples.clear();%0A            tuples.addAll( filteredTuples );%0A
     

Removed unnecessary code.