|
Re: Enforcing Uniqueness [message #1141667 is a reply to message #1140864] |
Thu, 17 October 2013 05:57 |
Ed Merks Messages: 33218 Registered: July 2009 |
Senior Member |
|
|
Jack,
Comments below.
On 16/10/2013 7:14 PM, Jack Mising name wrote:
> I am trying to control the content of a container. The contained
> objects are relatively simple. These objects are created from
> different sources and objects containing identical data can be created
> independently. Such different objects with identical data should be
> considered as identical and I need only one of them to exist in the
> collection even though several may be passed to add().
>
> Is there an 'obvious' way of achieving this that I am missing? I was
> expecting to find some mechanism like the Comparable interface where
> so that the object can determine it's uniqueness, but overriding
> equals() doesn't do the job.
And overriding equals is forbidden.
>
> Another approach would be to maybe subclass EList in the containing
> object to enforce constraints within add().
No, none of that is a good idea.
>
> Which is the correct approach?
I don't think there is a correct approach. If you model something with
an EClass you should expect the instances to be EObject that behave as
EObjects do. In particular they have identity so each one you create is
a different one. It may well be structurally equal to some other one,
but it's not identical. In addition, an EObject's state can change so
while it might be structurally equal to some other object at one point
in time, at a later point in time, that might not longer be the case.
So even if you "enforced uniqueness" you'd have to deal with enforcing
it remain unique in the face of arbitrary state changes to the instances.
So if you need all this enforcement, you probably should not be modeling
this with an EClass but rather as an EDataType, and then you can
implement equality as you see fit, and most likely should treat the data
values as immutable so that you don't have to deal with state changes
breaking your enforced uniqueness.
>
> TIA
>
> Jack
Ed Merks
Professional Support: https://www.macromodeling.com/
|
|
|
|
Powered by
FUDForum. Page generated in 0.05565 seconds