Skip to main content

Examples

Here are some solutions that use nativeTypes to define the behaviour of a SimpleValueType.

note

NativeTypes can only be defined as DataResources as part of an expansionResource.

Java Transport Class

You can define the type used in Projections and other transport classes for a SimpleValueType. Add a NativeType with concern transport and technology JDK.

<dataResource type="elements::NativeType">
<nativeType name="java:transport:net.demo.MyType">
<technology name="JDK"/>
<concern>transport</concern>
<!-- Definition should contain the fully qualified class name of the transport type. -->
<definition>net.demo.MyType</definition>
</nativeType>
</dataResource>
tip

The FieldComposite class has a property javaType which contains a representation of the transport class. Consult the javadoc of net.democritus.valuetype.JavaType to find out how to use it.

JPA Transport Class

Similarly, you can define the type used in JPA Entity classes (i.e. -Data classes). Use the technology JPA.

<dataResource type="elements::NativeType">
<nativeType name="jpa:transport:net.demo.MyJpaType">
<technology name="JDK"/>
<concern>transport</concern>
<!-- Definition should contain the fully qualified class name of the transport type. -->
<definition>net.demo.MyJpaType</definition>
</nativeType>
</dataResource>
tip

Several NativeTypes have already been defined for JPA and can be reused. E.g. String is represented by jpa:transport:String.

Converter Class

Some types need dedicated logic to convert between transport types. This class will need to implement conversion methods that map between specific transport types and the java transport type.

<dataResource type="elements::NativeType">
<nativeType name="java:converter:MyTypeConverter">
<technology name="JDK"/>
<concern>converter</concern>
<!-- Definition should contain the fully qualified class name of the converter class. -->
<definition>net.demo.MyTypeConverter</definition>
</nativeType>
</dataResource>

Undefined Checks

By default, value are seen as undefined only if they contain a null reference. It is possible to define another condition by adding a NativeType with the concern isUndefined and technology JDK.

<dataResource type="elements::NativeType">
<nativeType name="java:isUndefined:myType:notPresent">
<technology name="JDK"/>
<concern>isUndefined</concern>
<!--
Describe the condition in the definition.
$property$ will be replaced with the actual getter call for the value.
-->
<definition>$property$ == null || !$property$.isPresent()</definition>
</nativeType>
</dataResource>

Default Value

In constructors, values of SimpleValueTypes are initialized with the default constructor of the transport classes. This behaviour can be modified as necessary using the default concern.

<dataResource type="elements::NativeType">
<nativeType name="java:default:MyType">
<technology name="JDK"/>
<concern>default</concern>
<definition>MyType.empty()</definition>
</nativeType>
</dataResource>

The option defaultValue allows developers to set a default value in the model. The value of this option will be inserted one to one into the constructor.

<dataResource type="elements::NativeType">
<nativeType name="java:creation:MyType">
<technology name="JDK"/>
<concern>creation</concern>
<!--
Describe the code to construct a value in the definition.
$value$ will be replaced with the value of the option.
-->
<definition>new MyType("$value$")</definition>
</nativeType>
</dataResource>