From 03e6c60c72d34b5fdde5df07fd94d39b9f6d2b3a Mon Sep 17 00:00:00 2001 From: Michael Hoennig Date: Tue, 16 Apr 2024 09:20:22 +0200 Subject: [PATCH] refactor castToKeyValueMap --- .../hs/booking/item/HsBookingItemController.java | 4 ++-- .../hs/booking/item/HsBookingItemEntityPatcher.java | 12 ++---------- .../hostsharing/hsadminng/mapper/EntityPatcher.java | 11 +++++++++++ .../item/HsBookingItemEntityPatcherUnitTest.java | 2 +- 4 files changed, 16 insertions(+), 13 deletions(-) diff --git a/src/main/java/net/hostsharing/hsadminng/hs/booking/item/HsBookingItemController.java b/src/main/java/net/hostsharing/hsadminng/hs/booking/item/HsBookingItemController.java index 57ffefcd..16e79118 100644 --- a/src/main/java/net/hostsharing/hsadminng/hs/booking/item/HsBookingItemController.java +++ b/src/main/java/net/hostsharing/hsadminng/hs/booking/item/HsBookingItemController.java @@ -13,10 +13,10 @@ import org.springframework.web.bind.annotation.RestController; import org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder; import java.util.List; -import java.util.Map; import java.util.UUID; import java.util.function.BiConsumer; +import static net.hostsharing.hsadminng.mapper.EntityPatcher.castToKeyValueMap; import static net.hostsharing.hsadminng.mapper.PostgresDateRange.toPostgresDateRange; @RestController @@ -126,6 +126,6 @@ public class HsBookingItemController implements HsBookingItemsApi { @SuppressWarnings("unchecked") final BiConsumer RESOURCE_TO_ENTITY_POSTMAPPER = (resource, entity) -> { entity.setValidity(toPostgresDateRange(resource.getValidFrom(), resource.getValidTo())); - entity.putResources((Map) resource.getResources()); + entity.putResources(castToKeyValueMap(resource.getResources())); }; } diff --git a/src/main/java/net/hostsharing/hsadminng/hs/booking/item/HsBookingItemEntityPatcher.java b/src/main/java/net/hostsharing/hsadminng/hs/booking/item/HsBookingItemEntityPatcher.java index 8aa0e198..e6a6a101 100644 --- a/src/main/java/net/hostsharing/hsadminng/hs/booking/item/HsBookingItemEntityPatcher.java +++ b/src/main/java/net/hostsharing/hsadminng/hs/booking/item/HsBookingItemEntityPatcher.java @@ -4,9 +4,10 @@ import net.hostsharing.hsadminng.hs.booking.generated.api.v1.model.HsBookingItem import net.hostsharing.hsadminng.mapper.EntityPatcher; import net.hostsharing.hsadminng.mapper.OptionalFromJson; -import java.util.Map; import java.util.Optional; +import static net.hostsharing.hsadminng.mapper.EntityPatcher.castToKeyValueMap; + public class HsBookingItemEntityPatcher implements EntityPatcher { private final HsBookingItemEntity entity; @@ -26,13 +27,4 @@ public class HsBookingItemEntityPatcher implements EntityPatcher castToKeyValueMap(final Object obj) { - if (obj instanceof Map) { - return (Map) obj; - } - throw new ClassCastException("Map expected, but got: " + obj); - } - } diff --git a/src/main/java/net/hostsharing/hsadminng/mapper/EntityPatcher.java b/src/main/java/net/hostsharing/hsadminng/mapper/EntityPatcher.java index 23eb9753..82e18976 100644 --- a/src/main/java/net/hostsharing/hsadminng/mapper/EntityPatcher.java +++ b/src/main/java/net/hostsharing/hsadminng/mapper/EntityPatcher.java @@ -1,6 +1,17 @@ package net.hostsharing.hsadminng.mapper; +import java.util.Map; + public interface EntityPatcher { void apply(R resource); + + @SuppressWarnings("unchecked") + public static Map castToKeyValueMap(final Object obj) { + if (obj instanceof Map) { + return (Map) obj; + } + throw new ClassCastException("Map expected, but got: " + obj); + } + } diff --git a/src/test/java/net/hostsharing/hsadminng/hs/booking/item/HsBookingItemEntityPatcherUnitTest.java b/src/test/java/net/hostsharing/hsadminng/hs/booking/item/HsBookingItemEntityPatcherUnitTest.java index 9234a3ec..34d5a213 100644 --- a/src/test/java/net/hostsharing/hsadminng/hs/booking/item/HsBookingItemEntityPatcherUnitTest.java +++ b/src/test/java/net/hostsharing/hsadminng/hs/booking/item/HsBookingItemEntityPatcherUnitTest.java @@ -16,8 +16,8 @@ import java.util.Map; import java.util.UUID; import java.util.stream.Stream; -import static net.hostsharing.hsadminng.hs.booking.item.HsBookingItemEntityPatcher.castToKeyValueMap; import static net.hostsharing.hsadminng.hs.office.debitor.TestHsOfficeDebitor.TEST_DEBITOR; +import static net.hostsharing.hsadminng.mapper.EntityPatcher.castToKeyValueMap; import static net.hostsharing.hsadminng.mapper.PatchMap.entry; import static net.hostsharing.hsadminng.mapper.PatchMap.patchMap; import static org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS;