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  import org.apache.directory.api.ldap.model.schema.AttributeType;
29  
30  
31  /**
32   * A base class for all items which protects attribute types (or its values)
33   */
34  public abstract class AbstractAttributeTypeProtectedItem extends ProtectedItem
35  {
36      /** The attribute types. */
37      protected final Set<AttributeType> attributeTypes;
38  
39  
40      /**
41       * Creates a new instance.
42       * 
43       * @param attributeTypes the collection of attribute IDs
44       */
45      protected AbstractAttributeTypeProtectedItem( Set<AttributeType> attributeTypes )
46      {
47          this.attributeTypes = Collections.unmodifiableSet( attributeTypes );
48      }
49  
50  
51      /**
52       * Gets an iterator of all attribute types.
53       *
54       * @return the iterator of all attribute types
55       */
56      public Iterator<AttributeType> iterator()
57      {
58          return attributeTypes.iterator();
59      }
60  
61  
62      /**
63       * {@inheritDoc}
64       */
65      @Override
66      public int hashCode()
67      {
68          int hash = 37;
69          hash = hash * 17 + attributeTypes.hashCode();
70          return hash;
71      }
72  
73  
74      /**
75       * {@inheritDoc}
76       */
77      @Override
78      public boolean equals( Object o )
79      {
80          if ( this == o )
81          {
82              return true;
83          }
84  
85          if ( o == null )
86          {
87              return false;
88          }
89  
90          if ( getClass().isAssignableFrom( o.getClass() ) )
91          {
92              AbstractAttributeTypeProtectedItem that = ( AbstractAttributeTypeProtectedItem ) o;
93              return this.attributeTypes.equals( that.attributeTypes );
94          }
95  
96          return false;
97      }
98  
99  
100     /**
101      * {@inheritDoc}
102      */
103     @Override
104     public String toString()
105     {
106         StringBuilder buf = new StringBuilder();
107 
108         buf.append( "{ " );
109         boolean isFirst = true;
110 
111         for ( AttributeType attributeType : attributeTypes )
112         {
113             if ( isFirst )
114             {
115                 isFirst = false;
116             }
117             else
118             {
119                 buf.append( ", " );
120             }
121 
122             buf.append( attributeType.getName() );
123         }
124 
125         buf.append( " }" );
126 
127         return buf.toString();
128     }
129 }