001    /*
002     *  Licensed to the Apache Software Foundation (ASF) under one
003     *  or more contributor license agreements.  See the NOTICE file
004     *  distributed with this work for additional information
005     *  regarding copyright ownership.  The ASF licenses this file
006     *  to you under the Apache License, Version 2.0 (the
007     *  "License"); you may not use this file except in compliance
008     *  with the License.  You may obtain a copy of the License at
009     *  
010     *    http://www.apache.org/licenses/LICENSE-2.0
011     *  
012     *  Unless required by applicable law or agreed to in writing,
013     *  software distributed under the License is distributed on an
014     *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     *  KIND, either express or implied.  See the License for the
016     *  specific language governing permissions and limitations
017     *  under the License. 
018     *  
019     */
020    package org.apache.directory.shared.ldap.aci;
021    
022    
023    import java.util.ArrayList;
024    import java.util.Collection;
025    import java.util.Collections;
026    
027    
028    /**
029     * Represents permissions to be applied to all {@link ProtectedItem}s in
030     * {@link ItemFirstACIItem}.
031     * 
032     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
033     * @version $Rev: 638218 $, $Date: 2008-03-18 06:07:20 +0100 (Tue, 18 Mar 2008) $
034     */
035    public class ItemPermission extends Permission
036    {
037        private static final long serialVersionUID = 3940100745409337694L;
038    
039        private final Collection<UserClass> userClasses;
040    
041    
042        /**
043         * Creates a new instance
044         * 
045         * @param precedence
046         *            the precedence of this permission (<tt>-1</tt> to use the
047         *            default)
048         * @param grantsAndDenials
049         *            the set of {@link GrantAndDenial}s
050         * @param userClasses
051         *            the collection of {@link UserClass}es
052         */
053        public ItemPermission( int precedence, Collection<GrantAndDenial> grantsAndDenials, Collection<UserClass> userClasses )
054        {
055            super( precedence, grantsAndDenials );
056    
057            this.userClasses = Collections.unmodifiableCollection( new ArrayList<UserClass>( userClasses ) );
058        }
059    
060    
061        /**
062         * Returns the collection of {@link UserClass}es.
063         */
064        public Collection<UserClass> getUserClasses()
065        {
066            return userClasses;
067        }
068    
069    
070        public String toString()
071        {
072            StringBuilder buffer = new StringBuilder();
073            
074            buffer.append( "{ " );
075    
076            if ( getPrecedence() >= 0 && getPrecedence() <= 255 )
077            {
078                buffer.append( "precedence " );
079                buffer.append( getPrecedence() );
080                buffer.append( ", " );
081            }
082            
083            buffer.append( "userClasses { " );
084            
085            boolean isFirst = true;
086            
087            for ( UserClass userClass:userClasses )
088            {
089                if ( isFirst ) 
090                {
091                    isFirst = false;
092                }
093                else
094                {
095                    buffer.append( ", " );
096                }
097    
098                buffer.append( userClass.toString() );
099            }
100            
101            buffer.append( " }, grantsAndDenials { " );
102            
103            isFirst = true;
104            
105            for ( GrantAndDenial grantAndDenial:getGrantsAndDenials() )
106            {
107                if ( isFirst )
108                {
109                    isFirst = false;
110                }
111                else
112                {
113                    buffer.append( ", " );
114                }
115    
116                buffer.append( grantAndDenial.toString() );
117            }
118            
119            buffer.append( " } }" );
120            
121            return buffer.toString();
122        }
123    }