001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *  
010 *    http://www.apache.org/licenses/LICENSE-2.0
011 *  
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License. 
018 *  
019 */
020package org.apache.directory.api.ldap.aci.protectedItem;
021
022
023import java.util.Collections;
024import java.util.Iterator;
025import java.util.Set;
026
027import org.apache.directory.api.ldap.aci.ProtectedItem;
028import org.apache.directory.api.ldap.model.entry.Attribute;
029
030
031/**
032 * A specific value of specific attributes.
033 * 
034 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
035 */
036public class AttributeValueItem extends ProtectedItem
037{
038    /** The protected Attributes */
039    private final Set<Attribute> attributes;
040
041
042    /**
043     * Creates a new instance.
044     * 
045     * @param attributes the collection of {@link Attribute}s.
046     */
047    public AttributeValueItem( Set<Attribute> attributes )
048    {
049        this.attributes = Collections.unmodifiableSet( attributes );
050    }
051
052
053    /**
054     * Returns an iterator of all {@link org.apache.directory.api.ldap.model.entry.Attribute}s.
055     *
056     * @return the iterator
057     */
058    public Iterator<Attribute> iterator()
059    {
060        return attributes.iterator();
061    }
062
063
064    /**
065     * {@inheritDoc}
066     */
067    @Override
068    public int hashCode()
069    {
070        int hash = 37;
071        
072        if ( attributes != null )
073        {
074            for ( Attribute attribute : attributes )
075            {
076                hash = hash * 17 + attribute.hashCode();
077            }
078        }
079        
080        return hash;
081    }
082
083
084    /**
085     * {@inheritDoc}
086     */
087    @Override
088    public boolean equals( Object o )
089    {
090        if ( this == o )
091        {
092            return true;
093        }
094
095        if ( o instanceof AttributeValueItem )
096        {
097            AttributeValueItem that = ( AttributeValueItem ) o;
098
099            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}