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