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.model.schema.comparators;
021
022
023import org.apache.directory.shared.i18n.I18n;
024import org.apache.directory.shared.ldap.model.exception.LdapException;
025import org.apache.directory.shared.ldap.model.exception.LdapInvalidDnException;
026import org.apache.directory.shared.ldap.model.name.Dn;
027import org.apache.directory.shared.ldap.model.schema.LdapComparator;
028import org.apache.directory.shared.ldap.model.schema.SchemaManager;
029
030
031/**
032 * Compare two DNs
033 * 
034 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
035 */
036public class DnComparator extends LdapComparator<Object>
037{
038    /** The serial version UID */
039    private static final long serialVersionUID = 2L;
040
041    /** A reference to the schema manager */ 
042    private SchemaManager schemaManager;
043    
044    public DnComparator( String oid )
045    {
046        super( oid );
047    }
048    
049    
050    /**
051     * {@inheritDoc}
052     */
053    public int compare( Object obj0, Object obj1 ) 
054    {
055        Dn dn0 = null;
056        Dn dn1 = null;
057        
058        try 
059        {
060            dn0 = getDn( obj0 );
061            dn1 = getDn( obj1 );
062        }
063        catch ( LdapException e )
064        {
065            // -- what do we do here ?
066            return -1;
067        }
068        
069        if ( dn0.equals( dn1 ) )
070        {
071            return 0;
072        }
073        else
074        {
075            return -1;
076        }
077    }
078
079
080    private Dn getDn( Object obj ) throws LdapInvalidDnException
081    {
082        Dn dn = null;
083        
084        if ( obj instanceof Dn)
085        {
086            dn = (Dn)obj;
087            
088            dn = ( dn.isSchemaAware() ? dn : dn.apply( schemaManager ) );
089        }
090        else if ( obj instanceof String )
091        {
092            dn = new Dn( schemaManager, ( String ) obj );
093        }
094        else
095        {
096            throw new IllegalStateException( I18n.err( I18n.ERR_04218, (obj == null ? null : obj.getClass() ) ) );
097        }
098        
099        return dn;
100    }
101
102
103    /**
104     * {@inheritDoc}
105     */
106    public void setSchemaManager( SchemaManager schemaManager )
107    {
108        this.schemaManager = schemaManager;
109    }
110}