package username.util; import java.util.Arrays; /** * A simple wrapper for Java arrays to help resolve (a) the difference * in syntax between arrays and almost every other kind of object and * (b) the standard problem in declaring arrays in parameterized * classes. * * Warning! If you compile the source file, you will likely get a * warning message. In the standard Java compiler, the message * will read something like: * Note: SimpleArray.java uses unchecked or unsafe operations. * Note: Recompile with -Xlint:unchecked for details. * In Eclipse, the error may read more like * Type safety: The cast from Object to SimpleArray is actually ... * Type safety: The cast from Object to T is actually ... * You may ignore those warnings. * * @author Samuel A. Rebelsky * @version 1.0 of April 2006 */ public class SimpleArray { // +------------------+------------------------------------- // | Design Decisions | // +------------------+ /* (1) For complex reasons, Java does not permit you to declare an array whose base type is a type parameter for a parameterized type (also called a "generic"). Hence, the base type of the underlying array will be Object, and we will cast as necessary. (2) SimpleArray objects are intended to be simple. Hence, they do not expand on their own, as Vectors do. If you need to expand a SimpleArray, you must instead build a new one and copy over all of the contents. */ // +--------+----------------------------------------------- // | Fields | // +--------+ /** The underlying array. */ Object[] contents; // +--------------+----------------------------------------- // | Constructors | // +--------------+ /** * Build a new array of specified size. * * @pre: size >= 0. */ public SimpleArray(int size) { this.contents = new Object[size]; } // SimpleArray(int) /** * Build a new array, initializing the values to those * in stuff. */ public SimpleArray(T[] stuff) { this.contents = new Object[stuff.length]; for (int i = 0; i < stuff.length; i++) { this.contents[i] = stuff[i]; } // for(int) } // SimpleArray(T[]) // +------------------+------------------------------------- // | Standard Methods | // +------------------+ /** * Convert to a string for printing. */ public String toString() { return Arrays.toString(contents); } // toString() /** * This array equals another object if the other object is an * array and its contents are equal. */ @SuppressWarnings({"unchecked"}) public boolean equals(Object other) { try { return this.equals((SimpleArray) other); } catch (ClassCastException cce) { return false; } } // equals(Object) /** * This array equals another simple array containing the same * types if the two arrays are the same size and each corresponding * element is the same. */ public boolean equals(SimpleArray other) { // The two arrays must be the same size. if (this.size() != other.size()) { return false; } // Check each position int len = this.size(); for (int i = 0; i < len; i++) { if (!this.get(i).equals(other.get(i))) { return false; } // if } // for // If we've gotten this far, everything is the same return true; } // equals(SimpleArray) /** * Generate a hash code. */ public int hashCode() { if (this.size() == 0) { return Integer.MAX_VALUE; } else if (this.size() == 1) { return 3 * this.get(0).hashCode(); } else { return 3*this.get(0).hashCode() + 5*this.get(1).hashCode(); } } // hashCode // +----------------+--------------------------------------- // | Public Methods | // +----------------+ /** * Get the value associated with index i. * * @return t * the value at index i. * * @pre * 0 <= i < this.size() * @post * t is a value that meets the criertion that the most recent call * to set with first parameter equal to i has a second parameter * equal to t. * @post * If no call to set with equal i has been made, returns null. */ @SuppressWarnings({"unchecked"}) public T get(int i) { return (T) this.contents[i]; } // get(int) /** * Set the value at index i to newval. * * @pre * 0 <= i < this.size() */ public void set(int i, T newval) { this.contents[i] = newval; } // set(int, T) /** * Get the size of this array. */ public int size() { return this.contents.length; } // size() } // class SimpleArray