MappingConstants.ANY_UNMAPPED example

In this post under MapStruct, I will explain with example the purpose of MappingConstants.ANY_UNMAPPED enum.

This enum is used when mapping two enums with different constant names.

It indicates MapStruct that any unmapped enum constant (i.e., source enum constant without any explicit mapping) must be mapped to a particular target enum constant. The specific target enum constant must be specified in the mapping.

Lets say we want to map “Day” enum

Day enum

package package13;

public enum Day {
    MONDAY,
    TUESDAY,
    WEDNESDAY,
    THURSDAY,
    FRIDAY,
    SATURDAY,
    SUNDAY;
}

to “Week” enum

Week enum

package package13;

public enum Week {
    WEEK_DAY,
    WEEK_END;
}

So enum constant “MONDAY”, “TUESDAY”, “WEDNESDAY”, “THURSDAY, and “FRIDAY” has to mapped to “WEEK_DAY”. And enum constant “SATURDAY” and “SUNDAY” to “WEEK_END”.

To achieve this, if we follow what we have learned till now, our Mapper interface will be as shown below

Current Mapper Interface

package package12;

import org.mapstruct.Mapper;
import org.mapstruct.ValueMapping;
import org.mapstruct.ValueMappings;

@Mapper
public interface DayToWeekMapper {
    @ValueMappings({
            @ValueMapping(source = "MONDAY", target = "WEEK_DAY"),
            @ValueMapping(source = "TUESDAY", target = "WEEK_DAY"),
            @ValueMapping(source = "WEDNESDAY", target = "WEEK_DAY"),
            @ValueMapping(source = "THURSDAY", target = "WEEK_DAY"),
            @ValueMapping(source = "FRIDAY", target = "WEEK_DAY"),
            @ValueMapping(source = "SATURDAY", target = "WEEK_END"),
            @ValueMapping(source = "SUNDAY", target = "WEEK_END")
    })
    Week dayToWeekMapping(Day day);
}

We can further improve the above code using “MappingConstants.ANY_UNMAPPED” as shown below

New Mapper Interface

package package13;

import org.mapstruct.Mapper;
import org.mapstruct.MappingConstants;
import org.mapstruct.ValueMapping;
import org.mapstruct.ValueMappings;

@Mapper
public interface DayToWeekMapper {
    @ValueMappings({
            @ValueMapping(source = "MONDAY", target = "WEEK_DAY"),
            @ValueMapping(source = "TUESDAY", target = "WEEK_DAY"),
            @ValueMapping(source = "WEDNESDAY", target = "WEEK_DAY"),
            @ValueMapping(source = "THURSDAY", target = "WEEK_DAY"),
            @ValueMapping(source = "FRIDAY", target = "WEEK_DAY"),
            @ValueMapping(source = MappingConstants.ANY_UNMAPPED, target = "WEEK_END")
    })
    Week dayToWeekMapping(Day day);
}

The mappings at line 15 and 16 in the current mapper interface is replaced by one mapping (refer to line 16) in new mapper interface.

The ValueMapping annotation at line 16 of new mapper interface is saying any unmapped enum constant (i.e., SATURDAY and SUNDAY in this case) must be mapped to “WEEK_END” enum constant.

So “MappingConstants.ANY_UNMAPPED” is used to cover mapping of source enum constants which are not explicitly mapped.

In this way we can use “MappingConstants.ANY_UNMAPPED” enum.

Leave a comment