From c8a3070facb6e459d05045f9a8287448951e3f94 Mon Sep 17 00:00:00 2001 From: Michael Hoennig Date: Tue, 16 Apr 2024 09:33:52 +0200 Subject: [PATCH] castToKeyValueMap -> KeyValueMap.from --- .../hs/booking/item/HsBookingItemController.java | 4 ++-- .../booking/item/HsBookingItemEntityPatcher.java | 4 ++-- .../hsadminng/mapper/EntityPatcher.java | 11 ----------- .../hostsharing/hsadminng/mapper/KeyValueMap.java | 14 ++++++++++++++ .../item/HsBookingItemEntityPatcherUnitTest.java | 4 ++-- 5 files changed, 20 insertions(+), 17 deletions(-) create mode 100644 src/main/java/net/hostsharing/hsadminng/mapper/KeyValueMap.java 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 16e79118..bd05ad66 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 @@ -5,6 +5,7 @@ import net.hostsharing.hsadminng.hs.booking.generated.api.v1.api.HsBookingItemsA import net.hostsharing.hsadminng.hs.booking.generated.api.v1.model.HsBookingItemInsertResource; import net.hostsharing.hsadminng.hs.booking.generated.api.v1.model.HsBookingItemPatchResource; import net.hostsharing.hsadminng.hs.booking.generated.api.v1.model.HsBookingItemResource; +import net.hostsharing.hsadminng.mapper.KeyValueMap; import net.hostsharing.hsadminng.mapper.Mapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; @@ -16,7 +17,6 @@ import java.util.List; 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(castToKeyValueMap(resource.getResources())); + entity.putResources(KeyValueMap.from(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 e6a6a101..2131bef5 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 @@ -2,11 +2,11 @@ package net.hostsharing.hsadminng.hs.booking.item; import net.hostsharing.hsadminng.hs.booking.generated.api.v1.model.HsBookingItemPatchResource; import net.hostsharing.hsadminng.mapper.EntityPatcher; +import net.hostsharing.hsadminng.mapper.KeyValueMap; import net.hostsharing.hsadminng.mapper.OptionalFromJson; import java.util.Optional; -import static net.hostsharing.hsadminng.mapper.EntityPatcher.castToKeyValueMap; public class HsBookingItemEntityPatcher implements EntityPatcher { @@ -21,7 +21,7 @@ public class HsBookingItemEntityPatcher implements EntityPatcher entity.getResources().patch(castToKeyValueMap(resource.getResources()))); + .ifPresent(r -> entity.getResources().patch(KeyValueMap.from(resource.getResources()))); OptionalFromJson.of(resource.getValidFrom()) .ifPresent(entity::setValidFrom); OptionalFromJson.of(resource.getValidTo()) diff --git a/src/main/java/net/hostsharing/hsadminng/mapper/EntityPatcher.java b/src/main/java/net/hostsharing/hsadminng/mapper/EntityPatcher.java index 82e18976..23eb9753 100644 --- a/src/main/java/net/hostsharing/hsadminng/mapper/EntityPatcher.java +++ b/src/main/java/net/hostsharing/hsadminng/mapper/EntityPatcher.java @@ -1,17 +1,6 @@ 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/main/java/net/hostsharing/hsadminng/mapper/KeyValueMap.java b/src/main/java/net/hostsharing/hsadminng/mapper/KeyValueMap.java new file mode 100644 index 00000000..5a8cff2f --- /dev/null +++ b/src/main/java/net/hostsharing/hsadminng/mapper/KeyValueMap.java @@ -0,0 +1,14 @@ +package net.hostsharing.hsadminng.mapper; + +import java.util.Map; + +public class KeyValueMap { + + @SuppressWarnings("unchecked") + public static Map from(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 34d5a213..7e7f4308 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 @@ -3,6 +3,7 @@ package net.hostsharing.hsadminng.hs.booking.item; import io.hypersistence.utils.hibernate.type.range.Range; import net.hostsharing.hsadminng.hs.booking.generated.api.v1.model.HsBookingItemPatchResource; import net.hostsharing.hsadminng.hs.office.debitor.HsOfficeDebitorEntity; +import net.hostsharing.hsadminng.mapper.KeyValueMap; import net.hostsharing.hsadminng.rbac.test.PatchUnitTestBase; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.TestInstance; @@ -17,7 +18,6 @@ import java.util.UUID; import java.util.stream.Stream; 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; @@ -72,7 +72,7 @@ class HsBookingItemEntityPatcherUnitTest extends PatchUnitTestBase< final var entity = new HsBookingItemEntity(); entity.setUuid(INITIAL_BOOKING_ITEM_UUID); entity.setDebitor(TEST_DEBITOR); - entity.getResources().putAll(castToKeyValueMap(INITIAL_RESOURCES)); + entity.getResources().putAll(KeyValueMap.from(INITIAL_RESOURCES)); entity.setCaption(INITIAL_CAPTION); entity.setValidity(Range.closedInfinite(GIVEN_VALID_FROM)); return entity;