Search code examples
javatreemapcomparable

Comparable<T> not working for treemap


I am trying to build a treemap, and have implemented my own Comparable interface for the class of keys, but I'm still getting a "cannot be cast to comparable" exception, and I really don't understand why. Here is the relevant portion of my code:

Interface:

public interface Comparable<T> {

public int compareTo(T o);

}

Class of Keys:

public class PriorityBuy implements Comparable<PriorityBuy>{

protected double _priceDouble;
protected DollarValue _priceDollarValue = new DollarValue(_priceDouble);
protected Price _price = new Price(_priceDollarValue);
protected long _time;

public PriorityBuy(Price price, long time) {

    this._price = price;
    this._time = time;

}

public Price getPrice() {
    return _price;
}

public long getTime() {
    return _time;
}

/**
 * The following provides a new choice of hash function for this class.  The reason is that
 * we need to create a new equals method to match the compareTo method, and we need to know
 * that equal objects return equal hashCode.
 */

@Override
public int hashCode() {
    return (int) (((_price.hashCode())*13)^(_time));
}

/**
 * We re-implement the equals method to match our compareTo method, which depends on both price
 * and on time.
 */

@Override
public boolean equals(Object o) {
    if(! (o instanceof PriorityBuy)) {
        return false;
    }
    PriorityBuy p = (PriorityBuy) o;
    if(p.getPrice().getDollarValue().getValue() == _price.getDollarValue().getValue() && p.getTime() == _time) {
        return true;
    }

    return false;
}

/**
 * We are writing a compareTo method so that this class can implement Comparable<Priority>, which
 * in turn allows any treemap constructed with Priority as the class of keys to order the tree
 * according to the ordering defined by the compareTo method defined below, instead of using
 * the "natural ordering" as it usually does.
 */

@Override
public int compareTo(PriorityBuy a) {

    if(a.getPrice().getDollarValue().getValue() > this.getPrice().getDollarValue().getValue()) {
        return -1;
    }
    else if(a.getPrice().getDollarValue().getValue() < this.getPrice().getDollarValue().getValue()) {
        return 1;
    }
    else if(a.getTime() < this.getTime()) {
        return -1;
    }
    else if(a.getTime() > this.getTime()) {
        return 1;
    }
    else {
        return 0;   
    }
}


}

Portion where I add elements to the tree:

public void addToBuyBook(OrderBookMessage obm) throws Exception {
    Order order = this.createOrder(obm);
    TreeMap<PriorityBuy,Order> buyBookTree = _buyBook.get(obm.getTicker());
    buyBookTree.put(order.getPriorityBuy(), order);
    _buyBook.put(obm.getTicker(), buyBookTree);
    this.addBuyOrder(obm.getOrderID(), obm.getTicker(), obm.getLimitPrice(), obm.getQuantity());
}

Now when I go to test this code using the test:

public void testAddToBuyBook() throws Exception {
    OrderBookManager manager = new OrderBookManager();
    OrderBookMessage obm1 = new OrderBookMessage("IBM");
    OrderBookMessage obm2 = new OrderBookMessage("IBM");

    manager.addToBuyBook(obm1);

    manager.addToBuyBook(obm2);


}

then it returns the exception that PriorityBuy cannot be resolved to comparable. But it seems like I've built the program so that this should work. So.....can anyone help me see why it isn't?


Solution

  • You can't create your own Comparable interface; you must use the built-in java.lang.Comparable.