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.io.Serializable;
024    import java.util.Collection;
025    import java.util.Collections;
026    import java.util.HashSet;
027    import java.util.Set;
028    
029    
030    /**
031     * An abstract base class for {@link ItemPermission} and {@link UserPermission}.
032     * 
033     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
034     * @version $Rev: 572194 $, $Date: 2007-09-03 01:58:18 +0200 (Mon, 03 Sep 2007) $
035     */
036    public abstract class Permission implements Serializable
037    {
038        private final int precedence;
039    
040        private final Set<GrantAndDenial> grantsAndDenials;
041    
042        private final Set<GrantAndDenial> grants;
043    
044        private final Set<GrantAndDenial> denials;
045    
046    
047        /**
048         * Creates a new instance
049         * 
050         * @param precedence
051         *            the precedence of this permission (<tt>-1</tt> to use the
052         *            default)
053         * @param grantsAndDenials
054         *            the set of {@link GrantAndDenial}s
055         */
056        protected Permission(int precedence, Collection<GrantAndDenial> grantsAndDenials)
057        {
058            if ( precedence < 0 || precedence > 255 )
059            {
060                precedence = -1;
061            }
062    
063            this.precedence = precedence;
064    
065            Set<GrantAndDenial> tmpGrantsAndDenials = new HashSet<GrantAndDenial>();
066            Set<GrantAndDenial> tmpGrants = new HashSet<GrantAndDenial>();
067            Set<GrantAndDenial> tmpDenials = new HashSet<GrantAndDenial>();
068            
069            for ( GrantAndDenial gad:grantsAndDenials )
070            {
071                if ( gad.isGrant() )
072                {
073                    tmpGrants.add( gad );
074                }
075                else
076                {
077                    tmpDenials.add( gad );
078                }
079    
080                tmpGrantsAndDenials.add( gad );
081            }
082    
083            this.grants = Collections.unmodifiableSet( tmpGrants );
084            this.denials = Collections.unmodifiableSet( tmpDenials );
085            this.grantsAndDenials = Collections.unmodifiableSet( tmpGrantsAndDenials );
086        }
087    
088    
089        /**
090         * Returns the precedence of this permission.
091         */
092        public int getPrecedence()
093        {
094            return precedence;
095        }
096    
097    
098        /**
099         * Returns the set of {@link GrantAndDenial}s.
100         */
101        public Set<GrantAndDenial> getGrantsAndDenials()
102        {
103            return grantsAndDenials;
104        }
105    
106    
107        /**
108         * Returns the set of grants only.
109         */
110        public Set<GrantAndDenial> getGrants()
111        {
112            return grants;
113        }
114    
115    
116        /**
117         * Returns the set of denials only.
118         */
119        public Set<GrantAndDenial> getDenials()
120        {
121            return denials;
122        }
123    }