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 */
034public class AttributeValueItem extends ProtectedItem
035{
036    /** The protected Attributes */
037    private final Set<Attribute> attributes;
038
039
040    /**
041     * Creates a new instance.
042     * 
043     * @param attributes the collection of {@link Attribute}s.
044     */
045    public AttributeValueItem( Set<Attribute> attributes )
046    {
047        this.attributes = Collections.unmodifiableSet( attributes );
048    }
049
050
051    /**
052     * Returns an iterator of all {@link org.apache.directory.api.ldap.model.entry.Attribute}s.
053     *
054     * @return the iterator
055     */
056    public Iterator<Attribute> iterator()
057    {
058        return attributes.iterator();
059    }
060
061
062    /**
063     * {@inheritDoc}
064     */
065    @Override
066    public int hashCode()
067    {
068        int hash = 37;
069        hash = hash * 17 + attributes.hashCode();
070        return hash;
071    }
072
073
074    /**
075     * {@inheritDoc}
076     */
077    @Override
078    public boolean equals( Object o )
079    {
080        if ( this == o )
081        {
082            return true;
083        }
084
085        if ( o == null )
086        {
087            return false;
088        }
089
090        if ( o instanceof AttributeValueItem )
091        {
092            AttributeValueItem that = ( AttributeValueItem ) o;
093
094            return this.attributes.equals( that.attributes );
095        }
096
097        return false;
098    }
099
100
101    /**
102     * {@inheritDoc}
103     */
104    public String toString()
105    {
106        StringBuilder buf = new StringBuilder();
107
108        buf.append( "attributeValue {" );
109
110        boolean isFirst = true;
111
112        for ( Attribute attribute : attributes )
113        {
114            if ( isFirst )
115            {
116                isFirst = false;
117            }
118            else
119            {
120                buf.append( ", " );
121            }
122
123            buf.append( attribute.getId() );
124            buf.append( '=' );
125            buf.append( attribute.get() );
126        }
127
128        buf.append( " }" );
129
130        return buf.toString();
131    }
132}