/** *
 *  ArrayWrapper.java
 *
 *  Revisions: 1.0 Nov. 02, 2002
 *               Created the ArrayWrapper class
 *			   1.1 Nov. 04, 2002
 *				 Finished, Commented, Compiled
 *
 *  
* * Collaboration Statement: * I worked on the homework assignment alone, using only * course materials. * * Created with JCreatorLE, some indents are off when viewed through notepad * or EMACS * * @author Jose Manuel Caban * @version Version 1.1, Nov. 04, 2002 */ import java.util.*; import java.awt.*; import javax.swing.event.*; public class ArrayWrapper { /** *compArray - Stores the Array */ private Comparable[] compArray; /** *listeners - Stores the listeners to the array */ private Vector listeners; /** *Sets the type of random function to use for randomArray() */ private static int RANDOM_TYPE; //////////////// //Constructors// //////////////// public ArrayWrapper(Comparable[] compArray){ this(compArray,0); } public ArrayWrapper(Comparable[] compArray, int i){ this.compArray = compArray; listeners = new Vector(); RANDOM_TYPE = i; } /////////// //Methods// /////////// ////////////////// //Event Handling// ////////////////// /** *Add a ChangeListener to the listener pool *@param cl, the listener to add */ public void addChangeListener(ChangeListener cl){ Enumeration eTemp = listeners.elements(); boolean bDoAdd = true; while(eTemp.hasMoreElements()){ ChangeListener clTemp = (ChangeListener)eTemp.nextElement(); if(clTemp.equals(cl)){ bDoAdd = false; } } if(bDoAdd){ listeners.add(cl); } } /** *Remove a listener from the listener pool *@param cl, the listener to remove */ public void removeChangeListener(ChangeListener cl){ int iCount = 0; Enumeration eTemp = listeners.elements(); while(eTemp.hasMoreElements()){ ChangeListener clTemp = (ChangeListener)eTemp.nextElement(); if(clTemp.equals(cl)){ break; } else{iCount++;} } listeners.removeElementAt(iCount); } /** *Fire a ChangeEvent to every listener in the pool */ private void fireChangeEvent(){ Enumeration eTemp = listeners.elements(); while(eTemp.hasMoreElements()){ ((ChangeListener)eTemp.nextElement()).stateChanged( new ChangeEvent(this)); } } ///////////////// //Array Methods// ///////////////// /** *@return length of the array */ public int length(){ return compArray.length; } /** *Return the array */ public Integer[] toIntArray(){ return (Integer[])compArray; } /** *Swap the values from the two params in the array *@param i, the value to be put in *@param j */ public void swap(int i, int j){ Comparable[] cTemp = new Comparable[2]; try{ cTemp[0] = compArray[i]; cTemp[1] = compArray[j]; } catch(Exception e){ System.err.println("Swap() Error\n" + e); } compArray[j] = cTemp[0]; compArray[i] = cTemp[1]; fireChangeEvent(); } /** *@return value *@param i */ public Comparable get(int i){ try{ compArray[i] = compArray[i]; } catch(Exception e){ System.err.println("Get() Error\n" + e); return null; } return compArray[i]; } /** *Set the value *@param i to *@param value */ public void set(int i, Comparable value){ try{ compArray[i] = value; } catch(Exception e){ System.err.println("Set() Error\n" + e); } fireChangeEvent(); } /** *compare the values *@param i and *@param j */ public int compare(int i, int j){ try{ return compArray[i].compareTo(compArray[j]); } catch(Exception e){ System.err.println("compare() Error\n" + e); return 0; } } /** *Create an array of Random Integers *@param size, the size of the array *@return an array of length size and unique Integer values */ public static Integer[] getRandomIntegerArray(int size){ switch(RANDOM_TYPE){ case 0: return randomIntegerArrayUno(size); /*case 1: return randomIntegerArrayDeux(size); break;*/ default: return null; } } /** *This uses the rather boring Random class from the API *to produce an Integer[] array *@param size, the size of the array */ public static Integer[] randomIntegerArrayUno(int size){ Integer[] ioTemp = new Integer[size]; Random rando = new Random(); boolean bDone; for(int i=0; i
* @param args a String array of command line arguments. */ public static void main(String[] args) { ArrayWrapper bob = new ArrayWrapper(new Comparable[4]); Integer[] temp = getRandomIntegerArray(2000); for(int i=0;i