Apache Thrift: Can we instruct thrift to translate a SET container to Java LinkedHashSet?

耗尽温柔 提交于 2019-12-11 13:18:04

问题


From the documentation: Thrift translates a SET container to HashSet by default. Can we change this behavior?

Containers Thrift containers are strongly typed containers that map to commonly used and commonly available container types in most programming languages.

https://thrift.apache.org/docs/types:

There are three container types: list: An ordered list of elements. Translates to an STL vector, Java ArrayList, native arrays in scripting languages, etc. set: An unordered set of unique elements. Translates to an STL set, Java HashSet, set in Python, etc. Note: PHP does not support sets, so it is treated similar to a List


回答1:


The sorted_containers option switches to

  • TreeMap for map<>
  • TreeSet for set<>

The option is to be specified when calling the Thrift compiler, like so:

$ thrift  -gen java:sorted_containers  myfile.thrift

Here are all options available for Java (curent master branch, thrift -help reveals more info)

 java (Java):
    beans:           Members will be private, and setter methods will return void.
    private-members: Members will be private, but setter methods will return 'this' like usual.
    nocamel:         Do not use CamelCase field accessors with beans.
    fullcamel:       Convert underscored_accessor_or_service_names to camelCase.
    android:         Generated structures are Parcelable.
    android_legacy:  Do not use java.io.IOException(throwable) (available for Android 2.3 and above).
    option_type:     Wrap optional fields in an Option type.
    java5:           Generate Java 1.5 compliant code (includes android_legacy flag).
    reuse-objects:   Data objects will not be allocated, but existing instances will be used (read and write).
    sorted_containers:
                     Use TreeSet/TreeMap instead of HashSet/HashMap as a implementation of set/map.
    generated_annotations=[undated|suppress]:
                     undated: suppress the date at @Generated annotations
                     suppress: suppress @Generated annotations entirely

Other options are not available. The C++ generator supports annotations, this seems not to be the case with Java. Did I mention that we accept patches?



来源:https://stackoverflow.com/questions/34341492/apache-thrift-can-we-instruct-thrift-to-translate-a-set-container-to-java-linke

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!