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 */
020package org.apache.directory.api.ldap.aci;
021
022
023import java.util.ArrayList;
024import java.util.Collection;
025import 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 */
034public class ItemPermission extends Permission
035{
036    /** The user classes. */
037    private final Collection<UserClass> userClasses;
038
039
040    /**
041     * Creates a new instance
042     * 
043     * @param precedence
044     *            the precedence of this permission (<tt>-1</tt> to use the
045     *            default)
046     * @param grantsAndDenials
047     *            the collection of {@link GrantAndDenial}s
048     * @param userClasses
049     *            the collection of {@link UserClass}es
050     */
051    public ItemPermission( Integer precedence, Collection<GrantAndDenial> grantsAndDenials,
052        Collection<UserClass> userClasses )
053    {
054        super( precedence, grantsAndDenials );
055
056        this.userClasses = Collections.unmodifiableCollection( new ArrayList<UserClass>( userClasses ) );
057    }
058
059
060    /**
061     * Gets the collection of {@link UserClass}es.
062     *
063     * @return the collection of {@link UserClass}es
064     */
065    public Collection<UserClass> getUserClasses()
066    {
067        return userClasses;
068    }
069
070
071    /**
072     * {@inheritDoc}
073     */
074    @Override
075    public String toString()
076    {
077        StringBuilder buffer = new StringBuilder();
078
079        buffer.append( "{ " );
080
081        if ( getPrecedence() != null )
082        {
083            buffer.append( "precedence " );
084            buffer.append( getPrecedence() );
085            buffer.append( ", " );
086        }
087
088        buffer.append( "userClasses { " );
089
090        boolean isFirst = true;
091
092        for ( UserClass userClass : userClasses )
093        {
094            if ( isFirst )
095            {
096                isFirst = false;
097            }
098            else
099            {
100                buffer.append( ", " );
101            }
102
103            buffer.append( userClass.toString() );
104        }
105
106        buffer.append( " }, grantsAndDenials { " );
107
108        isFirst = true;
109
110        for ( GrantAndDenial grantAndDenial : getGrantsAndDenials() )
111        {
112            if ( isFirst )
113            {
114                isFirst = false;
115            }
116            else
117            {
118                buffer.append( ", " );
119            }
120
121            buffer.append( grantAndDenial.toString() );
122        }
123
124        buffer.append( " } }" );
125
126        return buffer.toString();
127    }
128}