You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by GitBox <gi...@apache.org> on 2018/07/25 10:53:10 UTC

[GitHub] nkurihar closed pull request #2226: Fix implementation of equals() for MessageId

nkurihar closed pull request #2226: Fix implementation of equals() for MessageId
URL: https://github.com/apache/incubator-pulsar/pull/2226
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

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 78109c490c..d66e242feb 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 boolean equals(Object obj) {
             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 41cc9c9d50..3686b124a8 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 int hashCode() {
 
     @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 a3b5f72995..af21fcfde6 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 void hashCodeTest() {
         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));
+    }
+
 }


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services