// // Queue Class // // This is my Queue class. Its members are Proc objects. class Queue { private int queueSize; private Proc theQueue[]; private int nextFree; // Constructor for the Queue class. // Its MUST receive as argument the maximum queue size. Queue(int size) { queueSize = size; nextFree = 0; theQueue = new Proc[size]; } // Returns TRUE if the queue is full. public boolean isFull() { return (queueSize == nextFree); } // returns TRUE if the queue is empty. public boolean isEmpty() { return (nextFree == 0); } // Returns the number of processes currently in the queue. public int length() { return nextFree; } // Reads the time left in the i-th process in the queue public int getTime(int i) { return theQueue[i].getTime(); } // Reads the process name of the i-th process in the queue public String getName(int i) { return theQueue[i].getName(); } // Reads the creationTime of the first process in the queue public float getCreation() { return theQueue[0].getCreation(); } // Changes the time left for the process being served by the CPU // which is always process 0. public void setTime(int i, int time) { theQueue[i].setTime(time); } // This method adds a new process to the queue. It receives as // arguments the desired process execution time. // Processes are always added to the end of the queue. // public synchronized void enqueue(Proc newProcess) { if (isFull()) { // System.out.println("Cannot add process. Queue is full"); } else { theQueue[nextFree] = newProcess; nextFree++; } } // This method removes the process at the beginning of the queue. // All other processes are rotated towards the beginning of the queue. public synchronized void dequeue() { if (isEmpty()) { // System.out.println("Cannot delete process. Queue is empty"); } else { for (int i = 1; i < nextFree; i++) { theQueue[i - 1] = theQueue[i]; } nextFree--; } } // This method rotates the queue, so that Queue[0] is always the // element being serviced by the CPU. public void rotate() { /* rotating an empty queue is harmless. (I think!) */ if (!isEmpty()) { Proc temp = theQueue[0]; for (int i = 1; i < nextFree; i++) { theQueue[i-1] = theQueue[i]; } theQueue[nextFree - 1] = temp; } } // For debugging purposes: // public void printQueue() { // for (int i = 0; i < nextFree; i++) { // System.out.print("Queue[" + i + "] "); // System.out.println(theQueue[i].getTime()); // } // } }