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 *    https://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
023import java.io.Serializable;
024import java.util.Comparator;
025
026import org.apache.directory.api.ldap.model.schema.normalizers.NoOpNormalizer;
027
028
029/**
030 * An class used for Comparator. It inherits from the general AbstractAdsSchemaObject class. It
031 * also implements the Comparator interface
032 * 
033 * @param <T> The comparator type
034 * 
035 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
036 */
037public abstract class LdapComparator<T> extends LoadableSchemaObject implements Comparator<T>, Serializable
038{
039    /** The serial version UID */
040    private static final long serialVersionUID = 2L;
041
042    /** A default normalizer*/
043    protected Normalizer normalizer = new NoOpNormalizer();
044
045
046    /**
047     * Create a new instance of a Comparator
048     * @param oid The associated OID
049     */
050    protected LdapComparator( String oid )
051    {
052        super( SchemaObjectType.COMPARATOR, oid );
053    }
054
055
056    /**
057     * Store the SchemaManager in this instance. It may be necessary for some
058     * comparator which needs to have access to the oidNormalizer Map.
059     *
060     * @param schemaManager the schemaManager to store
061     */
062    public void setSchemaManager( SchemaManager schemaManager )
063    {
064        // Do nothing (general case).
065    }
066    
067    
068    /**
069     * {@inheritDoc}
070     */
071    @Override
072    public int hashCode()
073    {
074        int hash = h;
075        
076        if ( normalizer != null )
077        {
078            hash = hash * 17 + normalizer.hashCode();
079        }
080        
081        return hash;
082    }
083
084
085    /**
086     * {@inheritDoc}
087     */
088    @Override
089    public boolean equals( Object o )
090    {
091        if ( !super.equals( o ) )
092        {
093            return false;
094        }
095        
096        if ( !( o instanceof LdapComparator<?> ) )
097        {
098            return false;
099        }
100        
101        LdapComparator<?> that = ( LdapComparator<?> ) o; 
102        
103        // Compare the normalizer
104        if ( normalizer != null )
105        {
106            return normalizer.equals( that.getNormalizer() );
107        }
108        else 
109        {
110            return that.getNormalizer() == null;
111        }
112    }
113    
114    
115    /**
116     * @return The associated normalizer
117     */
118    public Normalizer getNormalizer()
119    {
120        return normalizer;
121    }
122
123
124    /**
125     * {@inheritDoc}
126     */
127    @Override
128    public String toString()
129    {
130        return objectType + " " + DescriptionUtils.getDescription( this );
131    }
132}