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   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
35   */
36  public abstract class AbstractAttributeTypeProtectedItem extends ProtectedItem
37  {
38      /** The attribute types. */
39      protected final Set<AttributeType> attributeTypes;
40  
41      /**
42       * Creates a new instance.
43       * 
44       * @param attributeTypes the collection of attribute IDs
45       */
46      protected AbstractAttributeTypeProtectedItem( Set<AttributeType> attributeTypes )
47      {
48          this.attributeTypes = Collections.unmodifiableSet( attributeTypes );
49      }
50  
51  
52      /**
53       * Gets an iterator of all attribute types.
54       *
55       * @return the iterator of all attribute types
56       */
57      public Iterator<AttributeType> iterator()
58      {
59          return attributeTypes.iterator();
60      }
61  
62  
63      /**
64       * {@inheritDoc}
65       */
66      @Override
67      public int hashCode()
68      {
69          int hash = 37;
70          
71          if ( attributeTypes != null )
72          {
73              for ( AttributeType attributeType : attributeTypes )
74              {
75                  hash = hash * 17 + attributeType.hashCode();
76              }
77          }
78          
79          hash = hash * 17 + getClass().getName().hashCode();
80  
81          return hash;
82      }
83  
84  
85      /**
86       * {@inheritDoc}
87       */
88      @Override
89      public boolean equals( Object o )
90      {
91          if ( this == o )
92          {
93              return true;
94          }
95  
96          if ( o == null )
97          {
98              return false;
99          }
100 
101         if ( getClass().isAssignableFrom( o.getClass() ) )
102         {
103             AbstractAttributeTypeProtectedItem that = ( AbstractAttributeTypeProtectedItem ) o;
104             
105             if ( attributeTypes != null )
106             {
107                 if ( ( that.attributeTypes == null ) || ( that.attributeTypes.size() != attributeTypes.size() ) )
108                 {
109                     return false;
110                 }
111                 
112                 for ( AttributeType attributeType : attributeTypes )
113                 {
114                     if ( !that.attributeTypes.contains( attributeType ) )
115                     {
116                         return false;
117                     }
118                 }
119                 
120                 return true;
121             }
122             else
123             {
124                 return that.attributeTypes == null;
125             }
126         }
127 
128         return false;
129     }
130 
131 
132     /**
133      * {@inheritDoc}
134      */
135     @Override
136     public String toString()
137     {
138         StringBuilder buf = new StringBuilder();
139 
140         buf.append( "{ " );
141         boolean isFirst = true;
142 
143         if ( attributeTypes != null )
144         {
145             for ( AttributeType attributeType : attributeTypes )
146             {
147                 if ( isFirst )
148                 {
149                     isFirst = false;
150                 }
151                 else
152                 {
153                     buf.append( ", " );
154                 }
155     
156                 buf.append( attributeType.getName() );
157             }
158         }
159 
160         buf.append( " }" );
161 
162         return buf.toString();
163     }
164 }