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 org.apache.directory.api.ldap.model.schema.AttributeType;
024
025
026/**
027 * An element of {@link RestrictedByItem}.
028 * 
029 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
030 */
031public class RestrictedByElem
032{
033    /** The AttributeType on which the restriction is applied */
034    private AttributeType attributeType;
035
036    /** The list of allowed AttributeType values */
037    private AttributeType valuesIn;
038
039    /**
040     * Creates a new instance.
041     * 
042     * @param attributeType the attribute type to restrict
043     * @param valuesIn the attribute type only whose values are allowed in <tt>attributeType</tt>.
044     */
045    public RestrictedByElem( AttributeType attributeType, AttributeType valuesIn )
046    {
047        this.attributeType = attributeType;
048        this.valuesIn = valuesIn;
049    }
050
051
052    /**
053     * Gets the attribute type to restrict.
054     *
055     * @return the attribute type
056     */
057    public AttributeType getAttributeType()
058    {
059        return attributeType;
060    }
061
062
063    /**
064     * Gets the attribute type only whose values are allowed in
065     * <tt>attributeType</tt>.
066     *
067     * @return the list of allowed AttributeType values
068     */
069    public AttributeType getValuesIn()
070    {
071        return valuesIn;
072    }
073
074
075    /**
076     * {@inheritDoc}
077     */
078    @Override
079    public int hashCode()
080    {
081        int hash = 37;
082        
083        if ( attributeType != null )
084        {
085            hash = hash * 17 + attributeType.hashCode();
086        }
087        
088        if ( valuesIn != null )
089        {
090            hash = hash * 17 + valuesIn.hashCode();
091        }
092        
093        return hash;
094    }
095
096
097    /**
098     * {@inheritDoc}
099     */
100    @Override
101    public boolean equals( Object o )
102    {
103        if ( this == o )
104        {
105            return true;
106        }
107
108        if ( o instanceof RestrictedByElem )
109        {
110            RestrictedByElem that = ( RestrictedByElem ) o;
111            
112            if ( attributeType == null )
113            {
114                if ( that.attributeType == null )
115                {
116                    if ( valuesIn == null )
117                    {
118                        return that.valuesIn == null;
119                    }
120                    else
121                    {
122                        return valuesIn.equals( that.valuesIn );
123                    }
124                }
125            }
126            else
127            {
128                if ( attributeType.equals( that.attributeType ) )
129                {
130                    if ( valuesIn == null )
131                    {
132                        return that.valuesIn == null;
133                    }
134                    else
135                    {
136                        return valuesIn.equals( that.valuesIn );
137                    }
138                }
139            }
140        }
141        
142        return false;
143    }
144
145
146    /**
147     * {@inheritDoc}
148     */
149    @Override
150    public String toString()
151    {
152        StringBuilder sb = new StringBuilder();
153        
154        sb.append( "{ type " );
155        
156        if ( attributeType != null )
157        {
158            sb.append( attributeType.getName() );
159        }
160        else
161        {
162            sb.append( "null" );
163        }
164        
165        sb.append( ", valuesIn " );
166
167        if ( valuesIn != null )
168        {
169            sb.append( valuesIn.getName() );
170        }
171        else
172        {
173            sb.append( "null" );
174        }
175        
176        sb.append( "}" );
177        
178        return sb.toString();
179    }
180}