org.mapstruct.Qualifier Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mapstruct Show documentation
Show all versions of mapstruct Show documentation
An annotation processor for generating type-safe bean mappers
/*
* 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 {
}