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.protectedItem;
21  
22  
23  import java.util.Collections;
24  import java.util.Iterator;
25  import java.util.Set;
26  
27  import org.apache.directory.api.ldap.aci.ProtectedItem;
28  
29  
30  /**
31   * Restricts the maximum number of attribute values allowed for a specified
32   * attribute type. It is examined if the protected item is an attribute
33   * value of the specified type and the permission sought is add. Values of
34   * that attribute in the entry are counted without regard to context or
35   * access control and as though the operation which adds the values were
36   * successful. If the number of values in the attribute exceeds maxCount,
37   * the ACI item is treated as not granting add access.
38   * 
39   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
40   */
41  public class MaxValueCountItem extends ProtectedItem
42  {
43      /** The set of elements to protect */
44      private final Set<MaxValueCountElem> items;
45  
46      /**
47       * Creates a new instance.
48       * 
49       * @param items the collection of {@link MaxValueCountElem}s.
50       */
51      public MaxValueCountItem( Set<MaxValueCountElem> items )
52      {
53          this.items = Collections.unmodifiableSet( items );
54      }
55  
56  
57      /**
58       * Gets an iterator of all {@link MaxValueCountElem}s.
59       *
60       * @return an iterator of all {@link MaxValueCountElem}s
61       */
62      public Iterator<MaxValueCountElem> iterator()
63      {
64          return items.iterator();
65      }
66  
67  
68      /**
69       * {@inheritDoc}
70       */
71      @Override
72      public int hashCode()
73      {
74          int hash = 37;
75          
76          if ( items != null )
77          {
78              for ( MaxValueCountElem item : items )
79              {
80                  if ( item != null )
81                  {
82                      hash = hash * 17 + item.hashCode();
83                  }
84                  else
85                  {
86                      hash = hash * 17 + 37;
87                  }
88              }
89          }
90          
91          return hash;
92      }
93  
94  
95      /**
96       * {@inheritDoc}
97       */
98      @Override
99      public boolean equals( Object o )
100     {
101         if ( this == o )
102         {
103             return true;
104         }
105 
106         if ( o instanceof MaxValueCountItem )
107         {
108             MaxValueCountItem that = ( MaxValueCountItem ) o;
109             
110             return items.equals( that.items );
111         }
112 
113         return false;
114     }
115 
116 
117     /**
118      * {@inheritDoc}
119      */
120     @Override
121     public String toString()
122     {
123         StringBuilder buf = new StringBuilder();
124 
125         buf.append( "maxValueCount {" );
126 
127         boolean isFirst = true;
128 
129         if ( items != null )
130         {
131             for ( MaxValueCountElem item : items )
132             {
133                 if ( isFirst )
134                 {
135                     isFirst = false;
136                 }
137                 else
138                 {
139                     buf.append( ", " );
140                 }
141     
142                 buf.append( item.toString() );
143             }
144         }
145 
146         buf.append( "}" );
147 
148         return buf.toString();
149     }
150 }