FileDocCategorySizeDatePackage
BeanCollections.javaAPI DocExample6537Sun Dec 14 22:47:40 GMT 2003oreilly.hcj.collections

BeanCollections

public class BeanCollections extends Object
Demonstrate the collections issues with beans.
author
Robert Simmons jr. (kraythe)
version
$Revision: 1.3 $

Fields Summary
private static final Set
CUSTOMERS
A demo customer set.
Constructors Summary
Methods Summary
public static voidmain(java.lang.String[] args)
Main Method.

param
args command line arguments.

		CUSTOMERS = new HashSet();
		try {
			Customer customer = null;
			HashSet purchs = null;

			// -- Joe
			customer = new Customer();
			customer.setName("Joe");
			purchs = new HashSet();
			purchs.add(new Purchase("Cat Food", 22.34f));
			purchs.add(new Purchase("Cat Treats", 5.45f));
			purchs.add(new Purchase("Cat Toy", 12.95f));
			customer.setPurchases(purchs);
			customer.setPurchases2(purchs);
			customer.setPurchases3(purchs);
			CUSTOMERS.add(customer);
			// -- Jon
			customer = new Customer();
			customer.setName("Jon");
			purchs = new HashSet();
			purchs.add(new Purchase("Dog Food", 35.95f));
			purchs.add(new Purchase("Dog Toy", 9.24f));
			customer.setPurchases(purchs);
			customer.setPurchases2(purchs);
			customer.setPurchases3(purchs);
			CUSTOMERS.add(customer);
			// -- Jane
			customer = new Customer();
			customer.setName("Jane");
			purchs = new HashSet();
			customer.setPurchases(purchs);
			customer.setPurchases2(purchs);
			customer.setPurchases3(purchs);
			CUSTOMERS.add(customer);
		} catch (final Exception ex) {
			ex.printStackTrace();
		}
	
		try {
			Iterator iter = CUSTOMERS.iterator();
			Customer customer = null;
			while (iter.hasNext()) {
				customer = (Customer)iter.next();
				someFunction(customer);
				makeCustomerReport(customer);
			}
		} catch (final ClassCastException ex) {
			System.out.println("--- See? I told you. ---");
			ex.printStackTrace(System.out);
			System.out.flush();
		}
		System.out.println();
		System.out.println("--------------------------------------");
		System.out.println();

		// -- Write out the two types of report to show that they
		// do the same thing. Note that the order might be slightly 
		// different due to the nature of Set iterators.
		makeGroupReport(CUSTOMERS);

		System.out.println();
		System.out.println("--------------------------------------");
		System.out.println();

		makeGroupReportBetter(CUSTOMERS);
	
public static voidmakeCustomerReport(Customer customer)
Make a purchases report for a customer.

param
customer The customer for which to make a report.
throws
NullPointerException If customers is null.

		if (customer == null) {
			throw new NullPointerException();
		}
		Set purchases = customer.getPurchases();
		if (purchases != null) {
			Iterator iter = purchases.iterator();
			Purchase purch = null;
			System.out.println("Purchases for " + customer.getName());
			while (iter.hasNext()) {
				purch = (Purchase)iter.next();
				System.out.println(purch.getItemName() + "\t" + purch.getPrice());
			}
		}
	
public static voidmakeGroupReport(java.util.Set customers)
Prepare a report of purchases for the given customers. Example with potential null in Set peoperties.

param
customers The customers for which to prepare a report.
throws
NullPointerException If customers is null.

		if (customers == null) {
			throw new NullPointerException();
		}
		Iterator purchaseIter = null;
		Iterator customerIter = null;
		Set purchases = null;
		Customer customer = null;
		Purchase purch = null;

		customerIter = customers.iterator();
		while (customerIter.hasNext()) {
			customer = (Customer)customerIter.next();
			System.out.println("Purchases for " + customer.getName());
			purchases = customer.getPurchases3();
			if (purchases != null) {
				purchaseIter = purchases.iterator();
				while (purchaseIter.hasNext()) {
					purch = (Purchase)purchaseIter.next();
					System.out.println(purch.getItemName() + "\t" + purch.getPrice());
				}
			}
			System.out.print("Total Purchases = ");
			if (purchases != null) {
				System.out.println(purchases.size());
			} else {
				System.out.println(0);
			}
			System.out.println();
		}
	
public static voidmakeGroupReportBetter(java.util.Set customers)
Prepare a report of purchases for the given customers. Example with potential no nulls in Set peoperties.

param
customers The customers for which to prepare a report.
throws
NullPointerException If customers is null.

		if (customers == null) {
			throw new NullPointerException();
		}
		Iterator purchaseIter = null;
		Iterator customerIter = null;
		Set purchases = null;
		Customer customer = null;
		Purchase purch = null;

		customerIter = customers.iterator();
		while (customerIter.hasNext()) {
			customer = (Customer)customerIter.next();
			System.out.println("Purchases for " + customer.getName());
			purchases = customer.getPurchases3();
			purchaseIter = customer.getPurchases3()
				                   .iterator();
			while (purchaseIter.hasNext()) {
				purch = (Purchase)purchaseIter.next();
				System.out.println(purch.getItemName() + "\t" + purch.getPrice());
			}
			System.out.println("Total Purchases = " + purchases.size());
			System.out.println();
		}
	
public static voidsomeFunction(Customer customer)
Manipulate a customer.

param
customer The customer to manipulate.
throws
NullPointerException If customer is null.

		if (customer == null) {
			throw new NullPointerException();
		}
		Set purchs = customer.getPurchases();
		Set names = new HashSet();  // going to use to store customer names. 
		names.add(new String("Jason"));
		purchs.add(new String("Fred"));  // typo, he meant names, not purchs.