Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » Set<K> vs Map
Set<K> vs Map [message #421844] Thu, 14 August 2008 13:38 Go to next message
Eclipse UserFriend
Originally posted by: K_Juenemann.gmx.de

Hi,

I need to manage a collection of objects of type A which are considered
to be equal if a String member (it is actually a file path, so lets call
it "path") is equal. Besides, A can contain other data. The collection
should
.... ensure, that every A should be unique within the collection
(according to their "path" member)
.... should have an access time of O(1) to retrieve an A for a given path
.... and should be sorted according to the natural ordering of "path"
(for iterating over all values)
.... be typesafe, so ensure that only As are contained by the collection

So what I need is basically a SortedSet. Now I wondered about the right
way to model this with EMF. I know, that you should not override equals
and getHashCode, so I am not sure how to do this right. I see the
following option:

1) define the Datatype JavaTreeSet (which uses java.util.TreeSet)
Here I would have to override getHashcode and Equals I think. I also
wonder if there will be any problems with serialisation.

2) define a one to many reference to A, with unique and ordered = true.
But I would lack the O(1) access time.

3) use a EMap<String, A> which uses the path-member as a key and A's as
values. For some reason, this looks "hacky" to me a little bit, as I am
pulling the pathes out of the A's. This approach would also lack the
ordering.

So, what would be the best idea for modeling something like this? Maybe
I am lacking anything?

Cheers,
Konrad
Re: Set<K> vs Map [message #421847 is a reply to message #421844] Thu, 14 August 2008 14:00 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33142
Registered: July 2009
Senior Member
Konrad,

Comments below.

Konrad Juenemann wrote:
> Hi,
>
> I need to manage a collection of objects of type A which are
> considered to be equal if a String member (it is actually a file path,
> so lets call it "path") is equal.
I'm already getting concerned that you will be tempted to override
..equals and hashCode on your AImpl class, which is a no no.
> Besides, A can contain other data. The collection should
> ... ensure, that every A should be unique within the collection
> (according to their "path" member)
If you have an EReference to A you could define the "path" EAttribute to
be a key and validation will complain if there is more than one A with
the same key in that reference's value.
> ... should have an access time of O(1) to retrieve an A for a given path
Sounds like a map is needed.
> ... and should be sorted according to the natural ordering of "path"
> (for iterating over all values)
You have lots of constraints.
> ... be typesafe, so ensure that only As are contained by the collection
>
> So what I need is basically a SortedSet. Now I wondered about the
> right way to model this with EMF. I know, that you should not override
> equals and getHashCode, so I am not sure how to do this right. I see
> the following option:
Yes, don't do that.
>
> 1) define the Datatype JavaTreeSet (which uses java.util.TreeSet)
> Here I would have to override getHashcode and Equals I think. I also
> wonder if there will be any problems with serialisation.
I guess you can define a comparator and construct it with that.
>
> 2) define a one to many reference to A, with unique and ordered = true.
> But I would lack the O(1) access time.
EObject collections currently always force uniqueness, but that's based
on object identity. They're also always ordered, which means that order
has meaning and is preserved, not that they are sorted.
>
> 3) use a EMap<String, A> which uses the path-member as a key and A's
> as values. For some reason, this looks "hacky" to me a little bit, as
> I am pulling the pathes out of the A's. This approach would also lack
> the ordering.
But you do want a map given you want to find the A given a path in O(1)
time. Even if you didn't use EMF, you'd still want a map like that.
>
>
> So, what would be the best idea for modeling something like this?
> Maybe I am lacking anything?
I suppose given all the constraints, you're torn between wanting a
TreeMap and a TreeSet. As I said before, even without EMF you're a bit
in bind since a TreeMap is O(log(n)) I think. Perhaps you should has
convenience operations and just build a map the first time you need one
(much like EClass.getEStructuralFeature); you can alway use
ECollections.sort to sort an EList.
>
> Cheers,
> Konrad


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Set<K> vs Map [message #421849 is a reply to message #421847] Thu, 14 August 2008 14:25 Go to previous message
Eclipse UserFriend
Originally posted by: K_Juenemann.gmx.de

Wow, that was fast. :)

Uhm, I think you are right, Map + sorting + O(1) is not doable. So I
think I will drop the "sorted"-constraint and go for a HashMap / Set.
When I am thinking over it, a HashMap<String, A> might be best, as it
would guarantee uniqueness without forcing me to write a Comparer.

Thank you very much for your help!

Ed Merks schrieb:
> Konrad,
>
> Comments below.
>
> Konrad Juenemann wrote:
>> Hi,
>>
>> I need to manage a collection of objects of type A which are
>> considered to be equal if a String member (it is actually a file path,
>> so lets call it "path") is equal.
> I'm already getting concerned that you will be tempted to override
> ..equals and hashCode on your AImpl class, which is a no no.
>> Besides, A can contain other data. The collection should
>> ... ensure, that every A should be unique within the collection
>> (according to their "path" member)
> If you have an EReference to A you could define the "path" EAttribute to
> be a key and validation will complain if there is more than one A with
> the same key in that reference's value.
>> ... should have an access time of O(1) to retrieve an A for a given path
> Sounds like a map is needed.
>> ... and should be sorted according to the natural ordering of "path"
>> (for iterating over all values)
> You have lots of constraints.
>> ... be typesafe, so ensure that only As are contained by the collection
>>
>> So what I need is basically a SortedSet. Now I wondered about the
>> right way to model this with EMF. I know, that you should not override
>> equals and getHashCode, so I am not sure how to do this right. I see
>> the following option:
> Yes, don't do that.
>>
>> 1) define the Datatype JavaTreeSet (which uses java.util.TreeSet)
>> Here I would have to override getHashcode and Equals I think. I also
>> wonder if there will be any problems with serialisation.
> I guess you can define a comparator and construct it with that.
>>
>> 2) define a one to many reference to A, with unique and ordered = true.
>> But I would lack the O(1) access time.
> EObject collections currently always force uniqueness, but that's based
> on object identity. They're also always ordered, which means that order
> has meaning and is preserved, not that they are sorted.
>>
>> 3) use a EMap<String, A> which uses the path-member as a key and A's
>> as values. For some reason, this looks "hacky" to me a little bit, as
>> I am pulling the pathes out of the A's. This approach would also lack
>> the ordering.
> But you do want a map given you want to find the A given a path in O(1)
> time. Even if you didn't use EMF, you'd still want a map like that.
>>
>>
>> So, what would be the best idea for modeling something like this?
>> Maybe I am lacking anything?
> I suppose given all the constraints, you're torn between wanting a
> TreeMap and a TreeSet. As I said before, even without EMF you're a bit
> in bind since a TreeMap is O(log(n)) I think. Perhaps you should has
> convenience operations and just build a map the first time you need one
> (much like EClass.getEStructuralFeature); you can alway use
> ECollections.sort to sort an EList.
>>
>> Cheers,
>> Konrad
Previous Topic:EMF generated POJO containing dynamically overridden EClass
Next Topic:Object Action Contributions
Goto Forum:
  


Current Time: Fri Apr 26 23:50:32 GMT 2024

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

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

Back to the top