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.ArrayList;
24  import java.util.Collection;
25  import java.util.Collections;
26  
27  
28  /**
29   * Represents permissions to be applied to all {@link ProtectedItem}s in
30   * {@link ItemFirstACIItem}.
31   * 
32   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
33   */
34  public class ItemPermission extends Permission
35  {
36      /** The user classes. */
37      private final Collection<UserClass> userClasses;
38  
39  
40      /**
41       * Creates a new instance
42       * 
43       * @param precedence
44       *            the precedence of this permission (<tt>-1</tt> to use the
45       *            default)
46       * @param grantsAndDenials
47       *            the collection of {@link GrantAndDenial}s
48       * @param userClasses
49       *            the collection of {@link UserClass}es
50       */
51      public ItemPermission( Integer precedence, Collection<GrantAndDenial> grantsAndDenials,
52          Collection<UserClass> userClasses )
53      {
54          super( precedence, grantsAndDenials );
55  
56          this.userClasses = Collections.unmodifiableCollection( new ArrayList<UserClass>( userClasses ) );
57      }
58  
59  
60      /**
61       * Gets the collection of {@link UserClass}es.
62       *
63       * @return the collection of {@link UserClass}es
64       */
65      public Collection<UserClass> getUserClasses()
66      {
67          return userClasses;
68      }
69  
70  
71      /**
72       * {@inheritDoc}
73       */
74      @Override
75      public String toString()
76      {
77          StringBuilder buffer = new StringBuilder();
78  
79          buffer.append( "{ " );
80  
81          if ( getPrecedence() != null )
82          {
83              buffer.append( "precedence " );
84              buffer.append( getPrecedence() );
85              buffer.append( ", " );
86          }
87  
88          buffer.append( "userClasses { " );
89  
90          boolean isFirst = true;
91  
92          for ( UserClass userClass : userClasses )
93          {
94              if ( isFirst )
95              {
96                  isFirst = false;
97              }
98              else
99              {
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 }