Examples
Here are some solutions that use nativeTypes to define the behaviour of a SimpleValueType.
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
.
- nativeTypes.xml
- MyType.xml
<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>
<valueType name="MyType" type="elements::SimpleValueType">
<nativeTypes>
<nativeType name="java:transport:net.demo.MyType"/>
</nativeTypes>
</valueType>
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
.
- nativeTypes.xml
- MyType.xml
<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>
<valueType name="MyType" type="elements::SimpleValueType">
<nativeTypes>
<nativeType name="jpa:transport:net.demo.MyJpaType"/>
</nativeTypes>
</valueType>
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.
- nativeTypes.xml
- MyType.xml
- MyTypeConverter.java
<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>
<valueType name="MyType" type="elements::SimpleValueType">
<nativeTypes>
<nativeType name="java:transport:net.demo.MyType"/>
<nativeType name="jpa:transport:net.demo.MyJpaType"/>
<nativeType name="java:converter:MyTypeConverter"/>
</nativeTypes>
</valueType>
package net.demo;
import net.democritus.validation.Result;
public class MyTypeConverter {
public Result<MyType> fromMyJpaType(MyJpaType value) {
// MyJpaType to MyType
}
public Result<MyJpaType> asMyJpaType(MyType value) {
// MyType to MyJpaType
}
}
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
.
- nativeTypes.xml
- MyType.xml
<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>
<valueType name="MyType" type="elements::SimpleValueType">
<nativeTypes>
<nativeType name="java:isUndefined:myType:notPresent"/>
</nativeTypes>
</valueType>
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.
- nativeTypes.xml
- MyType.xml
<dataResource type="elements::NativeType">
<nativeType name="java:default:MyType">
<technology name="JDK"/>
<concern>default</concern>
<definition>MyType.empty()</definition>
</nativeType>
</dataResource>
<valueType name="MyType" type="elements::SimpleValueType">
<nativeTypes>
<nativeType name="java:default:MyType"/>
</nativeTypes>
</valueType>
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.
- nativeTypes.xml
- MyType.xml
<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>
<valueType name="MyType" type="elements::SimpleValueType">
<nativeTypes>
<nativeType name="java:creation:MyType"/>
</nativeTypes>
</valueType>