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.shared.ldap.aci.protectedItem;
021
022
023import java.util.Collections;
024import java.util.Iterator;
025import java.util.Set;
026
027import org.apache.directory.shared.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 */
040public class RestrictedByItem extends ProtectedItem
041{
042    /** The set of restricted elements */
043    private final Set<RestrictedByElem> items;
044
045
046    /**
047     * Creates a new instance.
048     * 
049     * @param items the collection of {@link RestrictedByElem}s.
050     */
051    public RestrictedByItem( Set<RestrictedByElem> items )
052    {
053        this.items = Collections.unmodifiableSet( items );
054    }
055
056
057    /**
058     * Gets an iterator of all {@link RestrictedByElem}s.
059     *
060     * @return the iterator of all {@link RestrictedByElem}s
061     */
062    public Iterator<RestrictedByElem> 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        hash = hash * 17 + items.hashCode();
076        return hash;
077    }
078
079
080    /**
081     * {@inheritDoc}
082     */
083    @Override
084    public boolean equals( Object o )
085    {
086        if ( this == o )
087        {
088            return true;
089        }
090
091        if ( o == null )
092        {
093            return false;
094        }
095
096        if ( o instanceof RestrictedByItem )
097        {
098            RestrictedByItem that = ( RestrictedByItem ) o;
099            return this.items.equals( that.items );
100        }
101
102        return false;
103    }
104
105
106    /**
107     * {@inheritDoc}
108     */
109    @Override
110    public String toString()
111    {
112        StringBuilder buf = new StringBuilder();
113
114        buf.append( "restrictedBy {" );
115
116        boolean isFirst = true;
117
118        for ( RestrictedByElem item : items )
119        {
120            if ( isFirst )
121            {
122                isFirst = false;
123            }
124            else
125            {
126                buf.append( ", " );
127            }
128
129            buf.append( item.toString() );
130        }
131
132        buf.append( '}' );
133
134        return buf.toString();
135    }
136}