2012-11-29 45 views
0

我正在嘗試構建一個樹形圖,並且已經爲類的鍵實現了我自己的Comparable接口,但我仍然得到一個「不能轉換爲可比較」的異常,而且我真的不會不明白爲什麼。這裏是我的代碼的相關部分:可比較<T>不適用於樹形圖

接口:

public interface Comparable<T> { 

public int compareTo(T o); 

} 

類鑰匙:

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; 
    } 
} 


} 

部分,其中我添加元素的樹:

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()); 
} 

現在當我使用測試去測試此代碼時:

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); 


} 

然後它返回異常,即無法將PriorityBuy解析爲可比較的。但是,似乎我已經構建了該程序,以便它能夠正常工作。所以.....誰能幫我看看爲什麼不是?

回答

3

您無法創建自己的Comparable接口;您必須使用內置的java.lang.Comparable

+0

好吧那麼我該如何使用java.lang.Comparable來實現可比較的接口?這或多或少會刪除我寫的界面嗎?編輯:沒關係,我是個白癡。感謝您幫助我看到它。 :) –