directory-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Emmanuel Lécharny <elecha...@gmail.com>
Subject Re: DN compareTo()
Date Wed, 08 Jul 2015 09:36:01 GMT
Le 08/07/15 10:47, Emmanuel Lécharny a écrit :
> Hi guys,
>
> I'm not sure we have not already implemented a dn.compareTo() method
> somewhere in a utility class...
>
> Just in case, I'll add it to the API. I think the algorithm for such a
> method would be :
>
> - if both dn are equals, return 0
> - if dn1.isDescendant( dn2 ) return 1
> - if dn2.isDescendant( dn1 ) return -1
> - otherwise, find the common parent
>   - if found, compare the first RDN after the parent in both RDN and
> return the result (Rdn has a compareTo method)
>   - otherwise, compare the first RDN of both DN
>
> sounds good ?
FTR, here is the code I'm coming with (DnWrapper is just a wrapper
around a DN, just consider it's a DN) :

    public int compareTo( DnWrapper that )
    {
        if ( that == null )
        {
            return 1;
        }

        if ( dn.equals( that.dn ) )
        {
            return 0;
        }
       
        if ( dn.isDescendantOf( that.dn ) )
        {
            return 1;
        }
        else if ( that.dn.isDescendantOf( dn ) )
        {
            return -1;
        }
        else
        {
            // Find the common ancestor, if any
            int upperBound = Math.min( dn.size(), that.dn.size() );
            int result = 0;
           
            for ( int i = 0; i < upperBound; i++ )
            {
                result = dn.getRdn( i ).compareTo( that.dn.getRdn( i ) );
               
                if ( result != 0 )
                {
                    return result;
                }
            }
            // We have exhausted one of the DN
            if ( dn.size() > upperBound )
            {
                return 1;
            }
            else
            {
                return -1;
            }
        }
    }


Mime
View raw message