Package org.mapstruct

Annotation Type Qualifier


@Target(ANNOTATION_TYPE) @Retention(CLASS) public @interface Qualifier
Declares an annotation type to be a qualifier. Qualifier annotations allow unambiguously identify a suitable mapping method in case several methods qualify to map a bean property, iterable element etc.

Can be used in:

Example:


 // create qualifiers
 @Qualifier
 @Target(ElementType.TYPE)
 @Retention(RetentionPolicy.CLASS)
 public @interface TitleTranslator {}

 @Qualifier
 @Target(ElementType.METHOD)
 @Retention(RetentionPolicy.CLASS)
 public @interface EnglishToGerman {}

 @Qualifier
 @Target(ElementType.METHOD)
 @Retention(RetentionPolicy.CLASS)
 public @interface GermanToEnglish {}
 

 // we can create class with map methods
 @TitleTranslator
 public class Titles {
     @EnglishToGerman
     public String translateTitleEnglishToGerman(String title) {
         // some mapping logic
     }
     @GermanToEnglish
     public String translateTitleGermanToEnglish(String title) {
         // some mapping logic
     }
 }
 

 // usage
 @Mapper( uses = Titles.class )
 public interface MovieMapper {
      @Mapping( target = "title", qualifiedBy = { TitleTranslator.class, EnglishToGerman.class } )
      GermanRelease toGerman( OriginalRelease movies );
 }
 

 // generates
 public class MovieMapperImpl implements MovieMapper {
      private final Titles titles = new Titles();
      @Override
      public GermanRelease toGerman(OriginalRelease movies) {
          if ( movies == null ) {
              return null;
          }
          GermanRelease germanRelease = new GermanRelease();
          germanRelease.setTitle( titles.translateTitleEnglishToGerman( movies.getTitle() ) );
          return germanRelease;
     }
 }
 
NOTE: Qualifiers should have RetentionPolicy.CLASS.
Author:
Sjaak Derksen
See Also: