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;
028
029
030/**
031 * Restricts the maximum number of attribute values allowed for a specified
032 * attribute type. It is examined if the protected item is an attribute
033 * value of the specified type and the permission sought is add. Values of
034 * that attribute in the entry are counted without regard to context or
035 * access control and as though the operation which adds the values were
036 * successful. If the number of values in the attribute exceeds maxCount,
037 * the ACI item is treated as not granting add access.
038 * 
039 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
040 */
041public class MaxValueCountItem extends ProtectedItem
042{
043    /** The set of elements to protect */
044    private final Set<MaxValueCountElem> items;
045
046    /**
047     * Creates a new instance.
048     * 
049     * @param items the collection of {@link MaxValueCountElem}s.
050     */
051    public MaxValueCountItem( Set<MaxValueCountElem> items )
052    {
053        this.items = Collections.unmodifiableSet( items );
054    }
055
056
057    /**
058     * Gets an iterator of all {@link MaxValueCountElem}s.
059     *
060     * @return an iterator of all {@link MaxValueCountElem}s
061     */
062    public Iterator<MaxValueCountElem> iterator()
063    {
064        return items.iterator();
065    }
066
067
068    /**
069     * {@inheritDoc}
070     */
071    @Override
072    public int hashCode()
073    {
074        int hash = 37;
075        
076        if ( items != null )
077        {
078            for ( MaxValueCountElem item : items )
079            {
080                if ( item != null )
081                {
082                    hash = hash * 17 + item.hashCode();
083                }
084                else
085                {
086                    hash = hash * 17 + 37;
087                }
088            }
089        }
090        
091        return hash;
092    }
093
094
095    /**
096     * {@inheritDoc}
097     */
098    @Override
099    public boolean equals( Object o )
100    {
101        if ( this == o )
102        {
103            return true;
104        }
105
106        if ( o instanceof MaxValueCountItem )
107        {
108            MaxValueCountItem that = ( MaxValueCountItem ) o;
109            
110            return items.equals( that.items );
111        }
112
113        return false;
114    }
115
116
117    /**
118     * {@inheritDoc}
119     */
120    @Override
121    public String toString()
122    {
123        StringBuilder buf = new StringBuilder();
124
125        buf.append( "maxValueCount {" );
126
127        boolean isFirst = true;
128
129        if ( items != null )
130        {
131            for ( MaxValueCountElem item : items )
132            {
133                if ( isFirst )
134                {
135                    isFirst = false;
136                }
137                else
138                {
139                    buf.append( ", " );
140                }
141    
142                buf.append( item.toString() );
143            }
144        }
145
146        buf.append( "}" );
147
148        return buf.toString();
149    }
150}