/*
Marcus Daniels August 2, 2001 swarm-support
(pj: This fixes a test example I posted)
Here's an example of how to use QSort in Java. 

Some particulars:

  1) Use (and if necessary, implement) the compareTo method in the Java class.
     Note it doesn't have two arguments.  

  2) Make the Selector against the object type you'll be comparing

  3) Use a Swarm List.  The JavaCollectionIndexProxy in Swarm uses a 
     ordinary Java Iterator, which doesn't provide the `set' that 
     QSort needs in order to rearrange the list. 
*/
import swarm.Globals; 
import swarm.simtools.NSelectImpl; 
import swarm.simtools.QSortImpl;
import swarm.simtools.QSort;
import swarm.Selector;
import swarm.collections.List;
import swarm.collections.ListImpl;
import swarm.collections.Index;
import java.lang.Integer;

public class TestQSort { 

    static void main (String args[]) { 
        Globals.env.initSwarm ("TestQSort",
                               "bug-swarm@swarm.org",
                               "0.0",
                               args); 

        List aList = new ListImpl (Globals.env.globalZone);
        
        for (int i = 0; i < 10; i++) 
            aList.addFirst (new Integer (i)); 

        printList (aList);
        Selector sel = getASelector (Integer.class, "compareTo");

        new QSortImpl().sortObjectsIn$using (aList, sel); 
        System.out.println ("sorted:");

        printList (aList);

        aList.drop ();
    }

    static void printList (List aList)
    {
        Index index = aList.listBegin (Globals.env.scratchZone);
        for (Object obj = index.next ();
             index.getLoc () == Globals.env.Member;
             obj = index.next ())
            System.out.println (obj);
        index.drop ();
    }
    
    static Selector getASelector (Class a, String s) {
        Selector sel = null;
        try {
            sel = new Selector (a,s,false);
        }
        catch (Exception e) {
            e.printStackTrace (System.err);
            System.exit (1);
        }
        return sel;
    }
}

