MyTest.java


Title:Technical utilization of nested top level classes
Copyright: Copyright (c) 2000
Author: Bob Byron,
Company: RAD Systems, Inc.

import java.util.*;

// Customer is simply an example type of class that I can use
// for the perposes of this example.  But, this example should
// apply to any class that needs to be sorted.
class Customer {
   int custId;
   String firstName = new String();
   String lastName = new String();
   String middleName = new String();
   Customer(int custId, String lastName, String firstName, String middleName) {
      this.custId = custId;
      this.lastName = lastName;
      this.firstName = firstName;
      this.middleName = middleName;
   }
}

// MyTest is simply a class that contains my main entry point and the
// example usage of the comparator classes.
public class MyTest {

// Main entry point.
   public static void main(String[] args) {
//    My example data array.  It contains 10 Customers in no particular order.
      Customer[] customers = {
         new Customer( 3, "Smith",     "Jake", "B"),
         new Customer( 8, "Hancock",   "John", "J"),
         new Customer( 9, "Jefferson", "Tom",  "R"),
         new Customer( 2, "Johnson",   "Burt", "A"),
         new Customer( 4, "Smith",     "Jake", "J"),
         new Customer( 7, "Marsh",     "Bob",  "W"),
         new Customer( 5, "Brown",     "Jack", "C"),
         new Customer( 1, "Johnson",   "John", "J"),
         new Customer( 6, "Russell",   "Don",  "E"),
         new Customer(10, "Adams",     "John", "Q")
      };

//    List the array 0 through 9 - it is in no particular order
      System.out.println("\nOrder: Random");
      for (int i=0; i < customers.length; i++) {
         System.out.print("ID=" + customers[i].custId);
         System.out.println(" Name=" + customers[i].lastName + ", " +
            customers[i].firstName + " " + customers[i].middleName + ".");
      }

//    Sort the customers by Customer ID
      Arrays.sort(customers, new CompareCustomers.ByCustId());

//    List the array 0 through 9 - it is in custId order
      System.out.println("\nOrder: CustId");
      for (int i=0; i < customers.length; i++) {
         System.out.print("ID=" + customers[i].custId);
         System.out.println(" Name=" + customers[i].lastName + ", " +
            customers[i].firstName + " " + customers[i].middleName + ".");
      }

//    Sort the customers by Customer Name
      Arrays.sort(customers, new CompareCustomers.ByName());

//    List the array 0 through 9 - it is in name order
      System.out.println("\nOrder: Name");
      for (int i=0; i < customers.length; i++) {
         System.out.print("ID=" + customers[i].custId);
         System.out.println(" Name=" + customers[i].lastName + ", " +
            customers[i].firstName + " " + customers[i].middleName + ".");
      }

   }
}

//For the purposes of this example, all of the code was
//placed in a single file.  But, for actual real world
//work, this abstract class should be placed in a
//seperate file.  This class contains the actual point
//of the session.
//The abstract class serves as a container for the other
//top level nested classes.  The CompareCustomers class,
//as you can see, is abstract.  You cannot instantiate
//the class itself, which is the way we want it.  It
//merely exists as a container for common functions and
//to TIGHTLY bind these related classes.
//The inner classes are defined statically and inherit
//from the abstracted container class.
abstract class CompareCustomers implements Comparator {

//We verify that we have customer types with this method.
//This method can be eliminated if you are sure all of your
//objects in your array are of the proper type.  This
//method does prevent an exception from being thrown
//when types aren't right.
  final private int checkType(Object o1, Object o2) {
    if (!(o1 instanceof Customer))
      return 1;
    else if (!(o2 instanceof Customer))
      return -1;
    return 0;
  }

//Low level routines to make comparisons
  final private int compareCustId(Customer cust1, Customer cust2) {
    return (cust1.custId - cust2.custId);
  }
  final private int compareLastName(Customer cust1, Customer cust2) {
    return (cust1.lastName.compareTo(cust2.lastName));
  }
  final private int compareFirstName(Customer cust1, Customer cust2) {
    return (cust1.firstName.compareTo(cust2.firstName));
  }
  final private int compareMiddleName(Customer cust1, Customer cust2) {
    return (cust1.middleName.compareTo(cust2.middleName));
  }

//This class compares customers by name
  public static class ByName extends CompareCustomers {
    public int compare (Object o1, Object o2) {
      int comp = 0;

//    Check the object types, if they are different, then we can't compare.
      if ((comp = super.checkType( o1, o2)) != 0)
        return comp;

//    Let's use our specific object types.
      Customer cust1 = (Customer) o1;
      Customer cust2 = (Customer) o2;

//    This is our actual comaparison.
      if ((comp=super.compareLastName(cust1,cust2))!=0)
        return comp;
      if ((comp=super.compareFirstName(cust1,cust2))!=0)
        return comp;
      if ((comp=super.compareMiddleName(cust1,cust2))!=0)
        return comp;
      return super.compareCustId(cust1,cust2);
    }
    public boolean equals(Object o1, Object o2) {
      return (compare(o1,o2)==0);
    }
  }
//This class compares customers by customer id
  public static class ByCustId extends CompareCustomers {
    public int compare (Object o1, Object o2) {
      int comp = 0;
//    Check the object types, if they are different, then we can't compare.
      if ((comp = super.checkType( o1, o2)) != 0)
        return comp;

//    Let's use our specific object types.
      Customer cust1 = (Customer) o1;
      Customer cust2 = (Customer) o2;

//    This is our actual comaparison.
      return super.compareCustId(cust1,cust2);
    }
    public boolean equals(Object o1, Object o2) {
      return (compare(o1,o2)==0);
    }
  }
}

[join us] [home] [javachina]
Last updated 03-25-2001 by webmaster
>
Hosted by www.Geocities.ws

1