// SortClient.java

     /****************************************************************
      * Copyright (C) 1996 - 2000 Micro Focus International Ltd.     *
      * All Rights Reserved.                                         *
      ****************************************************************
      * The program demonstrates the use of Java invoking Object     *
      * COBOL and vice versa using Java objects as parameters.       *
      *                                                              *
      * These classes form the client to the SortDemo class and the  *
      * comparison objects. The client will invoke methods in the    *
      * SortDemo class in order to sort various Java vector objects. *
      *                                                              *
      * Compile using either JAVAC *.java or JVC *.java              *
      * Run using either: JAVA SortClient or JVIEW SortClient        *
      *                                                              *
      * REQUIREMENTS: Sun JDK 1.2.2 (or later) must be installed.    *
      *                                                              *
      * $(#) V1.0                                                    *
      ****************************************************************/

import java.util.*;

class CompareStr implements Compare, java.io.Serializable
{
	public int compare(Object s1, Object s2)
	{
		return ((String) s1).compareTo((String) s2);
	}
}

class CompareNum implements Compare, java.io.Serializable
{
	public int compare(Object n1, Object n2) 
	{
		return ((Number) n1).intValue() - ((Number) n2).intValue();
	}
}

public class SortClient
{  
	private static void show(Vector v, Vector result)
	{
		System.out.println("Vector:  " + v);
		System.out.println("Sorted result: " + result);
	}

	public static void main(String[] args) throws Exception
	{
		SortDemo sort = new SortDemo();

		/* Sort a vector containing integers 2 and 1 */
		CompareNum compare1 = new CompareNum();
		Vector numbers = new Vector();

		numbers.addElement(new Integer(2));
		numbers.addElement(new Integer(1));
		show(numbers, sort.sortVector(numbers, compare1));

		/* Sort a vector containing more integers */
		numbers.addElement(new Integer(8));
		numbers.addElement(new Integer(4));
		numbers.addElement(new Integer(7));
		numbers.addElement(new Integer(12));
		numbers.addElement(new Integer(9));
		numbers.addElement(new Integer(4));
		numbers.addElement(new Integer(10));
		numbers.addElement(new Integer(15));
		show(numbers, sort.sortVector(numbers, compare1));

		/* Sort a vector containing random integers */
		Random random = new Random();
		for(int i = 0; i < 25; i++)
			numbers.addElement(new Integer(random.nextInt() % 100));
		show(numbers, sort.sortVector(numbers, compare1));
	
		/* Sort a vector containing a variety of numbers */
		Vector numbers2 = new Vector();
		numbers2.addElement(new Byte((byte)     (random.nextInt() % 10)));
		numbers2.addElement(new Double((double) (random.nextInt() % 10)));
		numbers2.addElement(new Float((float)   (random.nextInt() % 10)));
		numbers2.addElement(new Integer((int)   (random.nextInt() % 10)));
		numbers2.addElement(new Long((long)     (random.nextInt() % 10)));
		numbers2.addElement(new Short((short)   (random.nextInt() % 10)));
		show(numbers2, sort.sortVector(numbers2, compare1));
		
		/* Sort a vector containing strings */
		CompareStr compare2 = new CompareStr();
		Vector strings = new Vector();
		strings.addElement("Testing");
		strings.addElement("the");
		strings.addElement("string");
		strings.addElement("comparison");
		strings.addElement("using");
		strings.addElement("COBOL");
		show(strings, sort.sortVector(strings, compare2));
	}
}