Browse Source

hashcode and equals for oneofs in nano.

Jisi Liu 10 years ago
parent
commit
f173cdeb02

+ 22 - 0
src/google/protobuf/compiler/javanano/javanano_field.cc

@@ -168,6 +168,28 @@ void SetCommonOneofVariables(const FieldDescriptor* descriptor,
       SimpleItoa(descriptor->number());
 }
 
+void GenerateOneofFieldEquals(const map<string, string>& variables,
+                              io::Printer* printer) {
+  printer->Print(variables,
+    "if (this.has$capitalized_name$()) {\n"
+    "  if (!this.$oneof_name$_.equals(other.$oneof_name$_)) {\n"
+    "    return false;\n"
+    "  }\n"
+    "} else {\n"
+    "  if (other.has$capitalized_name$()) {\n"
+    "    return false;\n"
+    "  }\n"
+    "}\n");
+
+}
+
+void GenerateOneofFieldHashCode(const map<string, string>& variables,
+                                io::Printer* printer) {
+  printer->Print(variables,
+    "result = 31 * result +\n"
+    "  ($has_oneof_case$ ? this.$oneof_name$_.hashCode() : 0);\n");
+}
+
 }  // namespace javanano
 }  // namespace compiler
 }  // namespace protobuf

+ 4 - 0
src/google/protobuf/compiler/javanano/javanano_field.h

@@ -114,6 +114,10 @@ class FieldGeneratorMap {
 
 void SetCommonOneofVariables(const FieldDescriptor* descriptor,
                              map<string, string>* variables);
+void GenerateOneofFieldEquals(const map<string, string>& variables,
+                              io::Printer* printer);
+void GenerateOneofFieldHashCode(const map<string, string>& variables,
+                                io::Printer* printer);
 
 }  // namespace javanano
 }  // namespace compiler

+ 2 - 10
src/google/protobuf/compiler/javanano/javanano_message_field.cc

@@ -214,20 +214,12 @@ GenerateSerializedSizeCode(io::Printer* printer) const {
 
 void MessageOneofFieldGenerator::
 GenerateEqualsCode(io::Printer* printer) const {
-  printer->Print(variables_,
-    "if (this.has$capitalized_name$()) {\n"
-    "  if (!this.$oneof_name$_.equals(other.$oneof_name$_)) {\n"
-    "    return false;\n"
-    "  }\n"
-    "} else {\n"
-    "  if (other.has$capitalized_name$()) {\n"
-    "    return false;\n"
-    "  }\n"
-    "}\n");
+  GenerateOneofFieldEquals(variables_, printer);
 }
 
 void MessageOneofFieldGenerator::
 GenerateHashCodeCode(io::Printer* printer) const {
+  GenerateOneofFieldHashCode(variables_, printer);
 }
 
 // ===================================================================

+ 6 - 2
src/google/protobuf/compiler/javanano/javanano_primitive_field.cc

@@ -765,10 +765,14 @@ void PrimitiveOneofFieldGenerator::GenerateSerializedSizeCode(
     "}\n");
 }
 
-void PrimitiveOneofFieldGenerator::GenerateEqualsCode(io::Printer* printer) const {
+void PrimitiveOneofFieldGenerator::GenerateEqualsCode(
+    io::Printer* printer) const {
+  GenerateOneofFieldEquals(variables_, printer);
 }
 
-void PrimitiveOneofFieldGenerator::GenerateHashCodeCode(io::Printer* printer) const {
+void PrimitiveOneofFieldGenerator::GenerateHashCodeCode(
+    io::Printer* printer) const {
+  GenerateOneofFieldHashCode(variables_, printer);
 }
 
 // ===================================================================