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.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 ParsedDnComparator 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 transient SchemaManager schemaManager;
043
044    /**
045     * The ParsedDnComparator constructor.
046     * 
047     * @param oid The Comparator's OID
048     */
049    public ParsedDnComparator( String oid )
050    {
051        super( oid );
052    }
053
054
055    /**
056     * {@inheritDoc}
057     */
058    @Override
059    public int compare( Object obj0, Object obj1 )
060    {
061        Dn dn0 = null;
062        Dn dn1 = null;
063
064        try
065        {
066            dn0 = getDn( obj0 );
067            dn1 = getDn( obj1 );
068        }
069        catch ( LdapException e )
070        {
071            // -- what do we do here ?
072            return -1;
073        }
074
075        int dn0Size = dn0.getRdns().size();
076        int dn1Size = dn1.getRdns().size();
077        
078        // check the equality first, cause
079        // when both DNs are equal checking isAncestorOf() returns true
080        if ( dn0.equals( dn1 ) )
081        {
082            return 0;
083        }
084        else if ( dn0Size > dn1Size )
085        {
086            return -1;
087        }
088        else if ( dn1Size > dn0Size )
089        {
090            return 1;
091        }
092
093        for ( int i = dn0Size - 1; i >= 0; i-- )
094        {
095            int comp = dn0.getRdn( i ).compareTo( dn1.getRdn( i ) );
096            
097            if ( comp != 0 )
098            {
099                return comp;
100            }
101        }
102        
103        return 0;
104    }
105
106
107    private Dn getDn( Object obj ) throws LdapInvalidDnException
108    {
109        Dn dn;
110
111        if ( obj instanceof Dn )
112        {
113            dn = ( Dn ) obj;
114
115            dn = dn.isSchemaAware() ? dn : new Dn( schemaManager, dn );
116        }
117        else if ( obj instanceof String )
118        {
119            dn = new Dn( schemaManager, ( String ) obj );
120        }
121        else
122        {
123            throw new IllegalStateException( I18n.err( I18n.ERR_13720_CANNOT_HANDLE_DN_COMPARISONS, obj == null ? null : obj.getClass() ) );
124        }
125
126        return dn;
127    }
128
129
130    /**
131     * {@inheritDoc}
132     */
133    @Override
134    public void setSchemaManager( SchemaManager schemaManager )
135    {
136        this.schemaManager = schemaManager;
137    }
138}