Home » Modeling » EMF » Efficient check on EDataType(Trying to find out which EDataType you are dealing with)
Efficient check on EDataType [message #1740847] |
Thu, 18 August 2016 23:28 |
Johannes Gross Messages: 28 Registered: July 2009 |
Junior Member |
|
|
I am parsing some JSON and putting it in a EMF based model, and for literals i have to distinguish to set them.
Currently i am doing:
if (type.getName().equals("Real")) {
newobject.eSet(sf, Double.valueOf((String) ((JSONObject) je).get(property)));
} else if (type.getName().equals("Integer")) {
newobject.eSet(sf, Integer.valueOf((String) ((JSONObject) je).get(property)));
} else {
newobject.eSet(sf, ((JSONObject) je).get(property));
}
But i dont like it because string comparison is expensive and it should be an instanceof or equals object check. I could not get it to work however. EcorePackage.Literals.EString is just an int but not the type so i can't compare. As well as (type instanceof String) or EString didnt work for me. Any hints what the right approach is?
Thanks,
Joe
[Updated on: Wed, 24 August 2016 17:53] Report message to a moderator
|
|
|
Re: Efficiant check on EDataType [message #1740872 is a reply to message #1740847] |
Fri, 19 August 2016 09:15 |
Aurélien Mora Messages: 38 Registered: July 2014 |
Member |
|
|
Hello Joe,
I'm not sure to fully understand, what is your "type" variable ? an EMF object ? Anyway, if you want a quick comparison with no String, you probably should use an EEnum. You'll be able to use it like a java enum :
EType type = myObject.getType();
switch(type)
{
case INTEGER:
break;
case REAL:
break;
default:
break;
}
[Updated on: Fri, 19 August 2016 09:56] Report message to a moderator
|
|
|
Re: Efficiant check on EDataType [message #1740882 is a reply to message #1740872] |
Fri, 19 August 2016 10:44 |
Ed Merks Messages: 33224 Registered: July 2009 |
Senior Member |
|
|
It seems NNTP is broken so reposting this on the web forum.
Johannes,
Comments below.
On 19.08.2016 01:28, Johannes Mising name wrote:
> I am parsing some JSON and putting it in a EMF based model, and for literals i have to distinguish to set them.
> Currently i am doing:
>
> if (type.getName().equals("Real")) {
So type here an an EDataType presumably.
> newobject.eSet(sf, Double.valueOf((String) ((JSONObject) je).get(property)));
> } else if (type.getName().equals("Integer")) {
> newobject.eSet(sf, Integer.valueOf((String) ((JSONObject) je).get(property)));
> } else {
> newobject.eSet(sf, ((JSONObject) je).get(property));
> }
>
> But i dont like it because string comparison is expensive and it should be an instanceof or equals object check.
Well for Ecore you can assume identity. So testing type == XyzPackage.Literals.ABC is fine and of course highly efficient.
> I could not get it to work however. EcorePackage.Literals.EString is just an int but not the type so i can't compare.
No, your confusing EcorePackage.ESTRING, which is an int with EcorePackage.Literals.ESTRING, which is an EDataType.
> As well as (type instanceof String) or EString didnt work for me. Any hints what the right approach is?
I think better would be to use EcoreUtil.createFromString(type, (String) ((JSONObject) je).get(property))) in all cases; the factory knows how to convert a literal to a value of the data type. Of course I don't understand if it will always be a String, i.e., without your model, I don't know which cases the final else covers.
> Thanks,
> Joe
Ed Merks
Professional Support: https://www.macromodeling.com/
|
|
| |
Re: Efficiant check on EDataType [message #1741110 is a reply to message #1741088] |
Tue, 23 August 2016 07:42 |
Aurélien Mora Messages: 38 Registered: July 2014 |
Member |
|
|
Hello,
Actually, EcorePackage.Literals.ESTRING is not a type, it is an instance of EDataType, so you can't make an instanceof with that. If you need to make an instanceof, just use the interface EString.
But, in your example, both type and myType are not type of EStrings, they are EDataTypes. So I think the only comparison you can do is :
EDataType type = ((EAttribute) sf).getEAttributeType();
EDataType myType = EcorePackage.Literals.ESTRING;
if (type == myType) {
}
[Updated on: Tue, 23 August 2016 07:53] Report message to a moderator
|
|
| |
Re: Efficiant check on EDataType [message #1741988 is a reply to message #1740847] |
Fri, 19 August 2016 05:18 |
Ed Merks Messages: 33224 Registered: July 2009 |
Senior Member |
|
|
Johannes,
Comments below.
On 19.08.2016 01:28, Johannes Mising name wrote:
> I am parsing some JSON and putting it in a EMF based model, and for
> literals i have to distinguish to set them.
> Currently i am doing:
>
> if (type.getName().equals("Real")) {
So type here an an EDataType presumably.
> newobject.eSet(sf, Double.valueOf((String) ((JSONObject)
> je).get(property)));
> } else if
> (type.getName().equals("Integer")) {
> newobject.eSet(sf, Integer.valueOf((String) ((JSONObject)
> je).get(property)));
> } else {
> newobject.eSet(sf, ((JSONObject) je).get(property));
> }
>
> But i dont like it because string comparison is expensive and it
> should be an instanceof or equals object check.
Well for Ecore you can assume identity. So testing type ==
XyzPackage.Literals.ABC is fine and of course highly efficient.
> I could not get it to work however. EcorePackage.Literals.EString is
> just an int but not the type so i can't compare.
No, your confusing EcorePackage.ESTRING, which is an int with
EcorePackage.Literals.ESTRING, which is an EDataType.
> As well as (type instanceof String) or EString didnt work for me. Any
> hints what the right approach is?
I think better would be to use EcoreUtil.createFromString(type, (String)
((JSONObject) je).get(property))) in all cases; the factory knows how to
convert a literal to a value of the data type. Of course I don't
understand if it will always be a String, i.e., without your model, I
don't know which cases the final else covers.
> Thanks,
> Joe
Ed Merks
Professional Support: https://www.macromodeling.com/
|
|
|
Goto Forum:
Current Time: Sat Oct 12 04:41:42 GMT 2024
Powered by FUDForum. Page generated in 0.04755 seconds
|