This project has retired. For details please refer to its Attic page.
Conversion
Qi4j
Introduction
Tutorials
Javadoc
Samples
Core
Libraries
Extensions
Tools
Glossary 

Conversion

code

docs

tests

The Conversion Library provides support for converting composite types.

Table 26. Artifact

Group IDArtifact IDVersion

org.qi4j.library

org.qi4j.library.conversion

2.0


Entities to Values

To convert Entities to Values, use the EntityToValueService. It is easily assembled:

module.services( EntityToValueService.class );

Let’s say we have an interface defining state:

public interface PersonState
{

    Property<String> firstName();

    Property<String> lastName();

    Property<Date> dateOfBirth();

}

An EntityComposite using the state as a Private Mixin:

@Mixins( PersonMixin.class )
public interface PersonEntity
    extends EntityComposite
{

    String firstName();

    String lastName();

    Integer age();

    @Optional
    Association<PersonEntity> spouse();

    ManyAssociation<PersonEntity> children();

}
  [...snip...]

public static abstract class PersonMixin
    implements PersonEntity
{

    @This
    private PersonState state;
      [...snip...]

}

And a ValueComposite extending this very same state;

public interface PersonValue
    extends PersonState, ValueComposite
{

    @Optional
    Property<String> spouse();

    @Optional
    Property<List<String>> children();

}

Here is how to convert an EntityComposite to a ValueComposite:

EntityToValueService conversion = module.findService( EntityToValueService.class ).get();
PersonValue value = conversion.convert( PersonValue.class, entity );

Associations are converted to Identity strings.

If your Entities and Values cannot use the same state type, you can annotate the Value that is the target of the conversion with the @Unqualified annotation. Then, the lookup of the Value Property will be performed using the unqualified name only, and not via the default of the full qualified name. In other words, this means that the Property may be declared in the different interfaces and still be matched.

Here is an example:

@Unqualified
public interface PersonValue2
    extends ValueComposite
{

    Property<String> firstName();

    Property<String> lastName();

    Property<Date> dateOfBirth();

    @Optional
    Property<String> spouse();

    @Optional
    Property<List<String>> children();

}