IntelliJ IDEA:使用Google Guava生成equals,hashCode和toString
問題
在Java領域,我們經常需要編寫equals , hashCode和toString方法。 老實說,這通常只是一個樣板義務。
得益于智能IDE,我們通常不再自己這樣做。 我們只是讓和IDE一起努力。 不過有一個問題。 生成的代碼通常非常丑陋。 讓我們考慮以下POJO:
通常的解決方案
所有主要的IDE都具有生成我提到的方法的能力,但這就是hashCode,equals和toString的樣子:
1.等于– IF語句的長列表...
@Override public boolean equals(final Object o) {if (this == o) {return true;}if (!(o instanceof Beer)) {return false;}final Beer beer = (Beer) o;if (Double.compare(beer.alcoholPercentage, alcoholPercentage) != 0) {return false;}if (degrees != beer.degrees) {return false;}if (!brand.equals(beer.brand)) {return false;}if (!ingredients.equals(beer.ingredients)) {return false;}if (!type.equals(beer.type)) {return false;}return true; }2. hashCode –混淆魔術數字,異或運算
@Override public int hashCode() {int result;long temp;result = brand.hashCode();result = 31 * result + type.hashCode();result = 31 * result + degrees;temp = alcoholPercentage != +0.0d ? Double.doubleToLongBits(alcoholPercentage) : 0L;result = 31 * result + (int) (temp ^ (temp >>> 32));result = 31 * result + ingredients.hashCode();return result; }3. toString –討厭的字符串連接
@Override public String toString() {return 'Beer{' +'brand='' + brand + '\'' +', type='' + type + '\'' +', degrees=' + degrees +', alcoholPercentage=' + alcoholPercentage +', ingredients=' + ingredients +'}'; }GOOGLE GUAVA解決方案
也許您聽說過Google Guava 。 也許您已經在使用它。 無論如何,Google Guava是一個不錯的小圖書館,為Java提供了很多便利。 使用番石榴,我們可以重寫上面的三種方法來更好地尋找替代方法:
1.等于–將IF語句大軍轉變為鏈式贖回權
@Override public boolean equals(final Object obj) {if (this == obj) {return true;}if (obj == null || getClass() != obj.getClass()) {return false;}final Beer other = (Beer) obj;return Objects.equal(this.brand, other.brand) && Objects.equal(this.type, other.type) && Objects.equal(this.degrees, other.degrees) && Objects.equal(this.alcoholPercentage, other.alcoholPercentage) && Objects.equal(this.ingredients, other.ingredients); }2. hashCode –單行
@Override public int hashCode() {return Objects.hashCode(brand, type, degrees, alcoholPercentage, ingredients); }3. toString –一致的鏈式調用
@Override public String toString() {return Objects.toStringHelper(this).add('brand', brand).add('type', type).add('degrees', degrees).add('alcoholPercentage', alcoholPercentage).add('ingredients', ingredients).toString(); }設置您的智能想法
對于equals和hashCode,有一個來自Michal Jedynak的名為Equals和HashCode Deluxe Generator的插件。 您可以直接在IntelliJ中安裝它,只需鍵入CTRL + SHIFT + A (在Mac上是CMD + SHIFT + A),然后鍵入Browser倉庫 。 那應該帶您到以下對話框,您可以在其中搜索插件:
使用新的equals和hashCode插件很簡單,您將在舊版本旁邊緊挨著有一個新的上下文菜單選項equals()和hashCode()豪華版 。 只需按ALT + INS (在Mac上為CTRL + N),您將看到熟悉的生成菜單:
就toString而言,我們只需要在IntelliJ中創建一個新模板。 按ALT + INS并轉到toString()菜單選項。 單擊設置按鈕 ,然后導航到模板選項卡 。 在模板標簽中,點擊+按鈕 :
為新模板命名(例如Guava toString左右),并將以下代碼粘貼到編輯器中:
public String toString() {#set ($autoImportPackages = 'com.google.common.base.Objects')return Objects.toStringHelper(this)#foreach ($member in $members).add('$member.name', $member.accessor)#end.toString(); }使用新模板很容易,只需進入生成菜單( ALT + INS ),選擇toString()并確保選擇正確的模板:
參考: IntelliJ IDEA:通過vrtoonjava博客的JCG合作伙伴 Michal Vrtiak 使用Google Guava生成equals,hashCode和toString 。
翻譯自: https://www.javacodegeeks.com/2013/01/intellij-idea-generate-equals-hashcode-and-tostring-with-google-guava.html
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的IntelliJ IDEA:使用Google Guava生成equals,hashCode和toString的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: (安卓模拟器跑分)
- 下一篇: 使用执行程序和ThreadPoolExe