public abstract class AbstractPriorityQueue , V> { /** enum specifying ordering styles. * ASCENDING and MIN give element with smallest key first * DESCENDING and MAX give element with largest key first * Note that enum is a weird case of something that can be declared in an interface. * Enums can be thought of a "public static final" */ enum Ordering { ASCENDING, DESCENDING, MIN, MAX} /** The way in which the heap is ordered */ protected Ordering order; /** * An inner class to keep key/value pair together. */ protected class Entry,W> { /** Hold the key */ final L theK; /** Hold the value*/ final W theV; /** * Create an Entry instance * @param kk the key * @param vv the value */ public Entry(L kk, W vv) { theK = kk; theV = vv; } /** * A little utility to do comparisons. Takes advantage of L being defined to be Comparable, * so all this really does is to wrap the comparable with something to flip signs for * Min heap vs max heap. * @param e2 the item to be compared. * @return -1, or or 1 */ protected int doCompare(Entry e2) { switch (order) { case MIN: case ASCENDING: return this.theK.compareTo(e2.theK); case MAX: case DESCENDING: default: return e2.theK.compareTo(this.theK); } } public String toString() { return "{"+theK+","+theV+"}"; } } /** * The number of items in the queue * * @return the number of items in the queie */ public abstract int size(); /** * Return true iff the queue is empty * * @return true iff the queue is empty */ public abstract boolean isEmpty(); /** * Inserts the specified element into this queue if it is possible to do so * immediately without violating capacity restrictions, returning true upon * success and false on failure * * @param k the key of the element to be added * @param v the value of the ek=lement * @return true on success, false on failure */ public abstract boolean offer(K k, V v); /** * Retrieves and removes the head of this priority queue. * * @return the removed element, or null if the queue is empty */ public abstract V poll(); /** * Retrieves, but does not remove, the head of this priority queue. * * @return the element at the head of the queue or null if the queue is empty */ public abstract V peek(); }