summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBrian Duff <bduff@google.com>2015-03-20 23:15:14 +0000
committerAndroid Git Automerger <android-git-automerger@android.com>2015-03-20 23:15:14 +0000
commit6bcc95f7df592ad6ca91527a294503afc27dcf6c (patch)
treec64035000994d02a6560453eaa40880bf89788d4
parentbe3a18d1f871540519ac73efb4f420aef24a5be5 (diff)
parent3f1b763ab7bc54eeaff1d1c8ed707746bfe8a088 (diff)
downloadexternal_protobuf-6bcc95f7df592ad6ca91527a294503afc27dcf6c.zip
external_protobuf-6bcc95f7df592ad6ca91527a294503afc27dcf6c.tar.gz
external_protobuf-6bcc95f7df592ad6ca91527a294503afc27dcf6c.tar.bz2
am 3f1b763a: am ddf016d8: Merge "Add MessageNano.messageNanoEquals()."
* commit '3f1b763ab7bc54eeaff1d1c8ed707746bfe8a088': Add MessageNano.messageNanoEquals().
-rw-r--r--java/src/main/java/com/google/protobuf/nano/MessageNano.java26
-rw-r--r--java/src/test/java/com/google/protobuf/NanoTest.java3
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 d6288c9..164f317 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 6334e4b..6b69aa7 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;