castToKeyValueMap -> KeyValueMap.from

This commit is contained in:
Michael Hoennig 2024-04-16 09:33:52 +02:00
parent 03e6c60c72
commit c8a3070fac
5 changed files with 20 additions and 17 deletions

View File

@ -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<HsBookingItemInsertResource, HsBookingItemEntity> RESOURCE_TO_ENTITY_POSTMAPPER = (resource, entity) -> {
entity.setValidity(toPostgresDateRange(resource.getValidFrom(), resource.getValidTo()));
entity.putResources(castToKeyValueMap(resource.getResources()));
entity.putResources(KeyValueMap.from(resource.getResources()));
};
}

View File

@ -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<HsBookingItemPatchResource> {
@ -21,7 +21,7 @@ public class HsBookingItemEntityPatcher implements EntityPatcher<HsBookingItemPa
OptionalFromJson.of(resource.getCaption())
.ifPresent(entity::setCaption);
Optional.ofNullable(resource.getResources())
.ifPresent(r -> 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())

View File

@ -1,17 +1,6 @@
package net.hostsharing.hsadminng.mapper;
import java.util.Map;
public interface EntityPatcher<R> {
void apply(R resource);
@SuppressWarnings("unchecked")
public static Map<String, Object> castToKeyValueMap(final Object obj) {
if (obj instanceof Map<?, ?>) {
return (Map<String, Object>) obj;
}
throw new ClassCastException("Map expected, but got: " + obj);
}
}

View File

@ -0,0 +1,14 @@
package net.hostsharing.hsadminng.mapper;
import java.util.Map;
public class KeyValueMap {
@SuppressWarnings("unchecked")
public static Map<String, Object> from(final Object obj) {
if (obj instanceof Map<?, ?>) {
return (Map<String, Object>) obj;
}
throw new ClassCastException("Map expected, but got: " + obj);
}
}

View File

@ -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;