Source of 8.15.java


  1: // Doubles the size of the array queue if it is full.
  2: // Precondition: checkIntegrity has been called.
  3: // @author Frank M. Carrano, Timothy M. Henry
  4: // @version 5.0
  5: private void ensureCapacity()
  6: {
  7:    if (frontIndex == ((backIndex + 2) % queue.length)) // If array is full,
  8:    {                                                   // double size of array
  9:       T[] oldQueue = queue;
 10:       int oldSize = oldQueue.length;
 11:       int newSize = 2 * oldSize;
 12:       checkCapacity(newSize);
 13:       integrityOK = false;

 15:       // The cast is safe because the new array contains null entries
 16:       @SuppressWarnings("unchecked")
 17:       T[] tempQueue = (T[]) new Object[newSize];
 18:       queue = tempQueue;
 19:       for (int index = 0; index < oldSize - 1; index++)
 20:       {
 21:          queue[index] = oldQueue[frontIndex];
 22:          frontIndex = (frontIndex + 1) % oldSize;
 23:       } // end for
 24:       
 25:       frontIndex = 0;
 26:       backIndex = oldSize - 2;
 27:       integrityOK = true;
 28:    } // end if
 29: } // end ensureCapacity