On Wed, Jul 8, 2015 at 5:36 PM, Emmanuel Lécharny <[email protected]>
wrote:
> 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;
> }
>
can we change the return types here to -1 and 1, so that the DNs are
ordered in perfect hierarchy
this is useful for exporting/importing LDIF
> else
> {
> // Find the common ancestor, if any
> int upperBound = Math.min( dn.size(), that.dn.size() );
>
perfect, the existing size check in the comparator is flawed (as mentioned
in the other mail)
> 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;
> }
>
}
> }
>
>
--
Kiran Ayyagari
http://keydap.com