From 8be07c6b36aaa44327c5577bcf7f820da4a15e3f Mon Sep 17 00:00:00 2001 From: Tetiana Date: Thu, 13 Jul 2023 22:29:37 +0300 Subject: [PATCH] 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 --- core-java-modules/core-java-lang-6/README.md | 7 + core-java-modules/core-java-lang-6/pom.xml | 52 ++++++++ .../baeldung/enums/mapping/EnumMapper.java | 26 ++++ .../enums/mapping/order/CmsOrderStatus.java | 5 + .../enums/mapping/order/OrderStatus.java | 13 ++ .../mapping/user/ExternalUserStatus.java | 5 + .../enums/mapping/user/UserStatus.java | 26 ++++ .../enums/mapping/user/UserStatusMapper.java | 20 +++ .../user/UserStatusWithFieldVariable.java | 19 +++ .../enums/mapping/EnumConversionUnitTest.java | 120 ++++++++++++++++++ core-java-modules/pom.xml | 1 + 11 files changed, 294 insertions(+) create mode 100644 core-java-modules/core-java-lang-6/README.md create mode 100644 core-java-modules/core-java-lang-6/pom.xml create mode 100644 core-java-modules/core-java-lang-6/src/main/java/com/baeldung/enums/mapping/EnumMapper.java create mode 100644 core-java-modules/core-java-lang-6/src/main/java/com/baeldung/enums/mapping/order/CmsOrderStatus.java create mode 100644 core-java-modules/core-java-lang-6/src/main/java/com/baeldung/enums/mapping/order/OrderStatus.java create mode 100644 core-java-modules/core-java-lang-6/src/main/java/com/baeldung/enums/mapping/user/ExternalUserStatus.java create mode 100644 core-java-modules/core-java-lang-6/src/main/java/com/baeldung/enums/mapping/user/UserStatus.java create mode 100644 core-java-modules/core-java-lang-6/src/main/java/com/baeldung/enums/mapping/user/UserStatusMapper.java create mode 100644 core-java-modules/core-java-lang-6/src/main/java/com/baeldung/enums/mapping/user/UserStatusWithFieldVariable.java create mode 100644 core-java-modules/core-java-lang-6/src/test/java/com/baeldung/enums/mapping/EnumConversionUnitTest.java diff --git a/core-java-modules/core-java-lang-6/README.md b/core-java-modules/core-java-lang-6/README.md new file mode 100644 index 0000000000..577e9c27dc --- /dev/null +++ b/core-java-modules/core-java-lang-6/README.md @@ -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]() \ No newline at end of file diff --git a/core-java-modules/core-java-lang-6/pom.xml b/core-java-modules/core-java-lang-6/pom.xml new file mode 100644 index 0000000000..86121e0a7f --- /dev/null +++ b/core-java-modules/core-java-lang-6/pom.xml @@ -0,0 +1,52 @@ + + + 4.0.0 + + com.baeldung.core-java-modules + core-java-modules + 0.0.1-SNAPSHOT + + + core-java-lang-6 + + + + + org.mapstruct + mapstruct + ${mapstruct.version} + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.5.1 + + 17 + 17 + + + + org.mapstruct + mapstruct-processor + ${mapstruct.version} + + + + + + + + + 17 + 17 + UTF-8 + 1.5.5.Final + + + \ No newline at end of file diff --git a/core-java-modules/core-java-lang-6/src/main/java/com/baeldung/enums/mapping/EnumMapper.java b/core-java-modules/core-java-lang-6/src/main/java/com/baeldung/enums/mapping/EnumMapper.java new file mode 100644 index 0000000000..5b6a82d860 --- /dev/null +++ b/core-java-modules/core-java-lang-6/src/main/java/com/baeldung/enums/mapping/EnumMapper.java @@ -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); + +} diff --git a/core-java-modules/core-java-lang-6/src/main/java/com/baeldung/enums/mapping/order/CmsOrderStatus.java b/core-java-modules/core-java-lang-6/src/main/java/com/baeldung/enums/mapping/order/CmsOrderStatus.java new file mode 100644 index 0000000000..8c35f62180 --- /dev/null +++ b/core-java-modules/core-java-lang-6/src/main/java/com/baeldung/enums/mapping/order/CmsOrderStatus.java @@ -0,0 +1,5 @@ +package com.baeldung.enums.mapping.order; + +public enum CmsOrderStatus { + PENDING, APPROVED, PACKED, DELIVERED +} diff --git a/core-java-modules/core-java-lang-6/src/main/java/com/baeldung/enums/mapping/order/OrderStatus.java b/core-java-modules/core-java-lang-6/src/main/java/com/baeldung/enums/mapping/order/OrderStatus.java new file mode 100644 index 0000000000..b46018cc4f --- /dev/null +++ b/core-java-modules/core-java-lang-6/src/main/java/com/baeldung/enums/mapping/order/OrderStatus.java @@ -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()]; + } +} diff --git a/core-java-modules/core-java-lang-6/src/main/java/com/baeldung/enums/mapping/user/ExternalUserStatus.java b/core-java-modules/core-java-lang-6/src/main/java/com/baeldung/enums/mapping/user/ExternalUserStatus.java new file mode 100644 index 0000000000..7361e8fe75 --- /dev/null +++ b/core-java-modules/core-java-lang-6/src/main/java/com/baeldung/enums/mapping/user/ExternalUserStatus.java @@ -0,0 +1,5 @@ +package com.baeldung.enums.mapping.user; + +public enum ExternalUserStatus { + ACTIVE, INACTIVE +} diff --git a/core-java-modules/core-java-lang-6/src/main/java/com/baeldung/enums/mapping/user/UserStatus.java b/core-java-modules/core-java-lang-6/src/main/java/com/baeldung/enums/mapping/user/UserStatus.java new file mode 100644 index 0000000000..6e6d5d2a9a --- /dev/null +++ b/core-java-modules/core-java-lang-6/src/main/java/com/baeldung/enums/mapping/user/UserStatus.java @@ -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; + } + +} diff --git a/core-java-modules/core-java-lang-6/src/main/java/com/baeldung/enums/mapping/user/UserStatusMapper.java b/core-java-modules/core-java-lang-6/src/main/java/com/baeldung/enums/mapping/user/UserStatusMapper.java new file mode 100644 index 0000000000..a9ec017d6d --- /dev/null +++ b/core-java-modules/core-java-lang-6/src/main/java/com/baeldung/enums/mapping/user/UserStatusMapper.java @@ -0,0 +1,20 @@ +package com.baeldung.enums.mapping.user; + +import java.util.EnumMap; + +public class UserStatusMapper { + public static EnumMap 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); + } +} diff --git a/core-java-modules/core-java-lang-6/src/main/java/com/baeldung/enums/mapping/user/UserStatusWithFieldVariable.java b/core-java-modules/core-java-lang-6/src/main/java/com/baeldung/enums/mapping/user/UserStatusWithFieldVariable.java new file mode 100644 index 0000000000..49ec9200ae --- /dev/null +++ b/core-java-modules/core-java-lang-6/src/main/java/com/baeldung/enums/mapping/user/UserStatusWithFieldVariable.java @@ -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; + } +} diff --git a/core-java-modules/core-java-lang-6/src/test/java/com/baeldung/enums/mapping/EnumConversionUnitTest.java b/core-java-modules/core-java-lang-6/src/test/java/com/baeldung/enums/mapping/EnumConversionUnitTest.java new file mode 100644 index 0000000000..9272f9b63a --- /dev/null +++ b/core-java-modules/core-java-lang-6/src/test/java/com/baeldung/enums/mapping/EnumConversionUnitTest.java @@ -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)); + } +} diff --git a/core-java-modules/pom.xml b/core-java-modules/pom.xml index 9144e16359..f85189570f 100644 --- a/core-java-modules/pom.xml +++ b/core-java-modules/pom.xml @@ -93,6 +93,7 @@ core-java-lang-3 core-java-lang-4 core-java-lang-5 + core-java-lang-6 core-java-lang-math core-java-lang-math-2 core-java-lang-oop-constructors