You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@thrift.apache.org by ro...@apache.org on 2012/12/16 15:49:02 UTC

git commit: THRIFT-1630 Equivalent objects that contain sets and maps can serialize differently Patch: Kamil Salas

Updated Branches:
  refs/heads/master b03039208 -> e7b0b6924


THRIFT-1630 Equivalent objects that contain sets and maps can serialize differently
Patch: Kamil Salas


Project: http://git-wip-us.apache.org/repos/asf/thrift/repo
Commit: http://git-wip-us.apache.org/repos/asf/thrift/commit/e7b0b692
Tree: http://git-wip-us.apache.org/repos/asf/thrift/tree/e7b0b692
Diff: http://git-wip-us.apache.org/repos/asf/thrift/diff/e7b0b692

Branch: refs/heads/master
Commit: e7b0b692461735366d77b73a497863cc86c22d91
Parents: b030392
Author: Roger Meier <ro...@apache.org>
Authored: Sun Dec 16 15:44:48 2012 +0100
Committer: Roger Meier <ro...@apache.org>
Committed: Sun Dec 16 15:44:48 2012 +0100

----------------------------------------------------------------------
 .gitignore                                    |   18 +++++++++
 compiler/cpp/src/generate/t_java_generator.cc |   38 ++++++++++++++++---
 2 files changed, 50 insertions(+), 6 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/thrift/blob/e7b0b692/.gitignore
----------------------------------------------------------------------
diff --git a/.gitignore b/.gitignore
old mode 100644
new mode 100755
index 10c767f..9aa31aa
--- a/.gitignore
+++ b/.gitignore
@@ -30,6 +30,7 @@ gen-*
 /compiler/cpp/libparse.a
 /compiler/cpp/Makefile
 /compiler/cpp/Makefile.in
+/compiler/cpp/src/windows/version.h
 /compiler/cpp/thrift
 /compiler/cpp/thriftl.cc
 /compiler/cpp/thrifty.cc
@@ -191,6 +192,7 @@ gen-*
 /lib/php/test/Makefile
 /lib/php/test/Makefile.in
 /lib/php/test/phpunit.xml
+/lib/php/test/packages/
 /lib/erl/logs/
 /lib/erl/Makefile
 /lib/erl/Makefile.in
@@ -226,4 +228,20 @@ gen-*
 /test/py.twisted/test_suite.pyc
 /test/rb/Makefile
 /test/rb/Makefile.in
+/tutorial/Makefile
+/tutorial/Makefile.in
+/tutorial/cpp/Makefile
+/tutorial/cpp/Makefile.in
+/tutorial/cpp/TutorialClient
+/tutorial/cpp/TutorialServer
+/tutorial/java/Makefile
+/tutorial/java/Makefile.in
+/tutorial/java/build/
+/tutorial/js/Makefile
+/tutorial/js/Makefile.in
+/tutorial/js/build/
+/tutorial/py.twisted/Makefile
+/tutorial/py.twisted/Makefile.in
+/tutorial/py/Makefile
+/tutorial/py/Makefile.in
 /ylwrap

http://git-wip-us.apache.org/repos/asf/thrift/blob/e7b0b692/compiler/cpp/src/generate/t_java_generator.cc
----------------------------------------------------------------------
diff --git a/compiler/cpp/src/generate/t_java_generator.cc b/compiler/cpp/src/generate/t_java_generator.cc
index d5c0bfe..aa7f843 100644
--- a/compiler/cpp/src/generate/t_java_generator.cc
+++ b/compiler/cpp/src/generate/t_java_generator.cc
@@ -62,6 +62,9 @@ public:
     iter = parsed_options.find("android_legacy");
     android_legacy_ = (iter != parsed_options.end());
 
+    iter = parsed_options.find("sorted_containers");
+    sorted_containers_ = (iter != parsed_options.end());
+
     iter = parsed_options.find("java5");
     java5_ = (iter != parsed_options.end());
     if (java5_) {
@@ -292,6 +295,7 @@ public:
   bool gen_hash_code_;
   bool android_legacy_;
   bool java5_;
+  bool sorted_containers_;
 };
 
 
@@ -341,9 +345,15 @@ string t_java_generator::java_package() {
  */
 string t_java_generator::java_type_imports() {
   string hash_builder;
+  string tree_set_and_map;
   if (gen_hash_code_) {
     hash_builder = "import org.apache.commons.lang.builder.HashCodeBuilder;\n";
   }
+  if (sorted_containers_) {
+    tree_set_and_map = string() + 
+      "import java.util.TreeSet;\n" +
+      "import java.util.TreeMap;\n";
+  }
 
   return
     string() +
@@ -364,6 +374,7 @@ string t_java_generator::java_type_imports() {
     "import java.util.Set;\n" +
     "import java.util.HashSet;\n" +
     "import java.util.EnumSet;\n" +
+    tree_set_and_map + 
     "import java.util.Collections;\n" +
     "import java.util.BitSet;\n" +
     "import java.nio.ByteBuffer;\n"
@@ -2879,10 +2890,15 @@ void t_java_generator::generate_deserialize_container(ofstream& out,
 
   indent(out) << prefix << " = new " << type_name(ttype, false, true);
   // size the collection correctly
-  out << "("
-    << (ttype->is_list() ? "" : "2*" )
-    << obj << ".size"
-    << ");" << endl;
+  if (sorted_containers_ && (ttype->is_map() || ttype->is_set())) {
+    // TreeSet and TreeMap don't have any constructor which takes a capactity as an argument
+    out << "();" << endl;
+  } else {
+    out << "("
+      << (ttype->is_list() ? "" : "2*" )
+      << obj << ".size"
+      << ");" << endl;
+  }
 
   // For loop iterates over elements
   string i = tmp("_i");
@@ -3190,7 +3206,11 @@ string t_java_generator::type_name(t_type* ttype, bool in_container, bool in_ini
   } else if (ttype->is_map()) {
     t_map* tmap = (t_map*) ttype;
     if (in_init) {
-      prefix = "HashMap";
+      if (sorted_containers_) {
+        prefix = "TreeMap";
+      } else {
+        prefix = "HashMap";
+      }
     } else {
       prefix = "Map";
     }
@@ -3200,7 +3220,11 @@ string t_java_generator::type_name(t_type* ttype, bool in_container, bool in_ini
   } else if (ttype->is_set()) {
     t_set* tset = (t_set*) ttype;
     if (in_init) {
-      prefix = "HashSet";
+      if (sorted_containers_) {
+        prefix = "TreeSet";
+      } else { 
+        prefix = "HashSet";
+      }
     } else {
       prefix = "Set";
     }
@@ -4194,5 +4218,7 @@ THRIFT_REGISTER_GENERATOR(java, "Java",
 "    hashcode:        Generate quality hashCode methods.\n"
 "    android_legacy:  Do not use java.io.IOException(throwable) (available for Android 2.3 and above).\n"
 "    java5:           Generate Java 1.5 compliant code (includes android_legacy flag).\n"
+"    sorted_containers:\n"
+"                     Use TreeSet/TreeMap instead of HashSet/HashMap as a implementation of set/map.\n"
 )