Package org.mapstruct
Annotation Type EnumMapping
-
@Target(METHOD) @Retention(CLASS) public @interface EnumMappingConfigured 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.StringconfigurationThe configuration that should be passed on the appropriate name transformation strategy.java.lang.StringnameTransformationStrategySpecifies the name transformation strategy that should be used for implicit mapping between enums.java.lang.Class<? extends java.lang.Exception>unexpectedValueMappingExceptionException that should be thrown by the generated code if no mapping matches.
-
-
-
Element Detail
-
nameTransformationStrategy
java.lang.String nameTransformationStrategy
Specifies the name transformation strategy that should be used for implicit mapping between enums. Known strategies are:MappingConstants.SUFFIX_TRANSFORMATION- applies the givenconfiguration()as a suffix to the source enumMappingConstants.STRIP_SUFFIX_TRANSFORMATION- strips the givenconfiguration()from the end of the source enumMappingConstants.PREFIX_TRANSFORMATION- applies the givenconfiguration()as a prefix to the source enumMappingConstants.STRIP_PREFIX_TRANSFORMATION- strips the givenconfiguration()from the start of the source enum-
MappingConstants.CASE_TRANSFORMATION- applies the givenconfiguration()case transformation to the source enum. Supported configurations are:- upper - Performs upper case transformation to the source enum
- lower - Performs lower case transformation to the source enum
- capital - Performs capitalisation of the first character of every word in the source enum and everything else to lower case. A word is split by "_".
EnumTransformationStrategySPI.- Returns:
- the name transformation strategy
- 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 viaMapperConfig.unexpectedValueMappingException()orMapper.unexpectedValueMappingException()will be used, usingIllegalArgumentExceptionby default.Note:
-
The defined exception should at least have a constructor with a
Stringparameter. - 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
-
The defined exception should at least have a constructor with a
-
-