2015-01-13 3 views
5

के साथ जेनरेट की गई कक्षाओं के तरीकों के लिए क्या यह बराबर उत्पन्न करना और तुलना करना संभव है जैक्सबी से उत्पन्न कक्षाओं के तरीकों के लिए, मैं स्कीमा से कक्षाएं उत्पन्न करने के लिए जैक्सबी का उपयोग करता हूं। इन वर्गों में वास्तव में ऐसे ग्रिड होते हैं जो उन्हें विशिष्ट रूप से पहचाने जाने की अनुमति देते हैं लेकिन मैं बराबर/तुलना विधि को कैसे कार्यान्वित कर सकता हूं ताकि सेट क्लास जैसे संग्रह वर्ग एक ही इकाई के डुप्लिकेट उदाहरणों को पहचान सकें?क्या यह बराबर उत्पन्न करना और तुलना करना संभव है जैक्सबी

+1

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

+0

का संभावित डुप्लिकेट कोई अलग नहीं है, क्योंकि विधियों के आधार पर विधियां बनाना चाहते हैं वर्ग के एक विशेष तत्व, सभी तत्वों को देखकर नहीं।अभी के लिए मैं जिस समाधान का उपयोग कर रहा हूं वह हमेशा आईडी के आधार पर कस्टम तुलनित्र के साथ ट्रीसेट का उपयोग करना है। –

उत्तर

3

ठीक है, यहां एक और दृष्टिकोण है।

hashCode और equals विधियों को जोड़ने के लिए आप -XcodeInjector प्लगइन का उपयोग कर सकते हैं। यदि यह पर्याप्त नहीं है (

<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/के बराबर होती है के लिए गुण का चयन की अनुमति दें":

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

कुछ की तरह:

इस सवाल देखें) या अपनी खुद की प्लगइन को लागू करना।

+0

धन्यवाद जो एक समाधान की तरह दिखता है –

1

अस्वीकरण: मैं jaxb2-basics के लेखक जो JAXB2 hashCode और equals तरीकों पैदा करने में सक्षम प्लगइन्स प्रदान करता हूँ।

यहाँ 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 देखें)

आप उपयोग कर सकते हैं -XsimpleHashCode और -XsimpleEquals जो क्रम-रहित hashCode और equals विधियों (हैश कोड उत्पन्न या गणना के बराबर होती है रेखांकित है) या -XhashCode/-Xequals जो "रणनीतिक" hashCode और 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); 
    } 

} 

मेरी पीओवी से, "सामरिक" संस्करणों में अधिक शक्तिशाली हैं। कक्षाएं HashCode या Equals इंटरफेस लागू करती हैं जो लोकेटर और हैश कोड/बराबर रणनीतियों को स्वीकार करती हैं। यह आपको हैश कोड गणना या बाहर से तुलना को नियंत्रित करने की अनुमति देता है। मैं अक्सर यूनिट परीक्षणों में इसका उपयोग न केवल यह जांचने के लिए करता हूं कि दो ऑब्जेक्ट बराबर हैं या नहीं, बल्कि यह पता लगाने के लिए कि वे कहां भिन्न हैं।

दोनों प्लगइन्स प्रतिबिंब मुक्त विधियां उत्पन्न करते हैं (यह प्रदर्शन के लिए महत्वपूर्ण है)। वे जेएक्सबी विशेष मामलों जैसे JAXBElement एस, आदिम सरणी आदि पर भी विचार करते हैं।

+0

धन्यवाद, लेकिन थोड़ा उलझन में आप जाक्सब के बजाय या साथ ही जैक्सबैबिक्स का उपयोग करते हैं? –

+0

जेएक्सबी 2 मूल बातें जेएक्सबी/एक्सजेसी के लिए प्लगइन का एक सेट है, आप इसका उपयोग नहीं कर सकते * इसके बजाय * आपको * JAXB/XJC के साथ * उनका उपयोग करना होगा। – lexicore

+0

ठीक है, मेरे वर्गों में पहले से ही एक अद्वितीय आईडी फ़ील्ड है, इसलिए बराबर और हैशकोड उत्पन्न करने के लिए उस क्षेत्र का उपयोग करने का कोई तरीका है। –

संबंधित मुद्दे