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.Collection;
024    import java.util.Collections;
025    
026    
027    /**
028     * Represents permissions to be applied to all {@link UserClass}es in
029     * {@link UserFirstACIItem}.
030     * 
031     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
032     * @version $Rev: 664290 $, $Date: 2008-06-07 08:28:06 +0200 (Sat, 07 Jun 2008) $
033     */
034    public class UserPermission extends Permission
035    {
036        private static final long serialVersionUID = 3940100745409337694L;
037    
038        private final Collection<ProtectedItem> protectedItems;
039    
040    
041        /**
042         * Creates a new instance
043         * 
044         * @param precedence
045         *            the precedence of this permission (<tt>-1</tt> to use the
046         *            default)
047         * @param grantsAndDenials
048         *            the set of {@link GrantAndDenial}s
049         * @param protectedItems
050         *            the collection of {@link ProtectedItem}s
051         */
052        public UserPermission( int precedence, Collection<GrantAndDenial> grantsAndDenials, Collection<ProtectedItem> protectedItems )
053        {
054            super( precedence, grantsAndDenials );
055    
056            this.protectedItems = Collections.unmodifiableCollection( protectedItems );
057        }
058    
059    
060        /**
061         * Returns the collection of {@link ProtectedItem}s.
062         */
063        public Collection<ProtectedItem> getProtectedItems()
064        {
065            return protectedItems;
066        }
067    
068    
069        public String toString()
070        {
071            StringBuilder buf = new StringBuilder();
072            
073            buf.append( "{ " );
074    
075            if ( getPrecedence() >= 0 && getPrecedence() <= 255 )
076            {
077                buf.append( "precedence " );
078                buf.append( getPrecedence() );
079                buf.append( ", " );
080            }
081            
082            buf.append( "protectedItems { " );
083            
084            boolean isFirst = true;
085            
086            for ( ProtectedItem item:protectedItems )
087            {
088                if ( isFirst )
089                {
090                    isFirst = false;
091                }
092                else
093                {
094                    buf.append( ", " );
095                }
096                
097                buf.append( item.toString() );
098            }
099            
100            buf.append( " }, grantsAndDenials { " );
101    
102            isFirst = true;
103            
104            for ( GrantAndDenial grantAndDenial:getGrantsAndDenials() )
105            {
106                if ( isFirst )
107                {
108                    isFirst = false;
109                }
110                else
111                {
112                    buf.append( ", " );
113                }
114    
115                buf.append( grantAndDenial.toString() );
116            }
117            
118            buf.append( " } }" );
119            
120            return buf.toString();
121        }
122    }