2015-12-17 23 views
1

從文檔:默認情況下,Thrift將SET容器轉換爲HashSet。我們可以改變這種行爲嗎?Apache Thrift:我們可以指導節點將SET容器轉換爲Java LinkedHashSet嗎?

集裝箱 節儉容器是強類型容器,它映射到大多數編程語言中常用和常用的容器類型。

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

有三種類型的容器: 列表:元素的有序列表。轉換爲STL向量,Java ArrayList,腳本語言中的本地數組等。 set:無序的獨特元素集。轉換到一個STL集合的Java HashSet的,在Python設置等。注:PHP不支持組,因此它被視爲類似於一個列表

+1

我已經知道我可以將類型更改爲由thrift生成的java類中的LinkedHashSet。但是,在我們的情況下,這並不是有用的,因爲我們每次在構建過程中生成/替換節儉類。 – Ruby9191

回答

0

sorted_containers選項切換到

  • TreeMapmap<>
  • TreeSetset<>

的選項是被當愈傷組織指定納克節儉編譯,就像這樣:

$ thrift -gen java:sorted_containers myfile.thrift 

這裏是可以用於Java所有選項(CURENT主分支,thrift -help透露更多信息)

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 

其他選項不可用。 C++生成器支持註釋,Java似乎不是這種情況。我有沒有提到we accept patches

+0

@ [JensG](https://stackoverflow.com/users/499466/jensg),與sorted_containers選項類似,有沒有一種方法可以使用ConcurrentHashMap而不是HashMap實現?我們需要這個來防止地圖中的null鍵。 – user1347244

+0

[鏈接到源代碼](https://github.com/apache/thrift/blob/master/compiler/cpp/src/thrift/generate/t_java_generator.cc#L4190)不,只有我上面寫的。但是,如果您認爲這是一個很好的補充,並希望這樣做 - 我們接受拉請求! C++生成器可以通過IDL註釋指定類,也許這可能是一種方法。有問題嗎?郵件列表! – JensG

+0

@ [JensG](https://stackoverflow.com/users/499466/jensg),我們可以爲Java生成器添加此選項嗎? – user1347244

相關問題