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    @Override
053    public int hashCode()
054    {
055        int h = 37;
056        h += h * 17 + schemaObject.getObjectType().getValue();
057
058        if ( schemaObject.getOid() != null )
059        {
060            h += h * 17 + schemaObject.getOid().hashCode();
061        }
062
063        return h;
064    }
065
066
067    /**
068     * @see Object#equals(Object)
069     */
070    @Override
071    public boolean equals( Object o )
072    {
073        if ( o == this )
074        {
075            return true;
076        }
077
078        if ( !( o instanceof SchemaObjectWrapper ) )
079        {
080            return false;
081        }
082
083        SchemaObject that = ( ( SchemaObjectWrapper ) o ).get();
084        SchemaObject current = get();
085
086        // Ultimately, that has to be true, regardless of the OID value
087        if ( that.getObjectType() != current.getObjectType() )
088        {
089            return false;
090        }
091
092        // If both OID are null, instances are equals
093        if ( that.getOid() == null )
094        {
095            return current.getOid() == null;
096        }
097
098        // The that'oid will never be null, we don't really care if current.oid is null here.
099        return that.getOid().equals( current.getOid() );
100    }
101
102
103    /**
104     *  @return The interned SchemaObject
105     */
106    public SchemaObject get()
107    {
108        return schemaObject;
109    }
110
111
112    /**
113     * @see Object#toString()
114     */
115    @Override
116    public String toString()
117    {
118        return "<" + schemaObject.getObjectType() + "," + schemaObject.getOid() + ">";
119    }
120}