diff options
author | Brian Duff <bduff@google.com> | 2014-09-02 22:41:55 +0000 |
---|---|---|
committer | Android Git Automerger <android-git-automerger@android.com> | 2014-09-02 22:41:55 +0000 |
commit | 5db346f1ef4a241d31c604432a446cd7f57945dd (patch) | |
tree | d6fe187ab5a90414c5a13d176e8c98324b6f46d4 | |
parent | 80241687af45f8f055f366e8d684e0b35e1713a4 (diff) | |
parent | 553d39f9d8f246b57255022e23549951464b8c13 (diff) | |
download | protobuf-5db346f1ef4a241d31c604432a446cd7f57945dd.tar.gz protobuf-5db346f1ef4a241d31c604432a446cd7f57945dd.tar.bz2 protobuf-5db346f1ef4a241d31c604432a446cd7f57945dd.zip |
am ddf016d8: Merge "Add MessageNano.messageNanoEquals()."
* commit 'ddf016d828d928eefbce89c602c323bf0f998574':
Add MessageNano.messageNanoEquals().
-rw-r--r-- | java/src/main/java/com/google/protobuf/nano/MessageNano.java | 26 | ||||
-rw-r--r-- | java/src/test/java/com/google/protobuf/NanoTest.java | 3 |
2 files changed, 29 insertions, 0 deletions
diff --git a/java/src/main/java/com/google/protobuf/nano/MessageNano.java b/java/src/main/java/com/google/protobuf/nano/MessageNano.java index d6288c92..164f317f 100644 --- a/java/src/main/java/com/google/protobuf/nano/MessageNano.java +++ b/java/src/main/java/com/google/protobuf/nano/MessageNano.java @@ -31,6 +31,7 @@ package com.google.protobuf.nano; import java.io.IOException; +import java.util.Arrays; /** * Abstract interface implemented by Protocol Message objects. @@ -151,6 +152,31 @@ public abstract class MessageNano { } /** + * Compares two {@code MessageNano}s and returns true if the message's are the same class and + * have serialized form equality (i.e. all of the field values are the same). + */ + public static final boolean messageNanoEquals(MessageNano a, MessageNano b) { + if (a == b) { + return true; + } + if (a == null || b == null) { + return false; + } + if (a.getClass() != b.getClass()) { + return false; + } + final int serializedSize = a.getSerializedSize(); + if (b.getSerializedSize() != serializedSize) { + return false; + } + final byte[] aByteArray = new byte[serializedSize]; + final byte[] bByteArray = new byte[serializedSize]; + toByteArray(a, aByteArray, 0, serializedSize); + toByteArray(b, bByteArray, 0, serializedSize); + return Arrays.equals(aByteArray, bByteArray); + } + + /** * Returns a string that is (mostly) compatible with ProtoBuffer's TextFormat. Note that groups * (which are deprecated) are not serialized with the correct field name. * diff --git a/java/src/test/java/com/google/protobuf/NanoTest.java b/java/src/test/java/com/google/protobuf/NanoTest.java index 6334e4b8..6b69aa7a 100644 --- a/java/src/test/java/com/google/protobuf/NanoTest.java +++ b/java/src/test/java/com/google/protobuf/NanoTest.java @@ -3212,6 +3212,9 @@ public class NanoTest extends TestCase { TestAllTypesNano a = createMessageForHashCodeEqualsTest(); TestAllTypesNano aEquivalent = createMessageForHashCodeEqualsTest(); + assertTrue(MessageNano.messageNanoEquals(a, aEquivalent)); + assertFalse(MessageNano.messageNanoEquals(a, new TestAllTypesNano())); + // Null and empty array for repeated fields equality: TestAllTypesNano b = createMessageForHashCodeEqualsTest(); b.repeatedBool = null; |