是否可以使用 HashCodeBuilder 和 EqualsBuilder 使 Eclipse 生成 hashCode 并等于

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/281121/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 13:24:21  来源:igfitidea点击:

Is it possible to make eclipse generate hashCode and equals with HashCodeBuilder and EqualsBuilder

javaeclipse

提问by daveb

The default generated hashCode and equals implementations are ugly at best.

默认生成的 hashCode 和 equals 实现充其量是丑陋的。

Is it possible to make eclipse generate ones from HashCodeBuilder and EqualsBuilder, and perhaps even a toString with ToStringBuilder?

是否有可能让 Eclipse 从 HashCodeBuilder 和 EqualsBuilder 生成那些,甚至可能是一个带有 ToStringBuilder 的 toString?

采纳答案by toolkit

Take a look at Commons4E

看看Commons4E

It hasn't been updated in a while, but then I don't guess it needs to change much?

它已经有一段时间没有更新了,但我认为它不需要改变太多?

Update: Just checked against 3.4.1 and it works fine.

更新:刚刚检查了 3.4.1,它工作正常。

回答by Andrei Zagorneanu

You can configure Eclipse to generate toString()using a custom builder. In our case ToStringBuilderfrom Apache Commons Lang. You can see here http://azagorneanu.blogspot.com/2011/08/how-to-generate-equals-hashcode.htmlhow to do it.

您可以将 Eclipse 配置为toString()使用自定义构建器生成。在我们的例子中ToStringBuilder来自Apache Commons Lang。你可以在这里看到http://azagorneanu.blogspot.com/2011/08/how-to-generate-equals-hashcode.html怎么做。

That blog post contains also Eclipse templates for generating equals(), hashCode()and compareTo()using Apache Commons Lang builders.

这博客文章也包含Eclipse模板的生成equals()hashCode()compareTo()使用Apache Commons Lang中的建设者。

回答by bruno conde

You can do that with Code Templates in Eclipse.

您可以使用 Eclipse 中的代码模板来做到这一点。

Here's a solutionthat I found with examples of HashCodeBuilder and EqualsBuilder.

这是我通过 HashCodeBuilder 和 EqualsBuilder 的示例找到的解决方案

Template EqualsBuilder:

模板 EqualsBuilder:

    public boolean equals(Object o) {
        boolean result = false;

        if (this == o) {
            result = true;
        } else if (o instanceof $CLASSNAME$) {
            $CLASSNAME$ other = ($CLASSNAME$) o;

            result = new org.apache.commons.lang.builder.EqualsBuilder()
                    .append($END$
                    .isEquals();
        }

        return result;
    }

Template HashCodeBuilder:

模板 HashCodeBuilder:

    public int hashCode() {
        return new org.apache.commons.lang.builder.HashCodeBuilder()
                .append( $END$ )
                .toHashCode();
    }

回答by Chinnery

I use the Eclipse plugin called "Commonclipse"

我使用名为“Commonclipse”的 Eclipse 插件

After installation, you see a new context menu item "commonclipse" when you right click within a java source file. It can generate equals, hashcode, toString and compareTo methods based on the Apache commons libraries.

安装后,当您在 Java 源文件中右键单击时,您会看到一个新的上下文菜单项“commonclipse”。它可以基于 Apache 公共库生成 equals、hashcode、toString 和 compareTo 方法。

To install it, use this from within eclipse update: http://commonclipse.sourceforge.net

要安装它,请在 eclipse 更新中使用它:http: //commonclipse.sourceforge.net

回答by Sebastian D'Agostino

I made this template checking several answers, websites and testing it on Eclipse Luna. Go to Windows->Preferences and then to Java->Editor->Templates and add it there.

我制作了这个模板,检查了几个答案、网站并在 Eclipse Luna 上对其进行了测试。转到 Windows-> 首选项,然后转到 Java-> 编辑器-> 模板并将其添加到那里。

${:import(org.apache.commons.lang3.builder.HashCodeBuilder, org.apache.commons.lang3.builder.EqualsBuilder)}
@Override
public int hashCode() {
    HashCodeBuilder hashCodeBuilder = new HashCodeBuilder();
    hashCodeBuilder.append(${field1:field});
    hashCodeBuilder.append(${field2:field});
    hashCodeBuilder.append(${field3:field});
    hashCodeBuilder.append(${field4:field});
    hashCodeBuilder.append(${field5:field});
    return hashCodeBuilder.toHashCode();
}

@Override
public boolean equals(Object obj) {
    if (this == obj) {
        return true;
    }
    if (obj == null) {
        return false;
    }
    if (getClass() != obj.getClass()) {
        return false;
    }
    ${enclosing_type} rhs = (${enclosing_type}) obj;
    EqualsBuilder equalsBuilder = new EqualsBuilder();
    equalsBuilder.append(${field1}, rhs.${field1});
    equalsBuilder.append(${field2}, rhs.${field2});
    equalsBuilder.append(${field3}, rhs.${field3});
    equalsBuilder.append(${field4}, rhs.${field4});
    equalsBuilder.append(${field5}, rhs.${field5});${cursor}
    return equalsBuilder.isEquals();
}

回答by Jens Geiregat

Eclipse java code templates for eclipse 3.5.0, derived from Bruno Conde's templates:

Eclipse 3.5.0 的 Eclipse java 代码模板,源自 Bruno Conde 的模板:

@Override
public boolean equals(Object obj) {
    if (obj == null) {
        return false;
    } else if (obj == this) {
        return true;
    } else if (obj.getClass() != this.getClass()) {
        return false;
    }

    ${enclosing_type} other = (${enclosing_type}) obj;
    return new EqualsBuilder()//
            .appendSuper(super.equals(other))//
            .append(${cursor})//
                .isEquals();
}

and

@Override
public int hashCode() {
    return new HashCodeBuilder(${cursor})//
            .append()//
            .toHashCode();
}