refactor castToKeyValueMap

This commit is contained in:
Michael Hoennig 2024-04-16 09:20:22 +02:00
parent 33cffed85b
commit 03e6c60c72
4 changed files with 16 additions and 13 deletions

View File

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

View File

@ -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<HsBookingItemPatchResource> {
private final HsBookingItemEntity entity;
@ -26,13 +27,4 @@ public class HsBookingItemEntityPatcher implements EntityPatcher<HsBookingItemPa
OptionalFromJson.of(resource.getValidTo())
.ifPresent(entity::setValidTo);
}
@SuppressWarnings("unchecked")
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

@ -1,6 +1,17 @@
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

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