1. Overview

In this tutorial, we’ll see how to map the values of an enum to those of another enum with MapStruct. We’ll also learn how to throw an Exception when there’s no corresponding value in the other enum.

2. The MapStruct Library

MapStruct is a code generation tool that simplifies Java Beans mapping. The latest version of the MapStruct library can be found in the Maven Central Repository.

Let’s add the dependency to our pom.xml:

<dependency>
    <groupId>org.mapstruct</groupId>
    <artifactId>mapstruct</artifactId>
    <version>1.6.0.Beta1</version> 
</dependency>

Additionally, we need to add annotationProcessorPaths to the maven-compiler-plugin plugin in order to generate automatically the methods inside the project’s target folder:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.5.1</version>
    <configuration>
        <source>1.8</source>
        <target>1.8</target>
        <annotationProcessorPaths>
            <path>
                <groupId>org.mapstruct</groupId>
                <artifactId>mapstruct</artifactId>
                <version>1.6.0.Beta1</version>
            </path>
        </annotationProcessorPaths>
    </configuration>
</plugin>

3. Introduction to the Problem

First, let’s create our source enum. We’ll name it InputLevel, and it’ll have three possible values: HIGH, MEDIUM, and LOW:

enum InputLevel {

    LOW, MEDIUM, HIGH

}

We can now add the target enum. This one only contains two values HIGH and LOW:

enum OutputLevel {

    LOW, HIGH

}

We aim at converting an InputLevel to an OutputLevel. For instance, the input InputLevel.LOW gives us OutputLevel.LOW. However, there’s no matching value for MEDIUM. As a consequence, we want to throw an Exception in this case.

4. Throw When Source Has No Corresponding Target

We’ll use the Mapper annotation and create a Mapper interface. Since version 1.5.0.Beta1 of the MapStruct library, we can use the @ValueMapping annotation to achieve our goal:

@Mapper
interface LevelMapper {

    @ValueMapping(source = MappingConstants.ANY_REMAINING, target = MappingConstants.THROW_EXCEPTION)
    OutputLevel inputLevelToOutputLevel(InputLevel inputLevel);

}

As we can see, we configured the annotation so that mapping any value in the source without a corresponding target would result in an Exception.

Let’s now quickly check that the method correctly returns OutputLevel.HIGH when it’s given InputLevel.HIGH:

LevelMapper levelMapper = Mappers.getMapper(LevelMapper.class);

@Test
void givenHighInputLevel_WhenInputLevelToOutputLevel_ThenHighOutputLevel() {
    assertEquals(OutputLevel.HIGH, levelMapper.inputLevelToOutputLevel(InputLevel.HIGH));
}

Finally, we’ll confirm that when we try to convert InputLevel.MEDIUM into an OutputLevel, an Exception is thrown. Concretely, it throws an IllegalArgumentException:

@Test
void givenMediumInputLevel_WhenInputLevelToOutputLevel_ThenThrows() {
    assertThrows(IllegalArgumentException.class, () -> levelMapper.inputLevelToOutputLevel(InputLevel.MEDIUM));
}

5. Conclusion

In this article, we used the MapStruct library to map values from a source enum toward a target enum. Moreover, we configured our mapper to throw an Exception if the source value has no match in the target enum.

As always, the code is available over on GitHub.

 

Course – LS (cat=Java)

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE
res – REST with Spring (eBook) (everywhere)
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.