I think my second example should have had a few more comments. Basically this
would be possible:
ValueBuilder<X> b = vbf.newValueBuilder(X.class);
X p1 = b.prototype();
p1.att1().set("value1");
X inst1 = b.newInstance(); // p1 is now immutable
assertEquals("value1", inst1.att1().get());
assertNull(inst1.att2().get());
X p2 = b.prototype(); // p2 is a mutable copy of p1/inst1 containing the same
state
p2.att2().set("value2");
X inst2 = b.newInstance();
assertEquals("value1", inst1.att1().get());
assertEquals("value2", inst1.att2().get());
But NOT this:
ValueBuilder<X> b = vbf.newValueBuilder(X.class);
X p1 = b.prototype();
p1.att1().set("value1");
X inst1 = b.newInstance(); // p1 is now immutable
p1.att2().set("value2"); // BOOOM CRASH, p1 is now immutable - should have
fetched the prototype again.
So state is retained.
The point is that we can now avoid 'cloning' until we have already created a
new instance, and THEN requests a new prototype - thus cloning is only used if
the same valuebuilder is used for creating more than one value.
And performance of the cloning operation might be less critical - possibly
allowing us to postpone optimizations.
I really likeYour idea of constraining mutability in the builder phase to the
value 'in focus' - at least from a framework kind of view.
Not sure if it would make usage harder API-wise, though. But it could be a
start allowing better performance with slightly less functionality.
Den 25/06/2012 kl. 01.47 skrev Niclas Hedhman:
> On Mon, Jun 25, 2012 at 5:00 AM, Kent Sølvsten <[email protected]> wrote:
>
>> ValueBuilder<X> b = vbf.newValueBuilder(X.class);
>> X p1 = b.prototype();
>> p1.att1().set("value1");
>> X inst1 = b.newInstance(); // p1 is now immutable
>> X p2 = b.prototype();
>> p2.att1().set("value2");
>> X inst2 = b.newInstance();
>
> This effectively means that the cookie-cutter approach is not allowed,
> right? And the only actual save is to not require an
> vbf.newValueBuilder() call again...
>
> There is a another option as well, which isn't too hard.
> IF we define that only the 'prototype', i.e. the StateHolder of the
> ValueInstance being built, is read/write, but transitive values are
> not, then only the StateHolder needs to be cloned, which is far
> easier. That means one do;
>
> ValueBuilder<X> b = vbf.newValueBuilder(X.class);
> X p1 = b.prototype();
> p1.att1().set("value1");
> X instance1 = b.newInstance();
> p1.att1().set("value2");
> X instance2 = b.newInstance();
>
> but not;
> ValueBuilder<X> b = vbf.newValueBuilder(X.class);
> X p1 = b.prototype();
> p1.att1().set(value1);
> X instance1 = b.newInstance();
> p1.att1().get().att2().set("value2");
> X instance2 = b.newInstance();
>
> which kind of makes sense. The trick will be around how to prevent it...
>
>
> Cheers
> --
> Niclas Hedhman, Software Developer
> http://www.qi4j.org - New Energy for Java
>
> I live here; http://tinyurl.com/3xugrbk
> I work here; http://tinyurl.com/6a2pl4j
> I relax here; http://tinyurl.com/2cgsug
>
> _______________________________________________
> qi4j-dev mailing list
> [email protected]
> http://lists.ops4j.org/mailman/listinfo/qi4j-dev
>
_______________________________________________
qi4j-dev mailing list
[email protected]
http://lists.ops4j.org/mailman/listinfo/qi4j-dev