groovy - Object isn't recognized as part of collection -
i'm trying create script gathers puts elements xml in list , verifies specific object part of list. please find script below:
class baggageentitlement{ string allowancepieces string ruletype string ruleid string passengernameref string segmentnumber boolean equals(baggageentitlement that) { return ( this.allowancepieces.equals(that.allowancepieces) && this.ruletype.equals(that.ruletype) && this.ruleid.equals(that.ruleid) && this.passengernameref.equals(that.passengernameref) && this.segmentnumber.equals(that.segmentnumber) ) } } arraylist<baggageentitlement> expectedentitlements = new arraylist<baggageentitlement>() arraylist<baggageentitlement> actualentitlements = new arraylist<baggageentitlement>() baggageentitlement segment1entitlement = new baggageentitlement(allowancepieces : '2', ruletype : 'override', ruleid : '15483', passengernameref: '01.01', segmentnumber: '1') def cbfnode = new xmlslurper().parsetext(messageexchange.getresponsecontentasxml()).body.calculatebagfeesrs def baggageentitlementnode = cbfnode.ancillaryoffers.itinerary.baggageentitlements baggageentitlementnode.entitlementitinerarypart.each{ def baggageallowanceentitlementnode = it.baggageallowanceentitlement baggageentitlement baggageentitlement = new baggageentitlement() baggageentitlement.allowancepieces = baggageallowanceentitlementnode.maxpieces.text().tostring() baggageentitlement.ruletype = baggageallowanceentitlementnode.@ruletype.tostring() baggageentitlement.ruleid = baggageallowanceentitlementnode.@ruleid.tostring() baggageentitlement.passengernameref = it.passengerreference.@namereferencenumber.tostring() baggageentitlement.segmentnumber = airidtosegmentnumber[it.segmentreference.@segmentnumber.tostring()] actualentitlements.add(baggageentitlement) }
but after creating collection, objects present in created collection aren't recognized part of collection. i've created following piece of code demonstrate this:
println "is iterator in collection: ${actualentitlements.contains(segment1entitlement)}" println "is object equals (i) iterator: ${actualentitlements[0] == segment1entitlement}" println "is object equals (ii) iterator: " + actualentitlements[0].equals(segment1entitlement) println "is 'allowancepieces' members equal: " + (actualentitlements[0].allowancepieces == segment1entitlement.allowancepieces) println "is 'ruletype' members equal: " + (actualentitlements[0].ruletype == segment1entitlement.ruletype) println "is 'ruleid' members equal: " + (actualentitlements[0].ruleid == segment1entitlement.ruleid) println "is 'passengernameref' members equal: " + (actualentitlements[0].passengernameref == segment1entitlement.passengernameref) println "is 'segmentnumber' members equal: " + (actualentitlements[0].segmentnumber == segment1entitlement.segmentnumber)
and console output comparing object member member looks like:
is iterator in collection: false object equals (i) iterator: true object equals (ii) iterator: true 'allowancepieces' members equal: true 'ruletype' members equal: true 'ruleid' members equal: true 'passengernameref' members equal: true 'segmentnumber' members equal: true
could tell me why object isn't considered part of collection though member member comparison returns 'true' every member?
because did not override equals. supplied own equals method different signature. override correct equals method...
public boolean equals(object o) { ... code here... }
...and collection recognize object, collection contract collection.contains(...)
returns true if collection contains specified element. more formally, returns true if , if collection contains @ least 1 element e such (o==null ? e==null : o.equals(e)).
(this means correct equals method , not equals method. parameter must object, otherwise different equals method , not you.)
having convenient method boolean equals(baggageentitlement that)
may nice other purposes (though doubt it), method won't called since other classes expect object , not "baggageentitlement". equals 1 of basic methods of object, should override instead of creating own different version.
Comments
Post a Comment