Forráskód Böngészése

implement hashCode() for JavaNano maps.

Jisi Liu 11 éve
szülő
commit
d9a6f27bc3

+ 19 - 0
javanano/src/main/java/com/google/protobuf/nano/InternalNano.java

@@ -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();
+  }
 }

+ 3 - 0
src/google/protobuf/compiler/javanano/javanano_map_field.cc

@@ -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