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