Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » EStore and "equals"
EStore and "equals" [message #422431] Fri, 05 September 2008 13:53 Go to next message
Eclipse UserFriend
Originally posted by: swt.magellium.fr

Hello !

A simple question :
Is there a way to delegate the "equals" method implementation to an EStore ?

For the moment, I overrided the method in my base (root) model object,
but if tomorrow I want to use another model, I will have to override the
method again... And that's not really clean :(

Thanks in advance !
Stephane
Re: EStore and "equals" [message #422433 is a reply to message #422431] Fri, 05 September 2008 14:16 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33140
Registered: July 2009
Senior Member
Stephane,

No, EMF relies on object identity for equality so you mustn't override
..equals or hashCode.

* The framework also assumes that implementations will not specialize
{@link #equals(Object)} (nor {@link #hashCode()})
* so that "<code>==</code>" can be always used for equality testing;
* {@link org.eclipse.emf.ecore.util.EcoreUtil#equals(EObject, EObject)
EcoreUtil.equals} should be used for doing structural equality testing.

Why would you want to?


SWT wrote:
> Hello !
>
> A simple question :
> Is there a way to delegate the "equals" method implementation to an
> EStore ?
>
> For the moment, I overrided the method in my base (root) model object,
> but if tomorrow I want to use another model, I will have to override
> the method again... And that's not really clean :(
>
> Thanks in advance !
> Stephane


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: EStore and "equals" [message #422434 is a reply to message #422433] Fri, 05 September 2008 14:48 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: swt.magellium.fr

Ed,

All my objects are identified using unique IDs.

When I need to retrieve an object on the "persistent layer" (=> EStore),
I use a specific method similar to :
public EObject load(long uid);
But this method always returns new EObjects. For instance :
EObject a = load(1);
EObject b = load(1);
a.equals(b) => returns false.

But the expected behaviour is that "equals" should return true, because
they come from the same ID...

I think using ECoreUtil.equals(...) would return true, but the recursive
processing is time consuming, and in my case, I only need to compare the
ids...

Regards,
Stephane

Ed Merks wrote :
> Stephane,
>
> No, EMF relies on object identity for equality so you mustn't override
> .equals or hashCode.
> * The framework also assumes that implementations will not specialize
> {@link #equals(Object)} (nor {@link #hashCode()})
> * so that "<code>==</code>" can be always used for equality testing;
> * {@link org.eclipse.emf.ecore.util.EcoreUtil#equals(EObject, EObject)
> EcoreUtil.equals} should be used for doing structural equality testing.
>
> Why would you want to?
>
>
> SWT wrote:
>> Hello !
>>
>> A simple question :
>> Is there a way to delegate the "equals" method implementation to an
>> EStore ?
>>
>> For the moment, I overrided the method in my base (root) model object,
>> but if tomorrow I want to use another model, I will have to override
>> the method again... And that's not really clean :(
>>
>> Thanks in advance !
>> Stephane
Re: EStore and "equals" [message #422435 is a reply to message #422434] Fri, 05 September 2008 15:06 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33140
Registered: July 2009
Senior Member
Stephane,

Comments below.

SWT wrote:
> Ed,
>
> All my objects are identified using unique IDs.
>
> When I need to retrieve an object on the "persistent layer" (=>
> EStore), I use a specific method similar to :
> public EObject load(long uid);
> But this method always returns new EObjects. For instance :
> EObject a = load(1);
> EObject b = load(1);
> a.equals(b) => returns false.
You'll need to return the same object each time for a specific ID.
>
> But the expected behaviour is that "equals" should return true,
> because they come from the same ID...
>
> I think using ECoreUtil.equals(...) would return true, but the
> recursive processing is time consuming, and in my case, I only need to
> compare the ids...
>
> Regards,
> Stephane
>
> Ed Merks wrote :
>> Stephane,
>>
>> No, EMF relies on object identity for equality so you mustn't
>> override .equals or hashCode.
>> * The framework also assumes that implementations will not specialize
>> {@link #equals(Object)} (nor {@link #hashCode()})
>> * so that "<code>==</code>" can be always used for equality testing;
>> * {@link org.eclipse.emf.ecore.util.EcoreUtil#equals(EObject,
>> EObject) EcoreUtil.equals} should be used for doing structural
>> equality testing.
>>
>> Why would you want to?
>>
>>
>> SWT wrote:
>>> Hello !
>>>
>>> A simple question :
>>> Is there a way to delegate the "equals" method implementation to an
>>> EStore ?
>>>
>>> For the moment, I overrided the method in my base (root) model
>>> object, but if tomorrow I want to use another model, I will have to
>>> override the method again... And that's not really clean :(
>>>
>>> Thanks in advance !
>>> Stephane


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: EStore and "equals" [message #422661 is a reply to message #422435] Fri, 12 September 2008 08:47 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: swt.magellium.fr

Ed,

This works, (returning the same object each time the load() method is
called with the same ID) but ONLY if EObjects are created and retrieved
through this load() method :

// Standard loading
EObject a = load(1);
EObject b = load(1);

// With the new implementation, this works :
a.equals(b) => true

// We do the same thing as load(), without explicitly calling
// this load() method. The only difference is that the EObject
// is not referenced by the EStore.
EObject c = MyFactory.eINSTANCE.create(theEClass);
c.setId(1);
((InternalEObject) c).eSetStore(myEStore);
// The last statement is generally called when the EObject is attached
// to a Resource object which delegates to the Store...

// Here is the problem
a.equals(c) => false :'(

"a" and "c" have the same Id, so they should be equal (following my
design)...

Another solution : instead of overriding "equals", I can override
"eSetStore" in order for the store to be notified...
That may be cleaner than overriding equals, isn't it ?
Or am I missing something ?

Thanks again in advance !

Regards,
Stephane

Ed Merks wrote :
> Stephane,
>
> Comments below.
>
> SWT wrote:
>> Ed,
>>
>> All my objects are identified using unique IDs.
>>
>> When I need to retrieve an object on the "persistent layer" (=>
>> EStore), I use a specific method similar to :
>> public EObject load(long uid);
>> But this method always returns new EObjects. For instance :
>> EObject a = load(1);
>> EObject b = load(1);
>> a.equals(b) => returns false.
> You'll need to return the same object each time for a specific ID.
>>
>> But the expected behaviour is that "equals" should return true,
>> because they come from the same ID...
>>
>> I think using ECoreUtil.equals(...) would return true, but the
>> recursive processing is time consuming, and in my case, I only need to
>> compare the ids...
>>
>> Regards,
>> Stephane
>>
>> Ed Merks wrote :
>>> Stephane,
>>>
>>> No, EMF relies on object identity for equality so you mustn't
>>> override .equals or hashCode.
>>> * The framework also assumes that implementations will not specialize
>>> {@link #equals(Object)} (nor {@link #hashCode()})
>>> * so that "<code>==</code>" can be always used for equality testing;
>>> * {@link org.eclipse.emf.ecore.util.EcoreUtil#equals(EObject,
>>> EObject) EcoreUtil.equals} should be used for doing structural
>>> equality testing.
>>>
>>> Why would you want to?
>>>
>>>
>>> SWT wrote:
>>>> Hello !
>>>>
>>>> A simple question :
>>>> Is there a way to delegate the "equals" method implementation to an
>>>> EStore ?
>>>>
>>>> For the moment, I overrided the method in my base (root) model
>>>> object, but if tomorrow I want to use another model, I will have to
>>>> override the method again... And that's not really clean :(
>>>>
>>>> Thanks in advance !
>>>> Stephane
Re: EStore and "equals" [message #422665 is a reply to message #422661] Fri, 12 September 2008 11:21 Go to previous message
Ed Merks is currently offline Ed MerksFriend
Messages: 33140
Registered: July 2009
Senior Member
Stephane,

Comments below.


SWT wrote:
> Ed,
>
> This works, (returning the same object each time the load() method is
> called with the same ID) but ONLY if EObjects are created and
> retrieved through this load() method :
>
> // Standard loading
> EObject a = load(1);
> EObject b = load(1);
>
> // With the new implementation, this works :
> a.equals(b) => true
>
> // We do the same thing as load(), without explicitly calling
> // this load() method. The only difference is that the EObject
> // is not referenced by the EStore.
> EObject c = MyFactory.eINSTANCE.create(theEClass);
> c.setId(1);
> ((InternalEObject) c).eSetStore(myEStore);
> // The last statement is generally called when the EObject is attached
> // to a Resource object which delegates to the Store...
>
> // Here is the problem
> a.equals(c) => false :'(
The client should expect this. The framework will certainly expect
this. Creating a new object is a new object; the state of that object
doesn't affect it's identity; the framework expects equality to be
synonymous with identity and that state changes have no effect on either.
>
> "a" and "c" have the same Id, so they should be equal (following my
> design)...
It's in conflict with the framework's design.
>
> Another solution : instead of overriding "equals", I can override
> "eSetStore" in order for the store to be notified...
Yes, I'm not exactly sure why having equality defined the way you have
is important...
> That may be cleaner than overriding equals, isn't it ?
Not just cleaner, it won't cause downstream problems...
> Or am I missing something ?
Even in the rest of EMF. Two different resource sets can load the same
bytes to produce two different resources that are structurally equal
clones of the same data, and they aren't equal either...
>
> Thanks again in advance !
>
> Regards,
> Stephane
>
> Ed Merks wrote :
>> Stephane,
>>
>> Comments below.
>>
>> SWT wrote:
>>> Ed,
>>>
>>> All my objects are identified using unique IDs.
>>>
>>> When I need to retrieve an object on the "persistent layer" (=>
>>> EStore), I use a specific method similar to :
>>> public EObject load(long uid);
>>> But this method always returns new EObjects. For instance :
>>> EObject a = load(1);
>>> EObject b = load(1);
>>> a.equals(b) => returns false.
>> You'll need to return the same object each time for a specific ID.
>>>
>>> But the expected behaviour is that "equals" should return true,
>>> because they come from the same ID...
>>>
>>> I think using ECoreUtil.equals(...) would return true, but the
>>> recursive processing is time consuming, and in my case, I only need
>>> to compare the ids...
>>>
>>> Regards,
>>> Stephane
>>>
>>> Ed Merks wrote :
>>>> Stephane,
>>>>
>>>> No, EMF relies on object identity for equality so you mustn't
>>>> override .equals or hashCode.
>>>> * The framework also assumes that implementations will not
>>>> specialize {@link #equals(Object)} (nor {@link #hashCode()})
>>>> * so that "<code>==</code>" can be always used for equality testing;
>>>> * {@link org.eclipse.emf.ecore.util.EcoreUtil#equals(EObject,
>>>> EObject) EcoreUtil.equals} should be used for doing structural
>>>> equality testing.
>>>>
>>>> Why would you want to?
>>>>
>>>>
>>>> SWT wrote:
>>>>> Hello !
>>>>>
>>>>> A simple question :
>>>>> Is there a way to delegate the "equals" method implementation to
>>>>> an EStore ?
>>>>>
>>>>> For the moment, I overrided the method in my base (root) model
>>>>> object, but if tomorrow I want to use another model, I will have
>>>>> to override the method again... And that's not really clean :(
>>>>>
>>>>> Thanks in advance !
>>>>> Stephane


Ed Merks
Professional Support: https://www.macromodeling.com/
Previous Topic:[CDO] Listening to changes in a CDOResource
Next Topic:Ecore ID property
Goto Forum:
  


Current Time: Tue Apr 23 11:44:23 GMT 2024

Powered by FUDForum. Page generated in 0.03147 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top