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 */
034public abstract class AbstractAttributeTypeProtectedItem extends ProtectedItem
035{
036    /** The attribute types. */
037    protected final Set<AttributeType> attributeTypes;
038
039
040    /**
041     * Creates a new instance.
042     * 
043     * @param attributeTypes the collection of attribute IDs
044     */
045    protected AbstractAttributeTypeProtectedItem( Set<AttributeType> attributeTypes )
046    {
047        this.attributeTypes = Collections.unmodifiableSet( attributeTypes );
048    }
049
050
051    /**
052     * Gets an iterator of all attribute types.
053     *
054     * @return the iterator of all attribute types
055     */
056    public Iterator<AttributeType> iterator()
057    {
058        return attributeTypes.iterator();
059    }
060
061
062    /**
063     * {@inheritDoc}
064     */
065    @Override
066    public int hashCode()
067    {
068        int hash = 37;
069        hash = hash * 17 + attributeTypes.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 ( getClass().isAssignableFrom( o.getClass() ) )
091        {
092            AbstractAttributeTypeProtectedItem that = ( AbstractAttributeTypeProtectedItem ) o;
093            return this.attributeTypes.equals( that.attributeTypes );
094        }
095
096        return false;
097    }
098
099
100    /**
101     * {@inheritDoc}
102     */
103    @Override
104    public String toString()
105    {
106        StringBuilder buf = new StringBuilder();
107
108        buf.append( "{ " );
109        boolean isFirst = true;
110
111        for ( AttributeType attributeType : attributeTypes )
112        {
113            if ( isFirst )
114            {
115                isFirst = false;
116            }
117            else
118            {
119                buf.append( ", " );
120            }
121
122            buf.append( attributeType.getName() );
123        }
124
125        buf.append( " }" );
126
127        return buf.toString();
128    }
129}