Package org.mapstruct
Annotation Type Condition
-
@Target({METHOD,ANNOTATION_TYPE}) @Retention(CLASS) public @interface 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
@
Context
parameter-
@
TargetPropertyName
parameter - only possible when using theConditionStrategy.PROPERTIES
-
@
SourcePropertyName
parameter - 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:
SourceParameterCondition
- The parameter with the value of the source property.
e.g. the value given by calling
-
-
Optional Element Summary
Optional Elements Modifier and Type Optional Element Description ConditionStrategy[]
appliesTo
-
-
-
Element Detail
-
appliesTo
ConditionStrategy[] appliesTo
- Returns:
- the places where the condition should apply to
- Since:
- 1.6
- Default:
- {org.mapstruct.ConditionStrategy.PROPERTIES}
-
-