2015-01-13 38 views
5

是否有可能爲使用jaxb生成的類生成equals和compareTo方法,我使用jaxb從模式生成類。這些類實際上具有GUID,它們可以被唯一標識,但我如何實現一個equals/compare方法,以便集合類(如Set)能夠識別同一個實體的重複實例?是否有可能爲使用jaxb生成的類生成equals和compareTo方法

+1

可能的重複http://stackoverflow.com/questions/6523497/how-to-generate-equals-and-hashcode-methods-using-wsimport-in-jaxws –

+0

沒有什麼不同,因爲想創建基於這是班級中的一個特殊元素,而不是通過查看所有元素。目前我使用的解決方案是始終使用基於所述id的自定義比較器的TreeSet。 –

回答

3

好的,這是另一種方法。

您可以使用-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 /等於選擇屬性」 )或者實現你自己的插件。

+0

謝謝,看起來像一個解決方案 –

1

免責聲明:我的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> 

(見documentation爲Ant)

您可以使用-XsimpleHashCode-XsimpleEquals其產生運行時少hashCodeequals方法(散列碼或等於計算內聯)或-XhashCode/-Xequals,它們生成「戰略」hashCodeequals方法(將散列碼/等於計算委託給傳遞的策略方法) 。

這裏是-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特殊情況,如JAXBElement s,原始數組等。

+0

謝謝,但有點困惑,你使用jaxbbasics而不是jaxb或以及? –

+0

JAXB2 Basics是JAXB/XJC的一組插件,您不能使用它們,而必須使用* JAXB/XJC。 – lexicore

+0

好吧,我的類已經有一個uniqueId字段,所以有辦法使用該字段來生成equals和hashcode。 –

相關問題