order of entries in EMap when load from xml [message #1059550] |
Mon, 20 May 2013 08:23  |
Eclipse User |
|
|
|
Hello,
I have an issue with the order of the entries of an eMap instance while loading an eObject from xml. I have this code to load the element into the the resource.
ResourceSet resourceSet = new ResourceSetImpl();
resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put
(Resource.Factory.Registry.DEFAULT_EXTENSION, new XMIResourceFactoryImpl());
// Register the package to ensure it is available during loading.
resourceSet.getPackageRegistry().put
(CorePackage.eNS_URI, CorePackage.eINSTANCE);
Resource resource = resourceSet.createResource(
URI.createFileURI(new File("document.xml").getAbsolutePath()));
Map<String, Object> options = new HashMap<String, Object>();
((XMLResource) resource).load(element, options);
Then, the xml part which is related to the eMap (chunks) looks like this:
<xmi:XMI xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" ...>
<myproject:Task ...>
...
<chunks key="Record" value="/6"/>
<chunks key="Sample" value="/7"/>
...
<myproject:Task/>
...
</xmi:XMI>
Now, after I created the Task eObject the order of the 2 chunks is inverted. When I debug the resource object I see the order as below. Moreover I see some null pointers: The eclipse debug output show something like this:
- resource = XMIResourceImpl
- contents = ResourceImpl$ContentsEList<E>
- data = EObject[19]
- [0] = TaskImpl
- chunks = ECoreEMap<K,V>
- delegateEList ...
- entryClass = ...
- entryData = BasicEList<E>[5]
- [0] = null
...
- [2] = ECoreEMap$1 //(->Sample)
...
- [5] = ECoreEMap$1 //(->Record)
In the model I can see that the "ordered" property for the EMap is set to true.
Is it possible to force the order of the entries of this EMap somehow to be consistent.
Thanks for your comments,
Marian
|
|
|
Re: order of entries in EMap when load from xml [message #1059553 is a reply to message #1059550] |
Mon, 20 May 2013 08:33  |
Eclipse User |
|
|
|
Marian,
Comments below.
On 20/05/2013 2:23 PM, Marian Thieme wrote:
> Hello,
>
> I have an issue with the order of the entries of an eMap instance
> while loading an eObject from xml. I have this code to load the
> element into the the resource.
>
>
> ResourceSet resourceSet = new ResourceSetImpl();
> resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put
> (Resource.Factory.Registry.DEFAULT_EXTENSION, new
> XMIResourceFactoryImpl());
>
> // Register the package to ensure it is available during loading.
> resourceSet.getPackageRegistry().put
> (CorePackage.eNS_URI, CorePackage.eINSTANCE);
>
> Resource resource = resourceSet.createResource(
> URI.createFileURI(new File("document.xml").getAbsolutePath()));
> Map<String, Object> options = new HashMap<String, Object>();
>
> ((XMLResource) resource).load(element, options);
>
>
>
> Then, the xml part which is related to the eMap (chunks) looks like this:
>
> <xmi:XMI xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" ...>
> <myproject:Task ...>
> ..
> <chunks key="Record" value="/6"/>
> <chunks key="Sample" value="/7"/>
> ..
> <myproject:Task/>
> ..
> </xmi:XMI>
>
>
> Now, after I created the Task eObject the order of the 2 chunks is
> inverted. When I debug the resource object I see the order as below.
> Moreover I see some null pointers: The eclipse debug output show
> something like this:
>
>
> - resource = XMIResourceImpl
> - contents = ResourceImpl$ContentsEList<E>
> - data = EObject[19]
> - [0] = TaskImpl
> - chunks = ECoreEMap<K,V>
> - delegateEList ...
> - entryClass = ...
> - entryData = BasicEList<E>[5]
> - [0] = null
> ...
> - [2] = ECoreEMap$1 //(->Sample)
> ...
> - [5] = ECoreEMap$1 //(->Record)
The entryData is the hashed index so you should not expect things to be
ordered in a meaningful way. An EMap is an EList which is a List. The
order of that list is reflected in the order of the elements in the
delegateEList, which is a containment list. I'm quite sure that will be
ordered properly and consistently.
>
>
> In the model I can see that the "ordered" property for the EMap is set
> to true.
> Is it possible to force the order of the entries of this EMap somehow
> to be consistent.
No, the entryData order is necessarily meaningless. If you want to
iterate over the elements in their "consistent" order, you can do that
directly on the EMap. It's implemented like this:
public Iterator<Map.Entry<K, V>> iterator()
{
return (Iterator<Map.Entry<K,
V>>)(Iterator<?>)delegateEList.iterator();
}
I.e., the delegateEList is the ordered list...
>
> Thanks for your comments,
> Marian
>
|
|
|
Powered by
FUDForum. Page generated in 0.05725 seconds