0

メッセージの順序付けに使用する 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());
    }
}
}

しかし、このクラスをコンパレーター Eclipse として使用しようとすると、エラーが発生し、呼び出しを削除するように指示されます。私はこのようなクラスを使用しようとしています

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

MessageSentTimestampComparer をコンパレータとして拡張しようとしましたが、成功しませんでした。誰かが私が間違っていることを説明してもらえますか?

4

2 に答える 2

5

あなたは実装MessageSentTimestampComparerしていません。これを試して: Comparator

public class MessageSentTimestampComparer implements Comparator<Message> {
  @Override
  public int compare(Message x, Message y) {
    return 0;  // do your comparison
  }
}
于 2013-06-10T18:29:20.403 に答える