@Target(value=METHOD) @Retention(value=CLASS) public @interface EnumMapping
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;
}
}
Modifier and Type | Optional Element and 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.
|
public abstract String nameTransformationStrategy
MappingConstants.SUFFIX_TRANSFORMATION
- applies the given configuration()
as a
suffix to the source enumMappingConstants.STRIP_SUFFIX_TRANSFORMATION
- strips the the given configuration()
from the end of the source enumMappingConstants.PREFIX_TRANSFORMATION
- applies the given configuration()
as a
prefix to the source enumMappingConstants.STRIP_PREFIX_TRANSFORMATION
- strips the given configuration()
from
the start of the source enumEnumTransformationStrategy
SPI.public abstract String configuration
public abstract Class<? extends Exception> unexpectedValueMappingException
MapperConfig.unexpectedValueMappingException()
or
Mapper.unexpectedValueMappingException()
will be used, using IllegalArgumentException
by default.
Note:
String
parameter.
Copyright © 2012-2021 MapStruct Authors; All rights reserved. Released under the Apache Software License 2.0.