Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compare equality between EObject when they contained unordered EList?

I'm still a novice in EMF so maybe my question doesn't really make sense or I assume wrong things.

I'm working on a model-driven project, so I defined an ecore metamodel and generated the associate code. Now I'm currently trying to make unit tests and I need to be able to test equality between objects and more particularly between objects which extend EObject.

I tried to use EcoreUtil.equals() to make my tests but it always returns false as my objects contains references in lists (class EList) that are not ordered the same way. However, I explicitly defined in my metamodel that references are not ordered: I want to use them more like Set than List.

So, I finally decided to implements my own equals methods in my genereated *Impl Class, even if its discouraged in the javadoc, but it there another way, more elegant, to test the structural equality between EMF objects without taking into account the order of lists?

Thanks!

like image 225
Simon Urli Avatar asked Aug 30 '12 15:08

Simon Urli


People also ask

How do you compare equality of objects?

The behavior for performing loose equality using == is as follows: If the operands have the same type, they are compared as follows: Object: return true only if both operands reference the same object. String: return true only if both operands have the same characters in the same order.

How do you compare two lists of objects?

Java equals() method of List interface compares the specified object with the list for equality. It overrides the equals() method of Object class. This method accepts an object to be compared for equality with the list. It returns true if the specified object is equal to the list, else returns false.

How do you compare two lists irrespective orders in Java?

We can use the logic below to compare the equality of two lists using the assertTrue and assertFalse methods. In this first test, the size of both lists is compared before we check if the elements in both lists are the same. As both of these conditions return true, our test will pass.

How do you check two lists are equal or not and their values in Java?

You can compare two array lists using the equals() method of the ArrayList class, this method accepts a list object as a parameter, compares it with the current object, in case of the match it returns true and if not it returns false.


1 Answers

You can implement your own class of utilities where you code your own comparison for unordered lists using the EObject default equals method.

You can base your implementation in the EqualityHelper.equals(List list1, List list2) using list "contains" instead of going by index as that method does.

like image 122
maureyes Avatar answered Dec 08 '22 04:12

maureyes