2013-06-10 3 views
0

메시지 순서 지정에 사용할 Java TreeSet 함수에 대한 비교기 클래스를 만들었습니다. 이 클래스는Java TreeSet 용 비교기 클래스 만들기

public class MessageSentTimestampComparer 
{ 
/// <summary> 
/// IComparer implementation that compares the epoch SentTimestamp and MessageId 
/// </summary> 
/// <param name="x"></param> 
/// <param name="y"></param> 
/// <returns></returns> 

public int compare(Message x, Message y) 
{ 
    String sentTimestampx = x.getAttributes().get("SentTimestamp"); 
    String sentTimestampy = y.getAttributes().get("SentTimestamp"); 

    if((sentTimestampx == null) | (sentTimestampy == null)) 
    { 
     throw new NullPointerException("Unable to compare Messages " + 
       "because one of the messages did not have a SentTimestamp" + 
       " Attribute"); 
    } 

    Long epochx = Long.valueOf(sentTimestampx); 
    Long epochy = Long.valueOf(sentTimestampy); 

    int result = epochx.compareTo(epochy); 

    if (result != 0) 
    { 
     return result; 
    } 
    else 
    { 
     // same SentTimestamp so use the messageId for comparison 
     return x.getMessageId().compareTo(y.getMessageId()); 
    } 
} 
} 

를 다음과 같이 보입니다하지만 이클립스가 제공하고 오류 및 호출을 제거하기 위해 나에게 말한다 비교기로이 클래스를 사용하려고 할 때. 나는 또한없는 성공 비교기로 MessageSentTimestampComparer를 확장하는 시도이

private SortedSet<Message> _set = new TreeSet<Message>(new MessageSentTimestampComparer()); 

같은 클래스를 사용하려고 시도하고있다. 누군가 내가 잘못하고있는 것을 설명해 줄 수 있습니까?

답변

5

MessageSentTimestampComparerComparator을 구현하지 않습니다. 이 시도 : 생성자의 signatue 선택하면

public class MessageSentTimestampComparer implements Comparator<Message> { 
    @Override 
    public int compare(Message x, Message y) { 
    return 0; // do your comparison 
    } 
} 
+0

을 나는 구현과 확장의 차이를 잊어 버렸습니다. 감사 – JME