Gang,
I am looking at providing a general library for converting entity
state to a value composite and vice versa.
Simple example;
public interface PersonState
{
Property<String> firstName();
Property<String> lastName();
Property<Address> address();
}
public interface PersonValue extends PersonState, ValueComposite
{}
@Mixins( PersonMixin.class )
public interface PersonEntity extends PersonBehavior, EntityComposite
{
public class PersonMixin
implements PersonBehavior
{
@This
private PersonState state;
:
:
}
So, I have created a Service that can make the conversion under
certain constraints;
@Service
private EntityToValue entityToValue;
PersonEntity entity =...;
PersonValue value = entityToValue.convert( PersonValue.class, entity );
In this simple case, everything is straight forward. Properties that
are found in the Entity but is not present in the Value (identity in
the above for instance) will just be ignored.
This 'ignore' bit causes a NullPointerException in AbstractStateModel.
I can either catch that, but Exceptions are expensive, or I could
change setProperty() method to the following;
public void setProperty( QualifiedName name, Object value,
StateHolder valueState )
{
AbstractPropertyModel model =
propertiesModel.getPropertyByQualifiedName( name );
Property<Object> property = valueState.getProperty( model.accessor() );
if( property != null )
{
property.set( value );
}
}
Previous version doesn't do the null check.
This causes a silent 'fail' if the property was expected to be there.
A third solution is to throw a more specific exception there (or leave
the NPE) and instead introduce a ValueModel.hasProperty(QualifiedName)
method, which is used in the ValueBuilder callback visitor.
So, (I guess mostly Rickard) should I go with
1. Catch NPE in the library?
2. Change to the silent check in setProperty() method?
3. Introduce a hasProperty() method in ValueModel?
4. Forget the whole idea?
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/24svnvk
I relax here; http://tinyurl.com/2cgsug
_______________________________________________
qi4j-dev mailing list
[email protected]
http://lists.ops4j.org/mailman/listinfo/qi4j-dev