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 values added to the attribute type to being values that are
32   * already present in the same entry as values of the attribute valuesIn. It
33   * is examined if the protected item is an attribute value of the specified
34   * type and the permission sought is add. Values of the valuesIn attribute
35   * are checked without regard to context or access control and as though the
36   * operation which adds the values were successful. If the value to be added
37   * is not present in valuesIn the ACI item is treated as not granting add
38   * access.
39   * 
40   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
41   */
42  public class RestrictedByItem extends ProtectedItem
43  {
44      /** The set of restricted elements */
45      private final Set<RestrictedByElem> items;
46  
47      /**
48       * Creates a new instance.
49       * 
50       * @param items the collection of {@link RestrictedByElem}s.
51       */
52      public RestrictedByItem( Set<RestrictedByElem> items )
53      {
54          this.items = Collections.unmodifiableSet( items );
55      }
56  
57  
58      /**
59       * Gets an iterator of all {@link RestrictedByElem}s.
60       *
61       * @return the iterator of all {@link RestrictedByElem}s
62       */
63      public Iterator<RestrictedByElem> iterator()
64      {
65          return items.iterator();
66      }
67  
68  
69      /**
70       * {@inheritDoc}
71       */
72      @Override
73      public int hashCode()
74      {
75          int hash = 37;
76          
77          if ( items != null )
78          {
79              for ( RestrictedByElem item : items )
80              {
81                  if ( item != null )
82                  {
83                      hash = hash * 17 + item.hashCode();
84                  }
85                  else
86                  {
87                      hash = hash * 17 + 37;
88                  }
89              }
90          }
91  
92          return hash;
93      }
94  
95  
96      /**
97       * {@inheritDoc}
98       */
99      @Override
100     public boolean equals( Object o )
101     {
102         if ( this == o )
103         {
104             return true;
105         }
106 
107         if ( o instanceof RestrictedByItem )
108         {
109             RestrictedByItem that = ( RestrictedByItem ) o;
110             
111             if ( items != null )
112             {
113                 if ( that.items == null )
114                 {
115                     return false;
116                 }
117                 
118                 if ( items.size() != that.items.size() )
119                 {
120                     return false;
121                 }
122                 
123                 for ( RestrictedByElem item : items )
124                 {
125                     if ( !that.items.contains( item ) )
126                     {
127                         return false;
128                     }
129                 }
130                 
131                 return true;
132             }
133             else
134             {
135                 return that.items == null;
136             }
137         }
138 
139         return false;
140     }
141 
142 
143     /**
144      * {@inheritDoc}
145      */
146     @Override
147     public String toString()
148     {
149         StringBuilder buf = new StringBuilder();
150 
151         buf.append( "restrictedBy {" );
152 
153         boolean isFirst = true;
154 
155         if ( items != null )
156         {
157             for ( RestrictedByElem item : items )
158             {
159                 if ( isFirst )
160                 {
161                     isFirst = false;
162                 }
163                 else
164                 {
165                     buf.append( ", " );
166                 }
167     
168                 buf.append( item.toString() );
169             }
170         }
171 
172         buf.append( '}' );
173 
174         return buf.toString();
175     }
176 }