Package org.mapstruct

Annotation Type EnumMapping


  • @Target(METHOD)
    @Retention(CLASS)
    public @interface EnumMapping
    Configured the mapping between two value types.

    Example: Using a suffix for enums

    
     public enum CheeseType {
         BRIE,
         ROQUEFORT
     }
    
     public enum CheeseTypeSuffixed {
         BRIE_TYPE,
         ROQUEFORT_TYPE
     }
    
     @Mapper
     public interface CheeseMapper {
    
         @EnumMapping(nameTransformationStrategy = "suffix", configuration = "_TYPE")
         CheeseTypeSuffixed map(Cheese cheese);
    
         @InheritInverseConfiguration
         Cheese map(CheeseTypeSuffixed cheese);
    
     }
     
    
     // generates
     public class CheeseMapperImpl implements CheeseMapper {
    
         @Override
         public CheeseTypeSuffixed map(Cheese cheese) {
             if ( cheese == null ) {
                 return null;
             }
    
             CheeseTypeSuffixed cheeseTypeSuffixed;
    
             switch ( cheese ) {
                 case BRIE:
                     cheeseTypeSuffixed = CheeseTypeSuffixed.BRIE_TYPE;
                     break;
                 case ROQUEFORT:
                     cheeseTypeSuffixed = CheeseTypeSuffixed.ROQUEFORT_TYPE;
                     break;
                 default:
                     throw new IllegalArgumentException( "Unexpected enum constant: " + cheese );
             }
    
             return cheeseTypeSuffixed;
         }
    
         @Override
         public Cheese map(CheeseTypeSuffixed cheese) {
             if ( cheese == null ) {
                 return null;
             }
    
             CheeseType cheeseType;
    
             switch ( cheese ) {
                 case BRIE_TYPE:
                     cheeseType = CheeseType.BRIE;
                     break;
                 case ROQUEFORT_TYPE:
                     cheeseType = CheeseType.ROQUEFORT;
                     break;
                 default:
                     throw new IllegalArgumentException( "Unexpected enum constant: " + cheese );
             }
    
             return cheeseType;
         }
     }
     
    Since:
    1.4
    • Optional Element Summary

      Optional Elements 
      Modifier and Type Optional Element Description
      java.lang.String configuration
      The configuration that should be passed on the appropriate name transformation strategy.
      java.lang.String nameTransformationStrategy
      Specifies the name transformation strategy that should be used for implicit mapping between enums.
      java.lang.Class<? extends java.lang.Exception> unexpectedValueMappingException
      Exception that should be thrown by the generated code if no mapping matches.
      • configuration

        java.lang.String configuration
        The configuration that should be passed on the appropriate name transformation strategy. e.g. a suffix that should be applied to the source enum when doing name based mapping.
        Returns:
        the configuration to use
        Default:
        ""
      • unexpectedValueMappingException

        java.lang.Class<? extends java.lang.Exception> unexpectedValueMappingException
        Exception that should be thrown by the generated code if no mapping matches. If no exception is configured, the exception given via MapperConfig.unexpectedValueMappingException() or Mapper.unexpectedValueMappingException() will be used, using IllegalArgumentException by default.

        Note:

        • The defined exception should at least have a constructor with a String parameter.
        • If the defined exception is a checked exception then the enum mapping methods should have that exception in the throws clause.
        Returns:
        the exception that should be used in the generated code
        Default:
        java.lang.IllegalArgumentException.class