我正在尝试创建一个脚本,该脚本收集将XML中的元素放入列表中,并验证特定对象是否为此列表的一部分。请在下面找到我的脚本:
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)
}
但是在创建集合之后,创建集合中存在的对象不会被识别为集合的一部分。我已经创建了以下代码来证明这一点:
println "Is iterator in collection: ${actualEntitlements.contains(segment1Entitlement)}"
println "Is object equals (I) to iterator: ${actualEntitlements[0] == segment1Entitlement}"
println "Is object equals (II) to iterator: " + actualEntitlements[0].equals(segment1Entitlement)
println "Is 'allowancePieces' members are equal: " + (actualEntitlements[0].allowancePieces == segment1Entitlement.allowancePieces)
println "Is 'ruleType' members are equal: " + (actualEntitlements[0].ruleType == segment1Entitlement.ruleType)
println "Is 'ruleId' members are equal: " + (actualEntitlements[0].ruleId == segment1Entitlement.ruleId)
println "Is 'passengerNameRef' members are equal: " + (actualEntitlements[0].passengerNameRef == segment1Entitlement.passengerNameRef)
println "Is 'segmentNumber' members are equal: " + (actualEntitlements[0].segmentNumber == segment1Entitlement.segmentNumber)
用于按成员比较对象成员的控制台输出如下所示:
Is iterator in collection: false
Is object equals (I) to iterator: true
Is object equals (II) to iterator: true
Is 'allowancePieces' members are equal: true
Is 'ruleType' members are equal: true
Is 'ruleId' members are equal: true
Is 'passengerNameRef' members are equal: true
Is 'segmentNumber' members are equal: true
你能不能告诉我为什么我的对象不被认为是收藏的一部分虽然会员成员比较返回&#39; true&#39;对于每个成员?
答案 0 :(得分:0)
因为你没有覆盖等于。您提供了自己的equals方法和不同的签名。覆盖正确的equals方法......
public boolean equals(Object o) {
... your code here...
}
...而Collection会识别您的对象,例如Collection.contains(...)
如果此collection包含指定的元素,则返回true。更正式地,当且仅当此集合包含至少一个元素e时才返回true(o == null?e == null:o.equals(e))。
(这意味着正确的equals方法而不是任何equals方法。参数必须是一个对象,否则它是一个不同的equals方法,对你没用。)
有一个方便的方法,如boolean equals(BaggageEntitlement that)
可能很适合其他目的(虽然我对此表示怀疑),但是这个方法不会被调用,因为大多数其他类都期望一个Object而不是一个“BaggageEntitlement”。 Equals是Object的基本方法之一,因此您应该始终覆盖它而不是创建自己的不同版本。