/* @(#)IntArrayListNode.java 31 October 2005 */ /** * An IntArrayListNode is a structure consisting of an int[] data * of specified capacity, the count of the number of items in the * data array that are valid, and a reference to a possible * successor node. * * Normally if a successor node is non-null then the count of the * valid data items should equal the capacity. */ public class IntArrayListNode { int[] data = null; int capacity = 0; int count = 0; IntArrayListNode next = null; /** * The constructor that builds a node with the given * capacity, no initial data, and no successor node. * * If the capacity is less than 1, it is set to 1. */ public IntArrayListNode(int capacity) { if (capacity < 1) capacity = 1; data = new int[capacity]; this.capacity = capacity; } /** * The constructor that builds a node with the given * capacity, with number as it initial data, and no * successor node. * * If the capacity is less than 1, it is set to 1. */ public IntArrayListNode(int capacity, int number) { this(capacity); data[0] = number; count = 1; } /** * Returns the capacity of the internal int[]. */ public int getCapacity() { return capacity; } /** * Returns the count of valid data items in the * internal int[]. */ public int getCount() { return count; } /** * Returns true if the given index is a valid index * for retrieving a data item from this node. * * Equivalent to (0 <= index) && (index < getCount()). */ public boolean isValidIndex(int index) { return (0 <= index) && (index < count); } /** * Returns a data item in this node at the given index. */ public int getDataItem(int index) { if ((0 <= index) && (index < count)) return data[index]; throw new ArrayIndexOutOfBoundsException ("Invalid index in IntArrayListNode method getDataItem"); } /** * Returns the successor node to this node or null if * this node has no successor node. */ public IntArrayListNode getNext() { return next; } /** * Appends the given number to this node if the count * is less than the capacity and returns this node. * * Otherwise, if the successor node is currently null * then creates a new successor with the given number * as initial data and returns the new successor node. * * Otherwise, recursively calls append on the current * successor node and returns what this call returns. */ public IntArrayListNode append(int number) { if (count < capacity) { data[count] = number; count++; return this; } else if (next == null) return next = new IntArrayListNode(capacity, number); else return next.append(number); } }