Commit d9a6f27b authored by Jisi Liu's avatar Jisi Liu

implement hashCode() for JavaNano maps.

parent 1536e933
...@@ -531,4 +531,23 @@ public final class InternalNano { ...@@ -531,4 +531,23 @@ public final class InternalNano {
} }
return a.equals(b); return a.equals(b);
} }
public static <K, V> int hashCode(Map<K, V> map) {
if (map == null) {
return 0;
}
int result = 0;
for (Entry<K, V> entry : map.entrySet()) {
result += hashCodeForMap(entry.getKey())
^ hashCodeForMap(entry.getValue());
}
return result;
}
private static int hashCodeForMap(Object o) {
if (o instanceof byte[]) {
return Arrays.hashCode((byte[]) o);
}
return o.hashCode();
}
} }
...@@ -175,6 +175,9 @@ GenerateEqualsCode(io::Printer* printer) const { ...@@ -175,6 +175,9 @@ GenerateEqualsCode(io::Printer* printer) const {
void MapFieldGenerator:: void MapFieldGenerator::
GenerateHashCodeCode(io::Printer* printer) const { GenerateHashCodeCode(io::Printer* printer) const {
printer->Print(variables_,
"result = 31 * result +\n"
" com.google.protobuf.nano.InternalNano.hashCode(this.$name$);\n");
} }
} // namespace javanano } // namespace javanano
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment