PQItem.java

Download
1/*2 * PQItem.java3 *4 * Computer Science S-22, Harvard University5 */6 7/**8 * PQItem - a class that is used for items stored in a priority9 * queue (e.g., a heap).  Associated with each item is a priority, and10 * the compareTo() method of the class compares items according to their11 * priorities.12 */13public class PQItem implements Comparable<PQItem> {14    private Object data;15    private int priority;16    17    /*** Constructor ***/18    public PQItem(Object data, int priority) {19        this.data = data;20        this.priority = priority;21    }22    23    /** Accessor methods for the fields ***/24    public Object getData() {25        return data;26    }27    28    public int getPriority() {29        return priority;30    }31    32    /* 33     * compares two PQItem objects based solely on their priorities34     */35    public int compareTo(PQItem other) {36        return (priority - other.priority);37    }38    39    /*40     * returns a printable representation of a PQItem41     */42    public String toString() {43        if (data == null) {44            return "" + priority;45        } else {46            return data + "(priority = " + priority + ")";47        }48    }49}