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