In the Module 4 on polymorphism, we transformed MessagePriorityQueue and MessageQueue in Java into generic Queue and a PriorityQueue classes based on our own AbstractQueue interface. Now that we have been introduced to the Java Collections framework, it is time to take the next step by replacing our own Queue class and AbstractQueue interface with the ones from the Java Collection framework.
Starting with the code located in the 2019SPCS5004SV/lecture-10-priorityqueueJava GitHub repository, make the following changes.
In PriorityQueue, import the java.util.Queue interface and either the java.util.LinkedList or java.util.ArrayDeque class. Both implement the java.util.Queue interface. Also import the java.util.EnumMap class.
In PriorityQueue, change queues to be a EnumMap<Priority, Queue>. The EnumMap is an efficient Map whose keys are enum values. We will use it to hold a Queue for each Priority. Please review the documentation for EnumMap and if you are curious, also look at the source code for this class to see how it is implemented. In the PriorityQueue constructor, initialize the queues EnumMap. Use this special form of the EnumMap constructor for the Priority enum: queues = EnumMap<>(Priority.class).
Also initialize the queues map with instances of either java.util.LinkedList or java.util.ArrayDeque.
Modify the PriorityQueue and PriorityQueueIterator operations to access the java.util.Queue instances in the queues EnumMap and to call the the appropriate java.util.Queue methods. Also remove the PriorityQueue_test.test_0020_Queue() and PriorityQueue_test.test_0025_QueueIterator test methods since we are no longer relying on our own Queue and QueueIterator classes. Finally, delete the file Queue.java. Rerun the unit tests to verify that they still run correctly.
In PriorityQueue, import java.util.AbstractQueue and change the class to extend java.util.AbstractQueue. Rename all the getSize(), enqueue(), and dequeue() methods to the corresponding ones in java.util.Queue(). Be sure to add @Override where appropriate. Now use your IDE to add any unimplemented methods, and implement them. Consult the java.util.Queue JavaDoc for descriptions of these methods. Finally, delete the file AbstractQueue.java as we are no longer relying on this class.
Modify the unit tests in PriorityQueue_test to use the corresponding java.util.Queue methods and re-run the unit tests to verify that they still run correctly. Devise additional unit tests for the any other methods you implemented that came from extending java.util.AbstractQueue.
The resulting PriorityQueue class is implemented entirely with Java Collections interfaces and classes, and fully conforms to the Java Collection framework.