BAEL-6717 Convert One Enum to Another Enum in Java (#14400)

- added new module core-java-lang-6
- added data model and test for enums
This commit is contained in:
Tetiana 2023-07-13 22:29:37 +03:00 committed by GitHub
parent 0e6ef73cdd
commit 8be07c6b36
11 changed files with 294 additions and 0 deletions

View File

@ -0,0 +1,7 @@
## Core Java Lang (Part 6)
This module contains articles about core features in the Java language
### Relevant Articles:
- [Convert One Enum to Another Enum in Java](<link - TODO>)

View File

@ -0,0 +1,52 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.baeldung.core-java-modules</groupId>
<artifactId>core-java-modules</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>core-java-lang-6</artifactId>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.mapstruct/mapstruct -->
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>${mapstruct.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>17</source>
<target>17</target>
<annotationProcessorPaths>
<!-- https://mvnrepository.com/artifact/org.mapstruct/mapstruct-processor -->
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${mapstruct.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<mapstruct.version>1.5.5.Final</mapstruct.version>
</properties>
</project>

View File

@ -0,0 +1,26 @@
package com.baeldung.enums.mapping;
import org.mapstruct.Mapper;
import org.mapstruct.MappingConstants;
import org.mapstruct.ValueMapping;
import com.baeldung.enums.mapping.order.CmsOrderStatus;
import com.baeldung.enums.mapping.order.OrderStatus;
import com.baeldung.enums.mapping.user.ExternalUserStatus;
import com.baeldung.enums.mapping.user.UserStatus;
@Mapper
public interface EnumMapper {
CmsOrderStatus map(OrderStatus orderStatus);
@ValueMapping(source = "PENDING", target = "INACTIVE")
@ValueMapping(source = "BLOCKED", target = "INACTIVE")
@ValueMapping(source = "INACTIVATED_BY_SYSTEM", target = "INACTIVE")
@ValueMapping(source = "DELETED", target = "INACTIVE")
ExternalUserStatus map(UserStatus userStatus);
@ValueMapping(source = MappingConstants.ANY_REMAINING, target = "INACTIVE")
ExternalUserStatus mapDefault(UserStatus userStatus);
}

View File

@ -0,0 +1,5 @@
package com.baeldung.enums.mapping.order;
public enum CmsOrderStatus {
PENDING, APPROVED, PACKED, DELIVERED
}

View File

@ -0,0 +1,13 @@
package com.baeldung.enums.mapping.order;
public enum OrderStatus {
PENDING, APPROVED, PACKED, DELIVERED;
public CmsOrderStatus toCmsOrderStatus() {
return CmsOrderStatus.valueOf(this.name());
}
public CmsOrderStatus toCmsOrderStatusOrdinal() {
return CmsOrderStatus.values()[this.ordinal()];
}
}

View File

@ -0,0 +1,5 @@
package com.baeldung.enums.mapping.user;
public enum ExternalUserStatus {
ACTIVE, INACTIVE
}

View File

@ -0,0 +1,26 @@
package com.baeldung.enums.mapping.user;
public enum UserStatus {
PENDING, ACTIVE, BLOCKED, INACTIVATED_BY_SYSTEM, DELETED;
public ExternalUserStatus toExternalUserStatusViaSwitchStatement() {
return switch (this) {
case PENDING, BLOCKED, INACTIVATED_BY_SYSTEM, DELETED -> ExternalUserStatus.INACTIVE;
case ACTIVE -> ExternalUserStatus.ACTIVE;
};
}
public ExternalUserStatus toExternalUserStatusViaRegularSwitch() {
switch (this) {
case PENDING:
case BLOCKED:
case INACTIVATED_BY_SYSTEM:
case DELETED:
return ExternalUserStatus.INACTIVE;
case ACTIVE:
return ExternalUserStatus.ACTIVE;
}
return null;
}
}

View File

@ -0,0 +1,20 @@
package com.baeldung.enums.mapping.user;
import java.util.EnumMap;
public class UserStatusMapper {
public static EnumMap<UserStatus, ExternalUserStatus> statusesMap;
static {
statusesMap = new EnumMap<>(UserStatus.class);
statusesMap.put(UserStatus.PENDING, ExternalUserStatus.INACTIVE);
statusesMap.put(UserStatus.BLOCKED, ExternalUserStatus.INACTIVE);
statusesMap.put(UserStatus.DELETED, ExternalUserStatus.INACTIVE);
statusesMap.put(UserStatus.INACTIVATED_BY_SYSTEM, ExternalUserStatus.INACTIVE);
statusesMap.put(UserStatus.ACTIVE, ExternalUserStatus.ACTIVE);
}
public static ExternalUserStatus toExternalUserStatus(UserStatus userStatus) {
return statusesMap.get(userStatus);
}
}

View File

@ -0,0 +1,19 @@
package com.baeldung.enums.mapping.user;
public enum UserStatusWithFieldVariable {
PENDING(ExternalUserStatus.INACTIVE),
ACTIVE(ExternalUserStatus.ACTIVE),
BLOCKED(ExternalUserStatus.INACTIVE),
INACTIVATED_BY_SYSTEM(ExternalUserStatus.INACTIVE),
DELETED(ExternalUserStatus.INACTIVE);
private final ExternalUserStatus externalUserStatus;
UserStatusWithFieldVariable(ExternalUserStatus externalUserStatus) {
this.externalUserStatus = externalUserStatus;
}
public ExternalUserStatus toExternalUserStatus() {
return externalUserStatus;
}
}

View File

@ -0,0 +1,120 @@
package com.baeldung.enums.mapping;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
import com.baeldung.enums.mapping.order.CmsOrderStatus;
import com.baeldung.enums.mapping.order.OrderStatus;
import com.baeldung.enums.mapping.user.ExternalUserStatus;
import com.baeldung.enums.mapping.user.UserStatus;
import com.baeldung.enums.mapping.user.UserStatusMapper;
import com.baeldung.enums.mapping.user.UserStatusWithFieldVariable;
public class EnumConversionUnitTest {
@Test
void whenUsingSwitchStatement_thenEnumConverted() {
UserStatus userStatusDeleted = UserStatus.DELETED;
UserStatus userStatusPending = UserStatus.PENDING;
UserStatus userStatusActive = UserStatus.ACTIVE;
assertEquals(ExternalUserStatus.INACTIVE, userStatusDeleted.toExternalUserStatusViaSwitchStatement());
assertEquals(ExternalUserStatus.INACTIVE, userStatusPending.toExternalUserStatusViaSwitchStatement());
assertEquals(ExternalUserStatus.ACTIVE, userStatusActive.toExternalUserStatusViaSwitchStatement());
}
@Test
void whenUsingSwitch_thenEnumConverted() {
UserStatus userStatusDeleted = UserStatus.DELETED;
UserStatus userStatusPending = UserStatus.PENDING;
UserStatus userStatusActive = UserStatus.ACTIVE;
assertEquals(ExternalUserStatus.INACTIVE, userStatusDeleted.toExternalUserStatusViaRegularSwitch());
assertEquals(ExternalUserStatus.INACTIVE, userStatusPending.toExternalUserStatusViaRegularSwitch());
assertEquals(ExternalUserStatus.ACTIVE, userStatusActive.toExternalUserStatusViaRegularSwitch());
}
@Test
void whenUsingFieldVariable_thenEnumConverted() {
UserStatusWithFieldVariable userStatusDeleted = UserStatusWithFieldVariable.DELETED;
UserStatusWithFieldVariable userStatusPending = UserStatusWithFieldVariable.PENDING;
UserStatusWithFieldVariable userStatusActive = UserStatusWithFieldVariable.ACTIVE;
assertEquals(ExternalUserStatus.INACTIVE, userStatusDeleted.toExternalUserStatus());
assertEquals(ExternalUserStatus.INACTIVE, userStatusPending.toExternalUserStatus());
assertEquals(ExternalUserStatus.ACTIVE, userStatusActive.toExternalUserStatus());
}
@Test
void whenUsingEnumMap_thenEnumConverted() {
UserStatus userStatusDeleted = UserStatus.DELETED;
UserStatus userStatusPending = UserStatus.PENDING;
UserStatus userStatusActive = UserStatus.ACTIVE;
assertEquals(ExternalUserStatus.INACTIVE, UserStatusMapper.toExternalUserStatus(userStatusDeleted));
assertEquals(ExternalUserStatus.INACTIVE, UserStatusMapper.toExternalUserStatus(userStatusPending));
assertEquals(ExternalUserStatus.ACTIVE, UserStatusMapper.toExternalUserStatus(userStatusActive));
}
@Test
void whenUsingOrdinalApproach_thenEnumConverted() {
OrderStatus orderStatusApproved = OrderStatus.APPROVED;
OrderStatus orderStatusDelivered = OrderStatus.DELIVERED;
OrderStatus orderStatusPending = OrderStatus.PENDING;
assertEquals(CmsOrderStatus.APPROVED, orderStatusApproved.toCmsOrderStatusOrdinal());
assertEquals(CmsOrderStatus.DELIVERED, orderStatusDelivered.toCmsOrderStatusOrdinal());
assertEquals(CmsOrderStatus.PENDING, orderStatusPending.toCmsOrderStatusOrdinal());
}
@Test
void whenUsingEnumName_thenEnumConverted() {
OrderStatus orderStatusApproved = OrderStatus.APPROVED;
OrderStatus orderStatusDelivered = OrderStatus.DELIVERED;
OrderStatus orderStatusPending = OrderStatus.PENDING;
assertEquals(CmsOrderStatus.APPROVED, orderStatusApproved.toCmsOrderStatus());
assertEquals(CmsOrderStatus.DELIVERED, orderStatusDelivered.toCmsOrderStatus());
assertEquals(CmsOrderStatus.PENDING, orderStatusPending.toCmsOrderStatus());
}
@Test
void whenUsingDefaultMapstruct_thenEnumConverted() {
UserStatus userStatusDeleted = UserStatus.DELETED;
UserStatus userStatusPending = UserStatus.PENDING;
UserStatus userStatusActive = UserStatus.ACTIVE;
EnumMapper enumMapper = new EnumMapperImpl();
assertEquals(ExternalUserStatus.INACTIVE, enumMapper.map(userStatusDeleted));
assertEquals(ExternalUserStatus.INACTIVE, enumMapper.map(userStatusPending));
assertEquals(ExternalUserStatus.ACTIVE, enumMapper.map(userStatusActive));
}
@Test
void whenUsingConfiguredMapstruct_thenEnumConverted() {
OrderStatus orderStatusApproved = OrderStatus.APPROVED;
OrderStatus orderStatusDelivered = OrderStatus.DELIVERED;
OrderStatus orderStatusPending = OrderStatus.PENDING;
EnumMapper enumMapper = new EnumMapperImpl();
assertEquals(CmsOrderStatus.APPROVED, enumMapper.map(orderStatusApproved));
assertEquals(CmsOrderStatus.DELIVERED, enumMapper.map(orderStatusDelivered));
assertEquals(CmsOrderStatus.PENDING, enumMapper.map(orderStatusPending));
}
@Test
void whenUsingConfiguredWithRemainingMapstruct_thenEnumConverted() {
UserStatus userStatusDeleted = UserStatus.DELETED;
UserStatus userStatusPending = UserStatus.PENDING;
UserStatus userStatusActive = UserStatus.ACTIVE;
EnumMapper enumMapper = new EnumMapperImpl();
assertEquals(ExternalUserStatus.INACTIVE, enumMapper.mapDefault(userStatusDeleted));
assertEquals(ExternalUserStatus.INACTIVE, enumMapper.mapDefault(userStatusPending));
assertEquals(ExternalUserStatus.ACTIVE, enumMapper.mapDefault(userStatusActive));
}
}

View File

@ -93,6 +93,7 @@
<module>core-java-lang-3</module>
<module>core-java-lang-4</module>
<module>core-java-lang-5</module>
<module>core-java-lang-6</module>
<module>core-java-lang-math</module>
<module>core-java-lang-math-2</module>
<module>core-java-lang-oop-constructors</module>