比较两个 XmlBeans 对象是否相等
我有一个名为 SynonymsRequest 的 XML Beans 接口:
public interface SynonymsRequest extends org.apache.xmlbeans.XmlObject {...}
我想测试 SynonymsRequest 的两个实例是否相等:
SynonymsRequest s1 = SynonymsRequest.Factory.newInstance();
s1.setQueryText("blub");
s1.setRequesterId(BigInteger.valueOf(1));
SynonymsRequest s2 = SynonymsRequest.Factory.newInstance();
s2.setQueryText("guck");
s2.setRequesterId(BigInteger.valueOf(1));
我尝试了以下操作:
assertTrue(s1.equals(s2));
=>断言未通过assertEquals(0, s1.compareTo(s2));
=>抛出 ClassCastExceptionassertEquals(0, s1.compareValue(s2));
=>;断言未通过(返回2,不可比较)assertTrue(s1.valueEquals(s2));
=>无论两个实例是否相等,总是返回 true
那么执行此操作的正确方法是什么?
I've an XML Beans Interface called SynonymsRequest with:
public interface SynonymsRequest extends org.apache.xmlbeans.XmlObject {...}
I want to test two instances of SynonymsRequest for equality:
SynonymsRequest s1 = SynonymsRequest.Factory.newInstance();
s1.setQueryText("blub");
s1.setRequesterId(BigInteger.valueOf(1));
SynonymsRequest s2 = SynonymsRequest.Factory.newInstance();
s2.setQueryText("guck");
s2.setRequesterId(BigInteger.valueOf(1));
I've tried the following:
assertTrue(s1.equals(s2));
=> assertion does not passassertEquals(0, s1.compareTo(s2));
=> throws ClassCastExceptionassertEquals(0, s1.compareValue(s2));
=> assertion does not pass (returns 2, not compareable)assertTrue(s1.valueEquals(s2));
=> always returns true, no matter if the two instances are equal
So what is the proper way of doing this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果它不影响程序的性能,您可以像这样比较它们:
否则,我想您将不得不编写自己的自定义比较器。
If it doesn't impact the performance of your program, you could compare them like this:
Otherwise, I guess you will have to write your own custom comparator.
据我了解,比较仅比较两个简单的值。它不能推导出你想要的比较算法。
或者我不明白你的意思到底是什么?
As I understand, the comparison compares two simple values only. It cannot deduct your desired comparison algorithm.
Or I don't understand what exactly do you mean?
XmlBeans 不支持深度比较,因此您必须编写自己的比较。不久前,开发邮件列表上有一个关于模式感知比较的主题,但我不确定它会发生什么:
http://www.mail-archive.com/[电子邮件受保护]/msg01960.html
XmlBeans doesn't support a deep comparison so you'll have to write your own. There was a thread on the dev mailing list a while ago about a schema-aware comparison, but I'm not sure anything became of it:
http://www.mail-archive.com/[email protected]/msg01960.html
不久前注意到这一点 - 如果两个对象在创建时生成了 toString() 方法,那么您可以在对象的 toString() 方法上使用 .equals 。这些可以相对轻松地进行比较,因为它们将检查输出 xml 是否等效。
Noticed this a while back - if the two objects have toString() methods generated when they were made, then you can to an .equals on the toString() methods of the objects. These can be compared with relativ ease, since they will check if the output xml is equivalent.