Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » Do we have any class equivalent to LinkedHashMap in EMF?(Looking for a sortable/ordered EMap extension)
Do we have any class equivalent to LinkedHashMap in EMF? [message #1003801] Tue, 22 January 2013 02:35 Go to next message
Roza Ghamari is currently offline Roza GhamariFriend
Messages: 70
Registered: January 2013
Member
Hi all,

I am looking for keeping the order of my entries in the map based on the insertion order. So basically I am looking for creating a LinkedHashMap in my ecore model.

I need to persist this map, hence I'd like to use the serialization and proxy resolution feature of EMap. I tried to cast a LinkedHashMap to EcoreEmap by ((EMap.InternalMapView)myLinkedHashMap).eMap(); but that didn't work.

I can think of having a list and map together, and keep the order in list, but I'd really like to avoid duplication of reference, since these two can get out of sync.

Do you have any suggestions?



Re: Do we have any class equivalent to LinkedHashMap in EMF? [message #1004058 is a reply to message #1003801] Tue, 22 January 2013 14:05 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 26289
Registered: July 2009
Senior Member
Roza,

An EMap is a list. Doing a put of a new value will add an entry to the
end of the list, so it sounds like it already does what you need.


On 22/01/2013 2:35 PM, Roza Ghamari wrote:
> Hi all,
>
> I am looking for keeping the order of my entries in the map based on
> the insertion order. So basically I am looking for creating a
> LinkedHashMap in my ecore model.
>
> I need to persist this map, hence I'd like to use the serialization
> and proxy resolution feature of EMap. I tried to cast a LinkedHashMap
> to EcoreEmap by ((EMap.InternalMapView)myLinkedHashMap).eMap(); but
> that didn't work.
> I can think of having a list and map together, and keep the order in
> list, but I'd really like to avoid duplication of reference, since
> these two can get out of sync.
> Do you have any suggestions?
>
>
>
>
Re: Do we have any class equivalent to LinkedHashMap in EMF? [message #1004175 is a reply to message #1004058] Tue, 22 January 2013 18:31 Go to previous messageGo to next message
Roza Ghamari is currently offline Roza GhamariFriend
Messages: 70
Registered: January 2013
Member
Hi ED Merks,

Although an EMap is a list, but maps do not keep the order of insertions and they sort based on the Key. I have confirmed it by writing a test and the results showed that the map is sorted based on the keys (similar to java.util map). I should add that ecore uses ECoreEMap class which extends BaseEMap.
Re: Do we have any class equivalent to LinkedHashMap in EMF? [message #1004412 is a reply to message #1004175] Wed, 23 January 2013 07:25 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 26289
Registered: July 2009
Senior Member
Roza,

The method for put in org.eclipse.emf.common.util.BasicEMap.put(K, V)
looks like this.

public V put(K key, V value)
{
ensureEntryDataExists();

int hash = hashOf(key);
if (size > 0)
{
int index = indexOf(hash);
Entry<K, V> entry = entryForKey(index, hash, key);
if (entry != null)
{
V result = putEntry(entry, value);
didModify(entry, result);
return result;
}
}

Entry<K, V> entry = newEntry(hash, key, value);
delegateEList.add(entry);
return null;
}

It's clear that a new entry (a put for a key not already in the map) is
added to the end of the list. So the list is not sorted and the order
of that list appears to match exactly your stated needs.

Your general statement about maps doing sorting isn't true either. Only
things like SortedMap do sorting. Most maps maintain an order that's
effectively random based on the hashCodes of the keys and can be
completely reordered by the addition or removal of a key.

I'd be curious to see what test you wrote that confirms sorting but I'll
continue to assert that an EMap is a list and that the iterator
org.eclipse.emf.common.util.BasicEMap.iterator() which is implemented
like this

public Iterator<Map.Entry<K, V>> iterator()
{
return (Iterator<Map.Entry<K,
V>>)(Iterator<?>)delegateEList.iterator();
}

will return you the entries in list order and of course you have
complete control over that order and can even change it using the EMaps
move methods (because an EMap is also an EList).


On 22/01/2013 7:31 PM, Roza Ghamari wrote:
> Hi ED Merks,
>
> Although an EMap is a list, but maps do not keep the order of
> insertions and they sort based on the Key. I have confirmed it by
> writing a test and the results showed that the map is sorted based on
> the keys (similar to java.util map). I should add that ecore uses
> ECoreEMap class which extends BaseEMap.
Re: Do we have any class equivalent to LinkedHashMap in EMF? [message #1005307 is a reply to message #1004412] Thu, 24 January 2013 20:25 Go to previous message
Roza Ghamari is currently offline Roza GhamariFriend
Messages: 70
Registered: January 2013
Member
Thank you very much.
You are absolutely right, my fault was that since I suppressed the EMF type in generator I only got the map view of the EMap which does not keep the insertion ordered.

I really appreciate your help.
Previous Topic:Call for Submissions: Modeling Symposium EclipseCon North America 2013
Next Topic:EMF IncQuery for Eclipse 3.6
Goto Forum:
  


Current Time: Tue Dec 23 01:44:00 GMT 2014

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

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