Package org.mapstruct
Annotation Type Condition
This annotation marks a method as a presence check method to check for presence in beans
or it can be used to define additional check methods for something like source parameters.
By default, bean properties are checked against null or using a presence check method in the source bean.
If a presence check method is available then it will be used instead.
Presence check methods have to return boolean.
The following parameters are accepted for the presence check methods:
- The parameter with the value of the source property.
e.g. the value given by calling
getName()for the name property of the source bean - only possible when using theConditionStrategy.PROPERTIES - The mapping source parameter
@Contextparameter-
@TargetPropertyNameparameter - only possible when using theConditionStrategy.PROPERTIES -
@SourcePropertyNameparameter - only possible when using theConditionStrategy.PROPERTIES
public class PresenceCheckUtils {
@Condition
public static boolean isNotEmpty(String value) {
return value != null && !value.isEmpty();
}
}
@Mapper(uses = PresenceCheckUtils.class)
public interface MovieMapper {
MovieDto map(Movie movie);
}
The following implementation of MovieMapper will be generated:
public class MovieMapperImpl implements MovieMapper {
@Override
public MovieDto map(Movie movie) {
if ( movie == null ) {
return null;
}
MovieDto movieDto = new MovieDto();
if ( PresenceCheckUtils.isNotEmpty( movie.getTitle() ) ) {
movieDto.setTitle( movie.getTitle() );
}
return movieDto;
}
}
This annotation can also be used as a meta-annotation to define the condition strategy.
- Since:
- 1.5
- Author:
- Filip Hrisafov
- See Also:
-
Optional Element Summary
Optional ElementsModifier and TypeOptional ElementDescriptionThe condition strategy for the condition.
-
Element Details
-
appliesTo
ConditionStrategy[] appliesToThe condition strategy for the condition. This determines whether the condition is applied to properties, parameters, or both.- Returns:
- the places where the condition should apply to
- Since:
- 1.6
- Default:
{PROPERTIES}
-