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