diff options
author | Brian Duff <bduff@google.com> | 2013-10-15 18:35:44 -0700 |
---|---|---|
committer | Max Cai <maxtroy@google.com> | 2013-10-25 16:06:21 +0100 |
commit | 56a37328ae568225a672a4ba2654710caa0b8e59 (patch) | |
tree | 3201f50635c71bc298c1aef45d23106f3e06cee3 /java/src | |
parent | 461d4ac87a5a6dee584da0c8451cdaae253f3873 (diff) | |
download | protobuf-56a37328ae568225a672a4ba2654710caa0b8e59.tar.gz protobuf-56a37328ae568225a672a4ba2654710caa0b8e59.tar.bz2 protobuf-56a37328ae568225a672a4ba2654710caa0b8e59.zip |
Implement hashCode() and equals() behind a generator option.
The option is only called 'generate_equals' because:
- equals() is the main thing; hashCode() is there only to
complement equals();
- it's shorter;
- toString() should not be included in this option because
it's more for debugging and it's more likely to stop
ProGuard from working well.
Also shortened the "has bit" expression; was
((bitField & mask) == mask), now ((bitField & mask) != 0).
Both the Java code and the bytecode are slightly shorter.
Change-Id: Ic309a08a60883bf454eb6612679aa99611620e76
Diffstat (limited to 'java/src')
3 files changed, 455 insertions, 4 deletions
diff --git a/java/src/main/java/com/google/protobuf/nano/InternalNano.java b/java/src/main/java/com/google/protobuf/nano/InternalNano.java index 49309516..ce4a206c 100644 --- a/java/src/main/java/com/google/protobuf/nano/InternalNano.java +++ b/java/src/main/java/com/google/protobuf/nano/InternalNano.java @@ -31,6 +31,7 @@ package com.google.protobuf.nano; import java.io.UnsupportedEncodingException; +import java.util.Arrays; /** * The classes contained within are used internally by the Protocol Buffer @@ -40,7 +41,10 @@ import java.io.UnsupportedEncodingException; * * @author kenton@google.com (Kenton Varda) */ -public class InternalNano { +public final class InternalNano { + + private InternalNano() {} + /** * Helper called by generated code to construct default values for string * fields. @@ -69,7 +73,7 @@ public class InternalNano { * converts from the generated string to the string we actually want. The * generated code calls this automatically. */ - public static final String stringDefaultValue(String bytes) { + public static String stringDefaultValue(String bytes) { try { return new String(bytes.getBytes("ISO-8859-1"), "UTF-8"); } catch (UnsupportedEncodingException e) { @@ -88,7 +92,7 @@ public class InternalNano { * In this case we only need the second of the two hacks -- allowing us to * embed raw bytes as a string literal with ISO-8859-1 encoding. */ - public static final byte[] bytesDefaultValue(String bytes) { + public static byte[] bytesDefaultValue(String bytes) { try { return bytes.getBytes("ISO-8859-1"); } catch (UnsupportedEncodingException e) { @@ -103,11 +107,215 @@ public class InternalNano { * Helper function to convert a string into UTF-8 while turning the * UnsupportedEncodingException to a RuntimeException. */ - public static final byte[] copyFromUtf8(final String text) { + public static byte[] copyFromUtf8(final String text) { try { return text.getBytes("UTF-8"); } catch (UnsupportedEncodingException e) { throw new RuntimeException("UTF-8 not supported?"); } } + + /** + * Checks repeated int field equality; null-value and 0-length fields are + * considered equal. + */ + public static boolean equals(int[] field1, int[] field2) { + if (field1 == null || field1.length == 0) { + return field2 == null || field2.length == 0; + } else { + return Arrays.equals(field1, field2); + } + } + + /** + * Checks repeated long field equality; null-value and 0-length fields are + * considered equal. + */ + public static boolean equals(long[] field1, long[] field2) { + if (field1 == null || field1.length == 0) { + return field2 == null || field2.length == 0; + } else { + return Arrays.equals(field1, field2); + } + } + + /** + * Checks repeated float field equality; null-value and 0-length fields are + * considered equal. + */ + public static boolean equals(float[] field1, float[] field2) { + if (field1 == null || field1.length == 0) { + return field2 == null || field2.length == 0; + } else { + return Arrays.equals(field1, field2); + } + } + + /** + * Checks repeated double field equality; null-value and 0-length fields are + * considered equal. + */ + public static boolean equals(double[] field1, double[] field2) { + if (field1 == null || field1.length == 0) { + return field2 == null || field2.length == 0; + } else { + return Arrays.equals(field1, field2); + } + } + + /** + * Checks repeated boolean field equality; null-value and 0-length fields are + * considered equal. + */ + public static boolean equals(boolean[] field1, boolean[] field2) { + if (field1 == null || field1.length == 0) { + return field2 == null || field2.length == 0; + } else { + return Arrays.equals(field1, field2); + } + } + + /** + * Checks repeated bytes field equality. Only non-null elements are tested. + * Returns true if the two fields have the same sequence of non-null + * elements. Null-value fields and fields of any length with only null + * elements are considered equal. + */ + public static boolean equals(byte[][] field1, byte[][] field2) { + int index1 = 0; + int length1 = field1 == null ? 0 : field1.length; + int index2 = 0; + int length2 = field2 == null ? 0 : field2.length; + while (true) { + while (index1 < length1 && field1[index1] == null) { + index1++; + } + while (index2 < length2 && field2[index2] == null) { + index2++; + } + boolean atEndOf1 = index1 >= length1; + boolean atEndOf2 = index2 >= length2; + if (atEndOf1 && atEndOf2) { + // no more non-null elements to test in both arrays + return true; + } else if (atEndOf1 != atEndOf2) { + // one of the arrays have extra non-null elements + return false; + } else if (!Arrays.equals(field1[index1], field2[index2])) { + // element mismatch + return false; + } + index1++; + index2++; + } + } + + /** + * Checks repeated string/message field equality. Only non-null elements are + * tested. Returns true if the two fields have the same sequence of non-null + * elements. Null-value fields and fields of any length with only null + * elements are considered equal. + */ + public static boolean equals(Object[] field1, Object[] field2) { + int index1 = 0; + int length1 = field1 == null ? 0 : field1.length; + int index2 = 0; + int length2 = field2 == null ? 0 : field2.length; + while (true) { + while (index1 < length1 && field1[index1] == null) { + index1++; + } + while (index2 < length2 && field2[index2] == null) { + index2++; + } + boolean atEndOf1 = index1 >= length1; + boolean atEndOf2 = index2 >= length2; + if (atEndOf1 && atEndOf2) { + // no more non-null elements to test in both arrays + return true; + } else if (atEndOf1 != atEndOf2) { + // one of the arrays have extra non-null elements + return false; + } else if (!field1[index1].equals(field2[index2])) { + // element mismatch + return false; + } + index1++; + index2++; + } + } + + /** + * Computes the hash code of a repeated int field. Null-value and 0-length + * fields have the same hash code. + */ + public static int hashCode(int[] field) { + return field == null || field.length == 0 ? 0 : Arrays.hashCode(field); + } + + /** + * Computes the hash code of a repeated long field. Null-value and 0-length + * fields have the same hash code. + */ + public static int hashCode(long[] field) { + return field == null || field.length == 0 ? 0 : Arrays.hashCode(field); + } + + /** + * Computes the hash code of a repeated float field. Null-value and 0-length + * fields have the same hash code. + */ + public static int hashCode(float[] field) { + return field == null || field.length == 0 ? 0 : Arrays.hashCode(field); + } + + /** + * Computes the hash code of a repeated double field. Null-value and 0-length + * fields have the same hash code. + */ + public static int hashCode(double[] field) { + return field == null || field.length == 0 ? 0 : Arrays.hashCode(field); + } + + /** + * Computes the hash code of a repeated boolean field. Null-value and 0-length + * fields have the same hash code. + */ + public static int hashCode(boolean[] field) { + return field == null || field.length == 0 ? 0 : Arrays.hashCode(field); + } + + /** + * Computes the hash code of a repeated bytes field. Only the sequence of all + * non-null elements are used in the computation. Null-value fields and fields + * of any length with only null elements have the same hash code. + */ + public static int hashCode(byte[][] field) { + int result = 0; + for (int i = 0, size = field == null ? 0 : field.length; i < size; i++) { + byte[] element = field[i]; + if (element != null) { + result = 31 * result + Arrays.hashCode(element); + } + } + return result; + } + + /** + * Computes the hash code of a repeated string/message field. Only the + * sequence of all non-null elements are used in the computation. Null-value + * fields and fields of any length with only null elements have the same hash + * code. + */ + public static int hashCode(Object[] field) { + int result = 0; + for (int i = 0, size = field == null ? 0 : field.length; i < size; i++) { + Object element = field[i]; + if (element != null) { + result = 31 * result + element.hashCode(); + } + } + return result; + } + } diff --git a/java/src/main/java/com/google/protobuf/nano/UnknownFieldData.java b/java/src/main/java/com/google/protobuf/nano/UnknownFieldData.java index 0db2a83c..833ed2a9 100644 --- a/java/src/main/java/com/google/protobuf/nano/UnknownFieldData.java +++ b/java/src/main/java/com/google/protobuf/nano/UnknownFieldData.java @@ -30,6 +30,8 @@ package com.google.protobuf.nano; +import java.util.Arrays; + /** * Stores unknown fields. These might be extensions or fields that the generated API doesn't * know about yet. @@ -37,6 +39,7 @@ package com.google.protobuf.nano; * @author bduff@google.com (Brian Duff) */ public final class UnknownFieldData { + final int tag; final byte[] bytes; @@ -44,4 +47,25 @@ public final class UnknownFieldData { this.tag = tag; this.bytes = bytes; } + + @Override + public boolean equals(Object o) { + if (o == this) { + return true; + } + if (!(o instanceof UnknownFieldData)) { + return false; + } + + UnknownFieldData other = (UnknownFieldData) o; + return tag == other.tag && Arrays.equals(bytes, other.bytes); + } + + @Override + public int hashCode() { + int result = 17; + result = 31 * result + tag; + result = 31 * result + Arrays.hashCode(bytes); + return result; + } } diff --git a/java/src/test/java/com/google/protobuf/NanoTest.java b/java/src/test/java/com/google/protobuf/NanoTest.java index 1149c40b..b9061e9f 100644 --- a/java/src/test/java/com/google/protobuf/NanoTest.java +++ b/java/src/test/java/com/google/protobuf/NanoTest.java @@ -60,6 +60,7 @@ import junit.framework.TestCase; import java.util.ArrayList; import java.util.Arrays; +import java.util.HashMap; import java.util.List; /** @@ -2684,6 +2685,224 @@ public class NanoTest extends TestCase { assertHasWireData(message, false); } + public void testHashCodeEquals() throws Exception { + // Complete equality: + TestAllTypesNano a = createMessageForHashCodeEqualsTest(); + TestAllTypesNano aEquivalent = createMessageForHashCodeEqualsTest(); + + // Null and empty array for repeated fields equality: + TestAllTypesNano b = createMessageForHashCodeEqualsTest(); + b.repeatedBool = null; + b.repeatedFloat = new float[0]; + TestAllTypesNano bEquivalent = createMessageForHashCodeEqualsTest(); + bEquivalent.repeatedBool = new boolean[0]; + bEquivalent.repeatedFloat = null; + + // Ref-element-type repeated fields use non-null subsequence equality: + TestAllTypesNano c = createMessageForHashCodeEqualsTest(); + c.repeatedString = null; + c.repeatedStringPiece = new String[] {null, "one", null, "two"}; + c.repeatedBytes = new byte[][] {{3, 4}, null}; + TestAllTypesNano cEquivalent = createMessageForHashCodeEqualsTest(); + cEquivalent.repeatedString = new String[3]; + cEquivalent.repeatedStringPiece = new String[] {"one", "two", null}; + cEquivalent.repeatedBytes = new byte[][] {{3, 4}}; + + // Complete equality for messages with has fields: + TestAllTypesNanoHas d = createMessageWithHasForHashCodeEqualsTest(); + TestAllTypesNanoHas dEquivalent = createMessageWithHasForHashCodeEqualsTest(); + + // If has-fields exist, fields with the same default values but + // different has-field values are different. + TestAllTypesNanoHas e = createMessageWithHasForHashCodeEqualsTest(); + e.optionalInt32++; // make different from d + e.hasDefaultString = false; + TestAllTypesNanoHas eDifferent = createMessageWithHasForHashCodeEqualsTest(); + eDifferent.optionalInt32 = e.optionalInt32; + eDifferent.hasDefaultString = true; + + // Complete equality for messages with accessors: + TestNanoAccessors f = createMessageWithAccessorsForHashCodeEqualsTest(); + TestNanoAccessors fEquivalent = createMessageWithAccessorsForHashCodeEqualsTest(); + System.out.println("equals: " + f.equals(fEquivalent)); + System.out.println("hashCode: " + f.hashCode() + " vs " + fEquivalent.hashCode()); + + // If using accessors, explicitly setting a field to its default value + // should make the message different. + TestNanoAccessors g = createMessageWithAccessorsForHashCodeEqualsTest(); + g.setOptionalInt32(g.getOptionalInt32() + 1); // make different from f + g.clearDefaultString(); + TestNanoAccessors gDifferent = createMessageWithAccessorsForHashCodeEqualsTest(); + gDifferent.setOptionalInt32(g.getOptionalInt32()); + gDifferent.setDefaultString(g.getDefaultString()); + + // Complete equality for reference typed messages: + NanoReferenceTypes.TestAllTypesNano h = createRefTypedMessageForHashCodeEqualsTest(); + NanoReferenceTypes.TestAllTypesNano hEquivalent = createRefTypedMessageForHashCodeEqualsTest(); + + // Inequality of null and default value for reference typed messages: + NanoReferenceTypes.TestAllTypesNano i = createRefTypedMessageForHashCodeEqualsTest(); + i.optionalInt32 = 1; // make different from h + i.optionalFloat = null; + NanoReferenceTypes.TestAllTypesNano iDifferent = createRefTypedMessageForHashCodeEqualsTest(); + iDifferent.optionalInt32 = i.optionalInt32; + iDifferent.optionalFloat = 0.0f; + + HashMap<MessageNano, String> hashMap = new HashMap<MessageNano, String>(); + hashMap.put(a, "a"); + hashMap.put(b, "b"); + hashMap.put(c, "c"); + hashMap.put(d, "d"); + hashMap.put(e, "e"); + hashMap.put(f, "f"); + hashMap.put(g, "g"); + hashMap.put(h, "h"); + hashMap.put(i, "i"); + + assertEquals(9, hashMap.size()); // a-i should be different from each other. + + assertEquals("a", hashMap.get(a)); + assertEquals("a", hashMap.get(aEquivalent)); + + assertEquals("b", hashMap.get(b)); + assertEquals("b", hashMap.get(bEquivalent)); + + assertEquals("c", hashMap.get(c)); + assertEquals("c", hashMap.get(cEquivalent)); + + assertEquals("d", hashMap.get(d)); + assertEquals("d", hashMap.get(dEquivalent)); + + assertEquals("e", hashMap.get(e)); + assertNull(hashMap.get(eDifferent)); + + assertEquals("f", hashMap.get(f)); + assertEquals("f", hashMap.get(fEquivalent)); + + assertEquals("g", hashMap.get(g)); + assertNull(hashMap.get(gDifferent)); + + assertEquals("h", hashMap.get(h)); + assertEquals("h", hashMap.get(hEquivalent)); + + assertEquals("i", hashMap.get(i)); + assertNull(hashMap.get(iDifferent)); + } + + private TestAllTypesNano createMessageForHashCodeEqualsTest() { + TestAllTypesNano message = new TestAllTypesNano(); + message.optionalInt32 = 5; + message.optionalInt64 = 777; + message.optionalFloat = 1.0f; + message.optionalDouble = 2.0; + message.optionalBool = true; + message.optionalString = "Hello"; + message.optionalBytes = new byte[] { 1, 2, 3 }; + message.optionalNestedMessage = new TestAllTypesNano.NestedMessage(); + message.optionalNestedMessage.bb = 27; + message.optionalNestedEnum = TestAllTypesNano.BAR; + message.repeatedInt32 = new int[] { 5, 6, 7, 8 }; + message.repeatedInt64 = new long[] { 27L, 28L, 29L }; + message.repeatedFloat = new float[] { 5.0f, 6.0f }; + message.repeatedDouble = new double[] { 99.1, 22.5 }; + message.repeatedBool = new boolean[] { true, false, true }; + message.repeatedString = new String[] { "One", "Two" }; + message.repeatedBytes = new byte[][] { { 2, 7 }, { 2, 7 } }; + message.repeatedNestedMessage = new TestAllTypesNano.NestedMessage[] { + message.optionalNestedMessage, + message.optionalNestedMessage + }; + message.repeatedNestedEnum = new int[] { + TestAllTypesNano.BAR, + TestAllTypesNano.BAZ + }; + // We set the _nan fields to something other than nan, because equality + // is defined for nan such that Float.NaN != Float.NaN, which makes any + // instance of TestAllTypesNano unequal to any other instance unless + // these fields are set. This is also the behavior of the regular java + // generator when the value of a field is NaN. + message.defaultFloatNan = 1.0f; + message.defaultDoubleNan = 1.0; + return message; + } + + private TestAllTypesNanoHas createMessageWithHasForHashCodeEqualsTest() { + TestAllTypesNanoHas message = new TestAllTypesNanoHas(); + message.optionalInt32 = 5; + message.optionalString = "Hello"; + message.optionalBytes = new byte[] { 1, 2, 3 }; + message.optionalNestedMessage = new TestAllTypesNanoHas.NestedMessage(); + message.optionalNestedMessage.bb = 27; + message.optionalNestedEnum = TestAllTypesNano.BAR; + message.repeatedInt32 = new int[] { 5, 6, 7, 8 }; + message.repeatedString = new String[] { "One", "Two" }; + message.repeatedBytes = new byte[][] { { 2, 7 }, { 2, 7 } }; + message.repeatedNestedMessage = new TestAllTypesNanoHas.NestedMessage[] { + message.optionalNestedMessage, + message.optionalNestedMessage + }; + message.repeatedNestedEnum = new int[] { + TestAllTypesNano.BAR, + TestAllTypesNano.BAZ + }; + message.defaultFloatNan = 1.0f; + return message; + } + + private TestNanoAccessors createMessageWithAccessorsForHashCodeEqualsTest() { + TestNanoAccessors message = new TestNanoAccessors() + .setOptionalInt32(5) + .setOptionalString("Hello") + .setOptionalBytes(new byte[] {1, 2, 3}) + .setOptionalNestedMessage(new TestNanoAccessors.NestedMessage().setBb(27)) + .setOptionalNestedEnum(TestNanoAccessors.BAR) + .setDefaultFloatNan(1.0f); + message.repeatedInt32 = new int[] { 5, 6, 7, 8 }; + message.repeatedString = new String[] { "One", "Two" }; + message.repeatedBytes = new byte[][] { { 2, 7 }, { 2, 7 } }; + message.repeatedNestedMessage = new TestNanoAccessors.NestedMessage[] { + message.getOptionalNestedMessage(), + message.getOptionalNestedMessage() + }; + message.repeatedNestedEnum = new int[] { + TestAllTypesNano.BAR, + TestAllTypesNano.BAZ + }; + return message; + } + + private NanoReferenceTypes.TestAllTypesNano createRefTypedMessageForHashCodeEqualsTest() { + NanoReferenceTypes.TestAllTypesNano message = new NanoReferenceTypes.TestAllTypesNano(); + message.optionalInt32 = 5; + message.optionalInt64 = 777L; + message.optionalFloat = 1.0f; + message.optionalDouble = 2.0; + message.optionalBool = true; + message.optionalString = "Hello"; + message.optionalBytes = new byte[] { 1, 2, 3 }; + message.optionalNestedMessage = + new NanoReferenceTypes.TestAllTypesNano.NestedMessage(); + message.optionalNestedMessage.foo = 27; + message.optionalNestedEnum = NanoReferenceTypes.TestAllTypesNano.BAR; + message.repeatedInt32 = new int[] { 5, 6, 7, 8 }; + message.repeatedInt64 = new long[] { 27L, 28L, 29L }; + message.repeatedFloat = new float[] { 5.0f, 6.0f }; + message.repeatedDouble = new double[] { 99.1, 22.5 }; + message.repeatedBool = new boolean[] { true, false, true }; + message.repeatedString = new String[] { "One", "Two" }; + message.repeatedBytes = new byte[][] { { 2, 7 }, { 2, 7 } }; + message.repeatedNestedMessage = + new NanoReferenceTypes.TestAllTypesNano.NestedMessage[] { + message.optionalNestedMessage, + message.optionalNestedMessage + }; + message.repeatedNestedEnum = new int[] { + NanoReferenceTypes.TestAllTypesNano.BAR, + NanoReferenceTypes.TestAllTypesNano.BAZ + }; + return message; + } + public void testNullRepeatedFields() throws Exception { // Check that serialization after explicitly setting a repeated field // to null doesn't NPE. |