In this post under MapStruct, I will explain with example the purpose of “MappingConstants.ANY_REMAINING” enum constant.
This enum constant is basically used when mapping source enum constant to destination enum constant.
This enum constant is used in mapping interface to represent enum constants which don’t have explicit “@ValueMapping” annotation.
Let me explain with an example
Let’s say we have below source enum
Source Enum
package package16;
public enum ProcessStatus {
RUNNING,
PAUSE,
CANCELLED,
TERMINATED,
STOPPED;
}
and below destination enum
Destination enum
package package16;
public enum DisplayStatus {
IN_PROGRESS,
PAUSE,
CANCELLED,
SHUTDOWN;
}
Now if we want to define a mapping where except “RUNNING”, “PAUSE”, and “CANCELLED” all other remaining enum constant should be mapped to “SHUTDOWN”.
Our mapper interface will be like below
Mapper Interface
package package16;
import org.mapstruct.Mapper;
import org.mapstruct.MappingConstants;
import org.mapstruct.ValueMapping;
import org.mapstruct.ValueMappings;
@Mapper
public interface ProcessToDisplayStatusMapper {
@ValueMappings({
@ValueMapping(source = "RUNNING", target = "IN_PROGRESS"),
@ValueMapping(source = MappingConstants.ANY_REMAINING, target = "SHUTDOWN"),
})
DisplayStatus processStatusToDisplayStatusMap(ProcessStatus processStatus);
}
In the above code, at line 12, I am using “MappingConstants.ANY_REMAINING” to represent source enum contant which do not have explicit “@ValueMapping” annotation.
As a result “STOPPED” and “TERMINATED” from source enum are mapped to “SHUTDOWN” from destination enum
Please note “PAUSE” and “CANCELLED” in source enum will be automatically mapped to destination enum constant with same name. You don’t need to defind explicit “@ValueMapping” annotation.
In this way we can use enum constant “MappingConstants.ANY_REMAINING”
Below is the main class for your reference
Main class
package package16;
import org.mapstruct.factory.Mappers;
public class Example16 {
public static void main(String[] args) {
ProcessToDisplayStatusMapper processToDisplayStatusMapper = Mappers.getMapper(ProcessToDisplayStatusMapper.class);
DisplayStatus displayStatus = processToDisplayStatusMapper.processStatusToDisplayStatusMap(ProcessStatus.RUNNING);
System.out.println("RUNNING is mapped to " + displayStatus);
displayStatus = processToDisplayStatusMapper.processStatusToDisplayStatusMap(ProcessStatus.PAUSE);
System.out.println("PAUSE is mapped to " + displayStatus);
displayStatus = processToDisplayStatusMapper.processStatusToDisplayStatusMap(ProcessStatus.CANCELLED);
System.out.println("CANCELLED is mapped to " + displayStatus);
displayStatus = processToDisplayStatusMapper.processStatusToDisplayStatusMap(ProcessStatus.STOPPED);
System.out.println("STOPPED is mapped to " + displayStatus);
displayStatus = processToDisplayStatusMapper.processStatusToDisplayStatusMap(ProcessStatus.TERMINATED);
System.out.println("TERMINATED is mapped to " + displayStatus);
}
}
Below is the output
Output
RUNNING is mapped to IN_PROGRESS
PAUSE is mapped to PAUSE
CANCELLED is mapped to CANCELLED
STOPPED is mapped to SHUTDOWN
TERMINATED is mapped to SHUTDOWN
As you can see from the output “STOPPED” and “TERMINATED” from source enum for which there is no explicit “@ValueMapping” annotation is automatically mapped to “SHUTDOWN”