implement hashCode() for JavaNano maps.

pull/204/head
Jisi Liu 10 years ago
parent 1536e93349
commit d9a6f27bc3
  1. 19
      javanano/src/main/java/com/google/protobuf/nano/InternalNano.java
  2. 3
      src/google/protobuf/compiler/javanano/javanano_map_field.cc

@ -531,4 +531,23 @@ public final class InternalNano {
}
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 {
void MapFieldGenerator::
GenerateHashCodeCode(io::Printer* printer) const {
printer->Print(variables_,
"result = 31 * result +\n"
" com.google.protobuf.nano.InternalNano.hashCode(this.$name$);\n");
}
} // namespace javanano

Loading…
Cancel
Save