Hey,

I'm getting good progress on working with values through builders. The 
goal is to let all values be immutable, but then be able to modify them 
through ValueBuilders. Here's what I got working now:
// Create a new value
ValueBuilder<AnotherValue> anotherBuilder = 
valueBuilderFactory.newValueBuilder( AnotherValue.class );
// Set property
anotherBuilder.prototype().val1().set( "Val1" );
// Create instance
AnotherValue anotherValue = anotherBuilder.newInstance();

// Create a new value
ValueBuilder<SomeValue> builder = valueBuilderFactory.newValueBuilder( 
SomeValue.class );
SomeValue prototype = builder.prototype();
// Initialize state
prototype.some().set( "foo" );
prototype.other().set( "test" );
// List<String>
prototype.xyzzyList().get().add("blah");
// Property pointing to another value
prototype.another().set( anotherValue );
SomeValue instance = builder.newInstance();

assertThat("List has value blah", instance.xyzzyList().get().get( 0 ), 
equalTo("blah"));

// Modify value
builder = valueBuilderFactory.newValueBuilder( SomeValue.class 
).withPrototype( instance );
builder.prototype().some().set( "bar" );
instance = builder.newInstance();
---
The last instance will have all state set to the previous version, apart 
from some() which will have the new value "bar". This makes editing 
(immutable) values trivial! Note that this works both with properties 
pointing to primitives/Strings, other values, and also collections! You 
can therefore have an arbitrarily big value, and still be able to easily 
modify it.

Open questions are about naming. We are not sure that "withPrototype" 
and "prototype" are really good names. Examples of alternatives for 
"withPrototype" are:
"from", "use","with"
and "prototype":
"edit","build"

But it's all open for discussion. I'll commit it so you can check it out.

/Rickard

_______________________________________________
qi4j-dev mailing list
[email protected]
http://lists.ops4j.org/mailman/listinfo/qi4j-dev

Reply via email to