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.comparators;
021
022
023import org.apache.directory.api.i18n.I18n;
024import org.apache.directory.api.ldap.model.exception.LdapException;
025import org.apache.directory.api.ldap.model.exception.LdapInvalidDnException;
026import org.apache.directory.api.ldap.model.name.Dn;
027import org.apache.directory.api.ldap.model.schema.LdapComparator;
028import org.apache.directory.api.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        int dn0Size = dn0.getRdns().size();
070        int dn1Size = dn1.getRdns().size();
071        
072        // check the equality first, cause
073        // when both DNs are equal checking isAncestorOf() returns true
074        if ( dn0.equals( dn1 ) )
075        {
076            return 0;
077        }
078        else if ( dn0Size > dn1Size )
079        {
080            return -1;
081        }
082        else if ( dn1Size > dn0Size )
083        {
084            return 1;
085        }
086
087        return dn0.getNormName().compareTo( dn1.getNormName() );
088    }
089
090
091    private Dn getDn( Object obj ) throws LdapInvalidDnException
092    {
093        Dn dn = null;
094
095        if ( obj instanceof Dn )
096        {
097            dn = ( Dn ) obj;
098
099            dn = ( dn.isSchemaAware() ? dn : dn.apply( schemaManager ) );
100        }
101        else if ( obj instanceof String )
102        {
103            dn = new Dn( schemaManager, ( String ) obj );
104        }
105        else
106        {
107            throw new IllegalStateException( I18n.err( I18n.ERR_04218, ( obj == null ? null : obj.getClass() ) ) );
108        }
109
110        return dn;
111    }
112
113
114    /**
115     * {@inheritDoc}
116     */
117    public void setSchemaManager( SchemaManager schemaManager )
118    {
119        this.schemaManager = schemaManager;
120    }
121}