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:

Note: The usage of this annotation is mandatory for a method to be considered as a presence check method.

 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 Elements
    Modifier and Type
    Optional Element
    Description
    The condition strategy for the condition.
  • Element Details

    • appliesTo

      ConditionStrategy[] appliesTo
      The 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}