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.model.schema;
021
022
023/**
024 * A class containing a SchemaObject, used by the global registries. As the hash code
025 * method of the SchemaObject class is too complex, we had to define a simplest class
026 * for this purpose, where the hash code is computed using only the SchemaObject
027 * type and its OID.
028 *
029 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
030 */
031public class SchemaObjectWrapper
032{
033    /** The internal schemaObject */
034    private SchemaObject schemaObject;
035
036
037    /**
038     * Creates a new instance of SchemaObjectWrapper.
039     *
040     * @param schemaObject The contained SchemaObject
041     */
042    public SchemaObjectWrapper( SchemaObject schemaObject )
043    {
044        this.schemaObject = schemaObject;
045    }
046
047
048    /**
049     * Compute the hash code for this wrapper. We only use the object type
050     * and its oid.
051     */
052    public int hashCode()
053    {
054        int h = 37;
055        h += h * 17 + schemaObject.getObjectType().getValue();
056
057        if ( schemaObject.getOid() != null )
058        {
059            h += h * 17 + schemaObject.getOid().hashCode();
060        }
061
062        return h;
063    }
064
065
066    /**
067     * @see Object#equals(Object)
068     */
069    public boolean equals( Object o )
070    {
071        if ( o == this )
072        {
073            return true;
074        }
075
076        if ( !( o instanceof SchemaObjectWrapper ) )
077        {
078            return false;
079        }
080
081        SchemaObject that = ( ( SchemaObjectWrapper ) o ).get();
082        SchemaObject current = get();
083
084        // Ultimately, that has to be true, regardless of the OID value
085        if ( that.getObjectType() != current.getObjectType() )
086        {
087            return false;
088        }
089
090        // If both OID are null, instances are equals
091        if ( that.getOid() == null )
092        {
093            return ( current.getOid() == null );
094        }
095
096        // The that'oid will never be null, we don't really care if current.oid is null here.
097        return that.getOid().equals( current.getOid() );
098    }
099
100
101    /**
102     *  @return The interned SchemaObject
103     */
104    public SchemaObject get()
105    {
106        return schemaObject;
107    }
108
109
110    /**
111     * @see Object#toString()
112     */
113    public String toString()
114    {
115        return "<" + schemaObject.getObjectType() + "," + schemaObject.getOid() + ">";
116    }
117}