Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » Wrap a mutable Java type into a Datatype?(The default value is statically created (same for all instances))
Wrap a mutable Java type into a Datatype? [message #1808982] Fri, 05 July 2019 12:59 Go to next message
Aurélien Mora is currently offline Aurélien MoraFriend
Messages: 38
Registered: July 2014
Member
Hello,

I wanted to use datatypes to wrap a mutable java class, but I just saw the default value is statically created (so all model objects containing this datatypes contains de facto the same instance).

For example, let's suppose this xcore:

class DateContainer
{
	Date date = "2012-12-12"
}

type Date wraps Date
create { ... }
convert { ... }


This code:
final var date1 = TestDateFactory.eINSTANCE.createDateContainer().getDate();
final var date2 = TestDateFactory.eINSTANCE.createDateContainer().getDate();

date1.setTime(124586786545L);

System.out.println("After date1 change:");
System.out.println("Date1: " + date1);
System.out.println("Date2: " + date2);


will print that:
After date1 change:
Date1: Thu Dec 13 07:26:26 CST 1973
Date2: Thu Dec 13 07:26:26 CST 1973

(Changing date1 also change date2)

Because DateContainer create the default value statically:
protected static final Date DATE_EDEFAULT = (Date)TestDateFactory.eINSTANCE.createFromString(TestDatePackage.eINSTANCE.getDate(), "2012-12-12");
protected Date date = DATE_EDEFAULT;



Is there any way to make the generated code recreate the datatype dynamically?
Re: Wrap a mutable Java type into a Datatype? [message #1808989 is a reply to message #1808982] Fri, 05 July 2019 13:36 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33141
Registered: July 2009
Senior Member
No. One should generally not ever mutate any wrapped data values and should even avoid wrapping such things in the first place, but too many people whined long ago wanting such a thing... So now you have it and should avoid misusing it. Of course changing the underlying value produces no notification so that's already bad news because such changes would not be noticed. And in general a date is a really bad representation with all kinds of problems because it's really just a long value that means different things in different places. But then there really are no good immutable representations for such things. Even Calendar and XMLGregorianCalendar are mutable. So perhaps you'd be best if someone is mutating the dates and you want to prevent that is to specialize your own Date and override the methods that mute the value.

Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Wrap a mutable Java type into a Datatype? [message #1808990 is a reply to message #1808989] Fri, 05 July 2019 13:58 Go to previous messageGo to next message
Aurélien Mora is currently offline Aurélien MoraFriend
Messages: 38
Registered: July 2014
Member
Ok, thank you. Yes, I saw the restrictions (no notifications etc...), but I want to avoid creating too much objects in a 60Hz loop (especially location vectors), to reduce the GC load. I took Date as example because it's in the wiki of xcore (https://wiki.eclipse.org/Xcore#Specifying_a_Data_Type).
Re: Wrap a mutable Java type into a Datatype? [message #1809003 is a reply to message #1808990] Fri, 05 July 2019 16:41 Go to previous message
Ed Willink is currently offline Ed WillinkFriend
Messages: 7655
Registered: July 2009
Senior Member
Hi

Surely your model just needs an ELong of 60Hz ticks since some agreed epoch. Rendering this value as text can use a variety of mutable algorithms.

Regards

Ed Willink
Previous Topic:Nullify string field after deleting its value
Next Topic:Teneo: Avoiding unnecessary joins
Goto Forum:
  


Current Time: Fri Apr 26 03:19:47 GMT 2024

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

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

Back to the top