You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@thrift.apache.org by br...@apache.org on 2010/04/04 06:01:07 UTC

svn commit: r930618 - /incubator/thrift/trunk/compiler/cpp/src/generate/t_java_generator.cc

Author: bryanduxbury
Date: Sun Apr  4 04:01:07 2010
New Revision: 930618

URL: http://svn.apache.org/viewvc?rev=930618&view=rev
Log:
THRIFT-753. java: Don't look up TFieldIdEnum values with a map

Instead of using a generated map, use a big switch statement.

Modified:
    incubator/thrift/trunk/compiler/cpp/src/generate/t_java_generator.cc

Modified: incubator/thrift/trunk/compiler/cpp/src/generate/t_java_generator.cc
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/compiler/cpp/src/generate/t_java_generator.cc?rev=930618&r1=930617&r2=930618&view=diff
==============================================================================
--- incubator/thrift/trunk/compiler/cpp/src/generate/t_java_generator.cc (original)
+++ incubator/thrift/trunk/compiler/cpp/src/generate/t_java_generator.cc Sun Apr  4 04:01:07 2010
@@ -3566,13 +3566,11 @@ void t_java_generator::generate_field_na
 
   out << ";" << endl << endl;
 
-  indent(out) << "private static final Map<Integer, _Fields> byId = new HashMap<Integer, _Fields>();" << endl;
   indent(out) << "private static final Map<String, _Fields> byName = new HashMap<String, _Fields>();" << endl;
   out << endl;
 
   indent(out) << "static {" << endl;
   indent(out) << "  for (_Fields field : EnumSet.allOf(_Fields.class)) {" << endl;
-  indent(out) << "    byId.put((int)field._thriftId, field);" << endl;
   indent(out) << "    byName.put(field.getFieldName(), field);" << endl;
   indent(out) << "  }" << endl;
   indent(out) << "}" << endl << endl;
@@ -3581,7 +3579,22 @@ void t_java_generator::generate_field_na
   indent(out) << " * Find the _Fields constant that matches fieldId, or null if its not found." << endl;
   indent(out) << " */" << endl;
   indent(out) << "public static _Fields findByThriftId(int fieldId) {" << endl;
-  indent(out) << "  return byId.get(fieldId);" << endl;
+  indent_up();
+  indent(out) << "switch(fieldId) {" << endl;
+  indent_up();
+
+  for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
+    indent(out) << "case " << (*m_iter)->get_key() << ": // " << constant_name((*m_iter)->get_name()) << endl;
+    indent(out) << "  return " << constant_name((*m_iter)->get_name()) << ";" << endl;
+  }
+
+  indent(out) << "default:" << endl;
+  indent(out) << "  return null;" << endl;
+
+  indent_down();
+  indent(out) << "}" << endl;
+
+  indent_down();
   indent(out) << "}" << endl << endl;
 
   indent(out) << "/**" << endl;