Merge "Add MessageNano.messageNanoEquals()."
This commit is contained in:
commit
553d39f9d8
@ -31,6 +31,7 @@
|
||||
package com.google.protobuf.nano;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* Abstract interface implemented by Protocol Message objects.
|
||||
@ -150,6 +151,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.
|
||||
|
@ -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;
|
||||
|
Loading…
Reference in New Issue
Block a user