Reputation: 1371
I'm using EMF to model a lot of different kind of objects and now I want to go through a EList of any kind of objects and want to know if this EList contains an object with the same values for all fields that are of interest for me (defined in the equals method). The problem is that I want to use the equals method which I overloaded for my object and not the ==-Operator which only returns true if it is the same object, but in my case I create a new object set the fields and now I want to know if this object with the same fields is already in the list. I haven't figured out yet if there is any settings in EMF where I can change the behaviour of the ELIst but currently I found the following code and it seems that every time when I use the method contains it only compares the object with the ==-Operator because the EObjectList overloads the useEquals method which always returns false.
public class BasicEList<E> extends AbstractEList<E> {
/**
* Returns whether the list contains the object.
* This implementation uses either <code>equals</code> or <code>"=="</code> depending on {@link #useEquals useEquals}.
* @param object the object in question.
* @return whether the list contains the object.
* @see #useEquals
*/
@Override
public boolean contains(Object object)
{
if (useEquals() && object != null)
{
for (int i = 0; i < size; ++i)
{
if (object.equals(data[i]))
{
return true;
}
}
}
else
{
for (int i = 0; i < size; ++i)
{
if (data[i] == object)
{
return true;
}
}
}
return false;
}
}
public class EObjectEList<E> extends EcoreEList<E> {
--> Output skipped
@Override
protected boolean useEquals()
{
return false;
}
}
So my question is if anyone does know if I can change the behaviour of the EList or if I have to write my own utility class which performs the matching?
Upvotes: 1
Views: 842
Reputation: 381
If it's not too late, here is my hint using simple OO tricks (for references) :
When you use a getter method such as :
yourObject.getYourReference();
You will have behind something like :
public EList<YourReference> getYourReference() {
if (reference == null) {
reference = new EObjectResolvingEList<YourReference>(YourReference.class, this,
YourPackage.YOUR_OBJECT_REFERENCE);
}
return reference ;
}
Just override the method useEquals()
and mark the getter has NOT generated :
/**
* generated NOT
*/
public EList<YourReference> getYourReference() {
if (reference == null) {
reference = new EObjectResolvingEList<YourReference>(YourReference.class, this,
YourPackage.YOUR_OBJECT_REFERENCE) {
@Override
protected boolean useEquals() {
return true;
}
};
}
return reference;
}
However, you have to check if this new behaviour do not have any side effets !
Upvotes: 2