View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *  
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *  
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
18   *  
19   */
20  package org.apache.directory.api.ldap.aci;
21  
22  
23  import java.util.Collection;
24  import java.util.Collections;
25  import java.util.HashSet;
26  import java.util.Set;
27  
28  
29  /**
30   * An abstract base class for {@link ItemPermission} and {@link UserPermission}.
31   * 
32   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
33   */
34  public abstract class Permission
35  {
36      /** The precedence. */
37      private final Integer precedence;
38  
39      /** The grants and denials. */
40      private final Set<GrantAndDenial> grantsAndDenials;
41  
42      /** The grants. */
43      private final Set<GrantAndDenial> grants;
44  
45      /** The denials. */
46      private final Set<GrantAndDenial> denials;
47  
48  
49      /**
50       * Creates a new instance
51       * 
52       * @param precedence
53       *            the precedence of this permission (<tt>-1</tt> to use the
54       *            default)
55       * @param grantsAndDenials
56       *            the set of {@link GrantAndDenial}s
57       */
58      protected Permission( Integer precedence, Collection<GrantAndDenial> grantsAndDenials )
59      {
60          this.precedence = precedence;
61  
62          Set<GrantAndDenial> tmpGrantsAndDenials = new HashSet<GrantAndDenial>();
63          Set<GrantAndDenial> tmpGrants = new HashSet<GrantAndDenial>();
64          Set<GrantAndDenial> tmpDenials = new HashSet<GrantAndDenial>();
65  
66          for ( GrantAndDenial gad : grantsAndDenials )
67          {
68              if ( gad.isGrant() )
69              {
70                  tmpGrants.add( gad );
71              }
72              else
73              {
74                  tmpDenials.add( gad );
75              }
76  
77              tmpGrantsAndDenials.add( gad );
78          }
79  
80          this.grants = Collections.unmodifiableSet( tmpGrants );
81          this.denials = Collections.unmodifiableSet( tmpDenials );
82          this.grantsAndDenials = Collections.unmodifiableSet( tmpGrantsAndDenials );
83      }
84  
85  
86      /**
87       * Gets the precedence of this permission.
88       *
89       * @return the precedence
90       */
91      public Integer getPrecedence()
92      {
93          return precedence;
94      }
95  
96  
97      /**
98       * Gets the set of {@link GrantAndDenial}s.
99       *
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 }