IntelliJ IDEA:使用Google Guava生成equals,hashCode和toString

问题

在Java领域,我们经常需要编写equalshashCodetoString方法。 老实说,这通常只是一个样板义务。

得益于智能IDE,我们通常不再自己这样做。 我们只是让和IDE一起努力。 不过有一个问题。 生成的代码通常非常丑陋。 让我们考虑以下POJO:


public class Beer {

    private String brand;
    private String type;
    private int degrees;
    private double alcoholPercentage;
	private List<String> ingredients;

	// constructor
	// getters, setters if needed

}

通常的解决方案

所有主要的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仓库 那应该带您到以下对话框,您可以在其中搜索插件:

IntelliJ IDEA:使用Google Guava生成equals,hashCode和toString

使用新的equals和hashCode插件很简单,您将在旧版本旁边紧挨着有一个新的上下文菜单选项equals()和hashCode()豪华版 只需按ALT + INS (在Mac上为CTRL + N),您将看到熟悉的生成菜单:

IntelliJ IDEA:使用Google Guava生成equals,hashCode和toString

toString而言,我们只需要在IntelliJ中创建一个新模板。 ALT + INS并转到toString()菜单选项。 单击设置按钮 ,然后导航到模板选项卡 在模板标签中,点击+按钮

IntelliJ IDEA:使用Google Guava生成equals,hashCode和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:使用Google Guava生成equals,hashCode和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