[Date Prev][Date Next] [Chronological] [Thread] [Top]

[ldapext] Java LDAP API, sorting



Rob,

I have been reviewing the changes associated
with the addition of the Java Collections
Framework.  I believe the specification is
incomplete relative to the use of this framework.

Specifically I am looking at how one performs sorts
using the collections framework and have reviewed
the statements in Appendix 9.2 that relate to sorting.

>  LDAPAttributeSet 
>      
>          Implements Cloneable and java.util.Set. Removed the methods

>          made redundant by implementing Set: add(LDAPAttribute attr),

>          elementAt(), getAttributes(), remove(String name), 
>          removeElementAt(), and size().
>  
>  LDAPCompareAttrNames 
>           
>          Implements java.util.Comparator instead of
LDAPEntryComparator.
>  
>  LDAPSearchResults 
>      
>          Does not implement Enumeration. 
>           
>          Removed nextElement(). 
>           
>          Renamed hasMoreElements() to hasMore(). 
>           
>          Removed sort() (sorting can now be done with
classes/interfaces 
>          of the Collections framework now that LDAPAttributeSet 
>          implements Set).

The collections framework has two interfaces and two classes
that facilitate sorting.  These are:

-  The Comparable interface - an implementing
    object can be compared with another implementing
    object, determining the natural ordering of
    the objects.
-  The Comparator interface - implementers can
    compare and order two objects, called specific ordering.
-  The Arrays class - allows sorting of objects
    contained in an array using either natural
    or specific ordering.
-  The Collections class - allows sorting of
    objects contained in a class that implements the
    List interface using natural or specific ordering.

There seem to be three obvious kinds of data
that one might consider sorting:

1)	Values of a multi-valued attribute.
2)	Attribute Names
3)	Search Entries

Let's see how the Collections Framework
can be used to sort these items:

Sort values from a multivalued binary attribute:

	byte[][] values = attr.getByteValueArray( )
	Arrays.sort( values[]);                         // Order by byte
values
	Arrays.sort( values[], myComparator) // Specific ordering

Sort values from a multivalued String attribute:

	String[] values = attr.getStringValueArray();
	Arrays.sort(values);                            // Natural
ordering
	Arrays.sort(values, myComparator);  // Specific Ordering

Sort Attribute objects

	LDAPEntry entry = results.next();
	LDAPAttributeSet aset = entry.getAttributeSet()
	LDAPAttribute[] attrs = new LDAPAttribute[20];
	attrs = aset.toArray(attrs);            // from Set
	Arrays.sort(attrs);                           // Natural
ordering by attribute name
	Arrays.sort(attrs, myComparator); // Specific ordering

    Note: Currently natural ordering between
    LDAPAttribute objects is undefined since
    LDAPAttribute does not implement the Comparable
    interface.  I suggest that LDAPAttribute implement
    the Comparable interface with natural ordering
    based on attribute name.

Sort LDAPEntry objects

    It is not possible to sort Entries, since
    there is no way to get either a List or an
    Array from LDAPSearchResults.  I suggest
    that LDAPEntry implement the Comparable
    interface with natural ordering based on the
    dn, and that LDAPSearchResults implement
    a nextArray() method (see below).  If this
    were done, sorting of LDAPEntry objects
    could be done as follows:

    LDAPEntry[] entries = results.nextArray();
    Arrays.sort(entries);                           // Sort by dn
    Arrays.sort(entries, myComparator);	// Specific ordering.
    
    Note: myComparator is probably of type LDAPCompareAttrNames,
    but an application could implement their own Comparator class.
 
-----------------------------------------------------------------------------
Suggested changes to the draft:

2.1 public class LDAPAttribute
        implements Cloneable, Serializable, Comparable
  ------------------------
2.2.2 compareTo

	public int compareTo(Object obj)

    Compares this object with the specified object for
    order.  Ordering is determined by comparing attribute
    names (see getName()) using the compareTo() method of
    the String class. Returns a negative integer, zero, or
    a positive integer as this object is less than, equal
    to, or greater than the specified object.

     obj		The object to be compared to this object.
  ------------------------

2.14 public classs LDAPEntry
        implements Serializable, Comparable
  ------------------------
2.14.2 compareTo

	public int compareTo(Object obj)

    Compares this object with the specified object for
    order.  Ordering is determined by comparing dn values
    (see getDN) using the compareTo() method of the String
    class.  Returns a negative integer, zero, or a
    positive integer as this object is less than, equal
    to, or greater than the specified object.

     obj		The object to be compared to this object.

  ------------------------
(in class LDAPSearchResults)

2.35.5 nextArray 

	public LDAPEntry[] nextArray() throws LDAPException

Returns an array containing all LDAPEntries in the
order they were received. If automatic referral
following is disabled or a referral was not
followed, nextArray() will throw an
LDAPReferralException when the referral is received.

It should be noted that only those Entries which
have been received and not retrieved via next()
or nextArray() are placed in the array.  This will
generally be the number of entries specified by
batchsize. The size of the array returned will
equal the number of entries returned. If all
entries have already been returned, nextArray()
throws NoSuchElementException.  If a sort of all
entries returned by a search is desired, batchsize
should be set to zero so that all entries are received
by the API before control is returned to the application.
(see LDAPSearchConstraints.setBatchSize()).  Sorting
can be performed using the sort method of the Arrays class.
  ------------------------


-Steve



------------------------
Steven Sonntag
Novell, Inc., The leading provider of Net business solutions
http://www.novell.com 


_______________________________________________
Ldapext mailing list
Ldapext@ietf.org
https://www1.ietf.org/mailman/listinfo/ldapext