【问题标题】:Is it possible to generate equals and compareTo methods for classes generated with jaxb是否可以为使用 jaxb 生成的类生成 equals 和 compareTo 方法
【发布时间】:2015-01-13 10:54:21
【问题描述】:

是否可以为使用 jaxb 生成的类生成 equals 和 compareTo 方法,我使用 jaxb 从模式生成类。这些类实际上具有允许它们被唯一标识的 guid,但是我如何实现一个 equals/compare 方法,以便像 Set 这样的 Collection 类可以识别同一实体的重复实例?

【问题讨论】:

  • 没有什么不同,因为要创建基于类的特定元素的方法,而不是通过查看所有元素。目前我使用的解决方案是始终使用带有自定义比较器的 TreeSet,基于所述 id。

标签: java jaxb equals


【解决方案1】:

好的,这是另一种方法。

您可以使用-XcodeInjector 插件添加hashCodeequals 方法。

看到这个问题:

Inserting code with XJC+xsd+jxb using the options " -Xinject-code -extension "

类似:

<jxb:bindings schemaLocation="schema.xsd">
    <jxb:bindings node="/xs:schema/xs:complexType[@name='MyItemType']">
        <ci:code>
            @Override
            public int hashCode() { return guid == null? 0 : guid.hashCode();}
        </ci:code>
    </jxb:bindings>
</jxb:bindings>

如果这还不够好,请考虑filing an issue in JAXB2-Basics(“允许选择 hashCode/equals 的属性”)或实现您自己的插件。

【讨论】:

  • 谢谢,这看起来像一个解决方案
【解决方案2】:

免责声明:我是jaxb2-basics 的作者,它提供能够生成hashCodeequals 方法的JAXB2 插件。

这是 Maven 的使用示例:

        <plugin>
            <groupId>org.jvnet.jaxb2.maven2</groupId>
            <artifactId>maven-jaxb2-plugin</artifactId>
            <executions>
                <execution>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <extension>true</extension>
                <args>
                    <arg>-Xequals</arg>
                    <arg>-XhashCode</arg>
                </args>
                <plugins>
                    <plugin>
                        <groupId>org.jvnet.jaxb2_commons</groupId>
                        <artifactId>jaxb2-basics</artifactId>
                        <version>...</version>
                    </plugin>
                </plugins>
            </configuration>
        </plugin>

(有关 Ant,请参阅 documentation。)

您可以使用-XsimpleHashCode-XsimpleEquals 生成无运行时的hashCodeequals 方法(内联哈希码或等于计算)或-XhashCode/-Xequals 生成“战略”@987654335 @ 和 equals 方法(哈希码/等于计算委托给传递的策略方法)。

这是-XsimpleHashCode 生成的内容:

public class Customer {

    ...

    public int hashCode() {
        int currentHashCode = 1;
        {
            currentHashCode = (currentHashCode* 31);
            String theAddress;
            theAddress = this.getAddress();
            if (theAddress!= null) {
                currentHashCode += theAddress.hashCode();
            }
        }
        {
            currentHashCode = (currentHashCode* 31);
            Boolean theBlueEyes;
            theBlueEyes = this.isBlueEyes();
            if (theBlueEyes!= null) {
                currentHashCode += theBlueEyes.hashCode();
            }
        }
        {
            currentHashCode = (currentHashCode* 31);
            String theFamilyName;
            theFamilyName = this.getFamilyName();
            if (theFamilyName!= null) {
                currentHashCode += theFamilyName.hashCode();
            }
        }
        {
            currentHashCode = (currentHashCode* 31);
            String theGivenName;
            theGivenName = this.getGivenName();
            if (theGivenName!= null) {
                currentHashCode += theGivenName.hashCode();
            }
        }
        {
            currentHashCode = (currentHashCode* 31);
            List<String> theMiddleInitials;
            theMiddleInitials = (this.isSetMiddleInitials()?this.getMiddleInitials():null);
            if (theMiddleInitials!= null) {
                currentHashCode += theMiddleInitials.hashCode();
            }
        }
        {
            currentHashCode = (currentHashCode* 31);
            String thePostCode;
            thePostCode = this.getPostCode();
            if (thePostCode!= null) {
                currentHashCode += thePostCode.hashCode();
            }
        }
        {
            currentHashCode = (currentHashCode* 31);
            boolean theSingle;
            theSingle = this.isSingle();
            currentHashCode += (theSingle? 1231 : 1237);
        }
        return currentHashCode;
    }

}

这是-XhashCode 生成的内容:

public class Customer implements HashCode
{

    ...

    public int hashCode(ObjectLocator locator, HashCodeStrategy strategy) {
        int currentHashCode = 1;
        {
            String theAddress;
            theAddress = this.getAddress();
            currentHashCode = strategy.hashCode(LocatorUtils.property(locator, "address", theAddress), currentHashCode, theAddress);
        }
        {
            Boolean theBlueEyes;
            theBlueEyes = this.isBlueEyes();
            currentHashCode = strategy.hashCode(LocatorUtils.property(locator, "blueEyes", theBlueEyes), currentHashCode, theBlueEyes);
        }
        {
            String theFamilyName;
            theFamilyName = this.getFamilyName();
            currentHashCode = strategy.hashCode(LocatorUtils.property(locator, "familyName", theFamilyName), currentHashCode, theFamilyName);
        }
        {
            String theGivenName;
            theGivenName = this.getGivenName();
            currentHashCode = strategy.hashCode(LocatorUtils.property(locator, "givenName", theGivenName), currentHashCode, theGivenName);
        }
        {
            List<String> theMiddleInitials;
            theMiddleInitials = (this.isSetMiddleInitials()?this.getMiddleInitials():null);
            currentHashCode = strategy.hashCode(LocatorUtils.property(locator, "middleInitials", theMiddleInitials), currentHashCode, theMiddleInitials);
        }
        {
            String thePostCode;
            thePostCode = this.getPostCode();
            currentHashCode = strategy.hashCode(LocatorUtils.property(locator, "postCode", thePostCode), currentHashCode, thePostCode);
        }
        {
            boolean theSingle;
            theSingle = this.isSingle();
            currentHashCode = strategy.hashCode(LocatorUtils.property(locator, "single", theSingle), currentHashCode, theSingle);
        }
        return currentHashCode;
    }

    public int hashCode() {
        final HashCodeStrategy strategy = JAXBHashCodeStrategy.INSTANCE;
        return this.hashCode(null, strategy);
    }

}

从我的 PoV 来看,“战略”版本更强大。类实现接受定位器和哈希码/等于策略的HashCodeEquals 接口。这允许您从外部控制哈希码计算或比较。我经常在单元测试中使用它,不仅可以检查两个对象是否相等,还可以准确地找出它们的不同之处。

两个插件都生成无反射方法(这对性能至关重要)。他们还考虑了 JAXB 特殊情况,例如 JAXBElements、原始数组等。

【讨论】:

  • 谢谢,但有点困惑你是用 jaxbbasics 代替 jaxb 还是 as well ?
  • JAXB2 Basics 是一组用于 JAXB/XJC 的插件,你不能使用它们而是你必须使用它们 with JAXB/XJC .
  • 好的,我的类已经有一个 uniqueId 字段,所以有没有办法只使用该字段来生成 equals 和 hashcode。
  • @PaulTaylor 这是一个新问题。
  • lexicore 如果您查看我的问题,它会说“这些类实际上具有允许唯一标识它们的 guid,但是我如何实现 equals/compare 方法”所以我的意思是基于唯一的 ID。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-24
相关资源
最近更新 更多