Conditions
Conditional mapping of fields by predicates.
Conditions are used to determine wether a field should be mapped according to certain logic. In some use cases it is required to map a field from the source class only if some predicate is true, condition is that predicate.
We start with our two classes, our source class
SimpleEntity
and our destination class SimpleEntityDisplay
.Kotlin
Java
data class SimpleEntity(
val name: String
)
public class SimpleEntity {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Kotlin
Java
data class SimpleEntityDisplay(
val name: String = ""
)
public class SimpleEntityDisplay {
private String name = "";
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
To create a condition, create a new class implementing the
MappingCondition<T>
interface.Kotlin
Java
class NotBlankStringCondition : MappingCondition<String> {
override fun isValid(context: MappingConditionContext<String>): Boolean {
return !context.originalValue.isNullOrBlank()
}
}
public class NotBlankStringCondition implements MappingCondition<String> {
@Override
public boolean isValid(@NonNull MappingConditionContext<String> context) {
return context.getOriginalValue() != null && !context.getOriginalValue().trim().isEmpty();
}
}
The condition above checks that a string is not null or blank. After creating the condition class, all that is left is to use the condition.
Adding conditions to fields is available in both DSL and annotation mapping. Conditions can be added only to fields with the same type as the condition.
The condition can be added to annotation mapping using the
condition
attribute.Kotlin
Java
@DefaultMappingTarget(SimpleEntityDisplay::class)
data class SimpleEntity(
@MappedField(condition = NotBlankStringCondition::class)
val name: String,
)
@DefaultMappingTarget(SimpleEntityDisplay.class)
public class SimpleEntity {
@MappedField(condition = NotBlankStringCondition.class)
private String name;
// Getters and Setters...
}
We mapped the name field and added the condition. Now the name field will be mapped to
SimpleEntityDisplay
only if its value is not null or blank.The condition can be added to field mapping using the
withCondition
function. val mapper = mapper<SimpleEntity, SimpleEntityDisplay> {
SimpleEntity::name mappedTo SimpleEntityDisplay::name withCondition NotBlankStringCondition::class
}
Using the DSL, conditions can also be added inline.
val mapper = mapper<SimpleEntity, SimpleEntityDisplay> {
SimpleEntity::name mappedTo SimpleEntityDisplay::name withCondition {
!it.originalValue.isNullOrBlank()
}
}
Again, the name field will be mapped to
SimpleEntityDisplay
only if its value is not null or blank.The condition can be added to field mapping builder using the
withCondition
function. MappingDefinition mappingDefinition = new MappingDefinitionBuilder(SimpleEntity.class, SimpleEntityDisplay.class)
.mapField("name", "name")
.withCondition(NotBlankStringCondition.class)
.build();
Also with the builder, conditions can also be added inline.
MappingDefinition mappingDefinition = new MappingDefinitionBuilder(SimpleEntity.class, SimpleEntityDisplay.class)
.mapField("name", "name")
.withCondition(context -> context.getOriginalValue() != null && !((String) context.getOriginalValue()).trim().isEmpty())
.build();
Last modified 8mo ago