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.schema.AttributeType;
029
030
031/**
032 * A base class for all items which protects attribute types (or its values)
033 * 
034 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
035 */
036public abstract class AbstractAttributeTypeProtectedItem extends ProtectedItem
037{
038    /** The attribute types. */
039    protected final Set<AttributeType> attributeTypes;
040
041    /**
042     * Creates a new instance.
043     * 
044     * @param attributeTypes the collection of attribute IDs
045     */
046    protected AbstractAttributeTypeProtectedItem( Set<AttributeType> attributeTypes )
047    {
048        this.attributeTypes = Collections.unmodifiableSet( attributeTypes );
049    }
050
051
052    /**
053     * Gets an iterator of all attribute types.
054     *
055     * @return the iterator of all attribute types
056     */
057    public Iterator<AttributeType> iterator()
058    {
059        return attributeTypes.iterator();
060    }
061
062
063    /**
064     * {@inheritDoc}
065     */
066    @Override
067    public int hashCode()
068    {
069        int hash = 37;
070        
071        if ( attributeTypes != null )
072        {
073            for ( AttributeType attributeType : attributeTypes )
074            {
075                hash = hash * 17 + attributeType.hashCode();
076            }
077        }
078        
079        hash = hash * 17 + getClass().getName().hashCode();
080
081        return hash;
082    }
083
084
085    /**
086     * {@inheritDoc}
087     */
088    @Override
089    public boolean equals( Object o )
090    {
091        if ( this == o )
092        {
093            return true;
094        }
095
096        if ( o == null )
097        {
098            return false;
099        }
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}