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 /src/google/protobuf/compiler/javanano/javanano_message_field.cc | |
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 'src/google/protobuf/compiler/javanano/javanano_message_field.cc')
-rw-r--r-- | src/google/protobuf/compiler/javanano/javanano_message_field.cc | 55 |
1 files changed, 49 insertions, 6 deletions
diff --git a/src/google/protobuf/compiler/javanano/javanano_message_field.cc b/src/google/protobuf/compiler/javanano/javanano_message_field.cc index 02253f97..9f8298c7 100644 --- a/src/google/protobuf/compiler/javanano/javanano_message_field.cc +++ b/src/google/protobuf/compiler/javanano/javanano_message_field.cc @@ -126,8 +126,25 @@ GenerateSerializedSizeCode(io::Printer* printer) const { "}\n"); } -string MessageFieldGenerator::GetBoxedType() const { - return ClassName(params_, descriptor_->message_type()); +void MessageFieldGenerator:: +GenerateEqualsCode(io::Printer* printer) const { + printer->Print(variables_, + "if (this.$name$ == null) { \n" + " if (other.$name$ != null) {\n" + " return false;\n" + " }\n" + "} else {\n" + " if (!this.$name$.equals(other.$name$)) {\n" + " return false;\n" + " }\n" + "}\n"); +} + +void MessageFieldGenerator:: +GenerateHashCodeCode(io::Printer* printer) const { + printer->Print(variables_, + "result = 31 * result +\n" + " (this.$name$ == null ? 0 : this.$name$.hashCode());\n"); } // =================================================================== @@ -203,8 +220,22 @@ GenerateSerializedSizeCode(io::Printer* printer) const { "}\n"); } -string AccessorMessageFieldGenerator::GetBoxedType() const { - return ClassName(params_, descriptor_->message_type()); +void AccessorMessageFieldGenerator:: +GenerateEqualsCode(io::Printer* printer) const { + printer->Print(variables_, + "if ($name$_ == null) {\n" + " if (other.$name$_ != null) {\n" + " return false;\n" + " }\n" + "} else if (!$name$_.equals(other.$name$_)) {\n" + " return false;\n" + "}\n"); +} + +void AccessorMessageFieldGenerator:: +GenerateHashCodeCode(io::Printer* printer) const { + printer->Print(variables_, + "result = 31 * result + ($name$_ == null ? 0 : $name$_.hashCode());\n"); } // =================================================================== @@ -291,8 +322,20 @@ GenerateSerializedSizeCode(io::Printer* printer) const { "}\n"); } -string RepeatedMessageFieldGenerator::GetBoxedType() const { - return ClassName(params_, descriptor_->message_type()); +void RepeatedMessageFieldGenerator:: +GenerateEqualsCode(io::Printer* printer) const { + printer->Print(variables_, + "if (!com.google.protobuf.nano.InternalNano.equals(\n" + " this.$name$, other.$name$)) {\n" + " return false;\n" + "}\n"); +} + +void RepeatedMessageFieldGenerator:: +GenerateHashCodeCode(io::Printer* printer) const { + printer->Print(variables_, + "result = 31 * result\n" + " + com.google.protobuf.nano.InternalNano.hashCode(this.$name$);\n"); } } // namespace javanano |