This is an automated email from the ASF dual-hosted git repository. sijie pushed a commit to branch branch-2.1 in repository https://gitbox.apache.org/repos/asf/incubator-pulsar.git
The following commit(s) were added to refs/heads/branch-2.1 by this push: new 3eaa79b Fix implementation of equals() for MessageId (#2226) 3eaa79b is described below commit 3eaa79beb20a759d9dba49a3dd75a5257c29d2e1 Author: massakam <massa...@yahoo-corp.jp> AuthorDate: Wed Jul 25 19:53:08 2018 +0900 Fix implementation of equals() for MessageId (#2226) --- .../pulsar/client/impl/BatchMessageIdImpl.java | 4 +++- .../apache/pulsar/client/impl/MessageIdImpl.java | 8 ++++---- .../pulsar/client/impl/BatchMessageIdImplTest.java | 24 ++++++++++++++++++++++ 3 files changed, 31 insertions(+), 5 deletions(-) diff --git a/pulsar-client/src/main/java/org/apache/pulsar/client/impl/BatchMessageIdImpl.java b/pulsar-client/src/main/java/org/apache/pulsar/client/impl/BatchMessageIdImpl.java index 78109c4..d66e242 100644 --- a/pulsar-client/src/main/java/org/apache/pulsar/client/impl/BatchMessageIdImpl.java +++ b/pulsar-client/src/main/java/org/apache/pulsar/client/impl/BatchMessageIdImpl.java @@ -92,7 +92,9 @@ public class BatchMessageIdImpl extends MessageIdImpl { return ledgerId == other.ledgerId && entryId == other.entryId && partitionIndex == other.partitionIndex && batchIndex == other.batchIndex; } else if (obj instanceof MessageIdImpl) { - return batchIndex == NO_BATCH && obj.equals(this); + MessageIdImpl other = (MessageIdImpl) obj; + return ledgerId == other.ledgerId && entryId == other.entryId && partitionIndex == other.partitionIndex + && batchIndex == NO_BATCH; } return false; } diff --git a/pulsar-client/src/main/java/org/apache/pulsar/client/impl/MessageIdImpl.java b/pulsar-client/src/main/java/org/apache/pulsar/client/impl/MessageIdImpl.java index 41cc9c9..3686b12 100644 --- a/pulsar-client/src/main/java/org/apache/pulsar/client/impl/MessageIdImpl.java +++ b/pulsar-client/src/main/java/org/apache/pulsar/client/impl/MessageIdImpl.java @@ -70,12 +70,12 @@ public class MessageIdImpl implements MessageId { @Override public boolean equals(Object obj) { - if (obj instanceof MessageIdImpl) { - MessageIdImpl other = (MessageIdImpl) obj; - return ledgerId == other.ledgerId && entryId == other.entryId && partitionIndex == other.partitionIndex; - } else if (obj instanceof BatchMessageIdImpl){ + if (obj instanceof BatchMessageIdImpl) { BatchMessageIdImpl other = (BatchMessageIdImpl) obj; return other.equals(this); + } else if (obj instanceof MessageIdImpl) { + MessageIdImpl other = (MessageIdImpl) obj; + return ledgerId == other.ledgerId && entryId == other.entryId && partitionIndex == other.partitionIndex; } return false; } diff --git a/pulsar-client/src/test/java/org/apache/pulsar/client/impl/BatchMessageIdImplTest.java b/pulsar-client/src/test/java/org/apache/pulsar/client/impl/BatchMessageIdImplTest.java index a3b5f72..af21fcf 100644 --- a/pulsar-client/src/test/java/org/apache/pulsar/client/impl/BatchMessageIdImplTest.java +++ b/pulsar-client/src/test/java/org/apache/pulsar/client/impl/BatchMessageIdImplTest.java @@ -44,4 +44,28 @@ public class BatchMessageIdImplTest { Assert.assertTrue(batchMsgId1.hashCode() != batchMsgId2.hashCode()); } + + @Test + public void equalsTest() { + BatchMessageIdImpl batchMsgId1 = new BatchMessageIdImpl(0, 0, 0, 0); + BatchMessageIdImpl batchMsgId2 = new BatchMessageIdImpl(1, 1, 1, 1); + BatchMessageIdImpl batchMsgId3 = new BatchMessageIdImpl(0, 0, 0, 1); + BatchMessageIdImpl batchMsgId4 = new BatchMessageIdImpl(0, 0, 0, -1); + MessageIdImpl msgId = new MessageIdImpl(0, 0, 0); + + Assert.assertTrue(batchMsgId1.equals(batchMsgId1)); + Assert.assertFalse(batchMsgId1.equals(batchMsgId2)); + Assert.assertFalse(batchMsgId1.equals(batchMsgId3)); + Assert.assertFalse(batchMsgId1.equals(batchMsgId4)); + Assert.assertFalse(batchMsgId1.equals(msgId)); + + Assert.assertTrue(msgId.equals(msgId)); + Assert.assertFalse(msgId.equals(batchMsgId1)); + Assert.assertFalse(msgId.equals(batchMsgId2)); + Assert.assertFalse(msgId.equals(batchMsgId3)); + Assert.assertTrue(msgId.equals(batchMsgId4)); + + Assert.assertTrue(batchMsgId4.equals(msgId)); + } + }