All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.mapstruct.Qualifier Maven / Gradle / Ivy

There is a newer version: 1.6.3
Show newest version
/*
 * Copyright MapStruct Authors.
 *
 * Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
 */
package org.mapstruct;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * 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: *

    *
  • {@link Mapping#qualifiedBy() }
  • *
  • {@link BeanMapping#qualifiedBy() }
  • *
  • {@link IterableMapping#qualifiedBy() }
  • *
  • {@link MapMapping#keyQualifiedBy() }
  • *
  • {@link MapMapping#valueQualifiedBy() }
  • *
  • {@link SubclassMapping#qualifiedBy() }
  • *
*

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 {@link RetentionPolicy#CLASS}. * * @author Sjaak Derksen * @see Named */ @Target(ElementType.ANNOTATION_TYPE) @Retention(RetentionPolicy.CLASS) public @interface Qualifier { }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy