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
- Author:
- Filip Hrisafov
-
-
Optional Element Summary
Optional Elements Modifier and Type Optional Element Description String
configuration
The configuration that should be passed on the appropriate name transformation strategy.String
nameTransformationStrategy
Specifies the name transformation strategy that should be used for implicit mapping between enums.Class<? extends Exception>
unexpectedValueMappingException
Exception that should be thrown by the generated code if no mapping matches.
-
-
-
Element Detail
-
nameTransformationStrategy
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 "_".
EnumTransformationStrategy
SPI.- Returns:
- the name transformation strategy
- Default:
- ""
-
-
-
configuration
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
Class<? extends 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, usingIllegalArgumentException
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
-
The defined exception should at least have a constructor with a
-
-