diff --git a/devkit-utils/src/main/java/com/onixbyte/devkit/utils/MapUtil.java b/devkit-utils/src/main/java/com/onixbyte/devkit/utils/MapUtil.java index 195020c..a81e8cf 100644 --- a/devkit-utils/src/main/java/com/onixbyte/devkit/utils/MapUtil.java +++ b/devkit-utils/src/main/java/com/onixbyte/devkit/utils/MapUtil.java @@ -31,7 +31,7 @@ import java.util.Optional; * Please see documentation for more information. * * @author zihluwang - * @version 1.4.2 + * @version 1.7.0 * @since 1.0.0 */ @Slf4j @@ -40,16 +40,13 @@ public final class MapUtil { /** * Converts an object to a map by mapping the field names to their corresponding values. * - * @param the type of the object - * @param entity the object to be converted to a map - * @param adapters adapts the entity for mapping to a map + * @param the type of the object + * @param entity the object to be converted to a map + * @param adapter adapts the entity for mapping to a map * @return a map representing the fields and their values of the object */ - public static Map objectToMap(T entity, - Map> adapters) { - var resultMap = new HashMap(); - adapters.forEach((fieldName, adapter) -> resultMap.put(fieldName, adapter.fetch(entity))); - return resultMap; + public static Map objectToMap(T entity, ObjectMapAdapter adapter) { + return adapter.toMap(entity); } /** @@ -57,64 +54,17 @@ public final class MapUtil { * map entries. * * @param objectMap the map representing the fields and their values - * @param entity an empty entity of the target class - * @param adapters the adapters to execute the setter for the entity + * @param adapter the adapter to execute the setter for the entity * @param the type of the object to be created * @return an object of the specified type with the field values set from the map */ - public static T mapToObject(Map objectMap, - T entity, - Map> adapters) { - adapters.forEach((fieldName, adapter) -> Optional.ofNullable(objectMap) - .map((data) -> data.get(fieldName)) - .ifPresent((fieldValue) -> adapter.setValue(entity, fieldValue))); - return entity; + public static T mapToObject(Map objectMap, ObjectMapAdapter adapter) { + return adapter.toObject(objectMap); } /** - * Retrieves the value of a field from an object using reflection. - * - * @param the type of the entity - * @param the type of the field value - * @param entity the object from which to retrieve the field value - * @param adapter the adapter to execute the getter - * @return the value of the field in the object, or null if the field does not exist or cannot - * be accessed + * Private constructor prevent class being instantiated. */ - public static T getFieldValue(E entity, ObjectMapAdapter adapter) { - return adapter.fetch(entity); - } - - /** - * Sets the value of a field in an object using reflection. - * - * @param the type of the entity - * @param the type of the field value - * @param entity the object in which to set the field value - * @param adapter the adapter to execute the setter - * @param fieldValue the value to be set - */ - public static void setFieldValue(E entity, - ObjectMapAdapter adapter, - Object fieldValue) { - adapter.setValue(entity, fieldValue); - } - - /** - * Casts the specified value to the required type with Optional. - * - * @param value the value to be cast - * @param requiredType the type to which the value should be cast - * @param the type to which the value should be cast - * @return the cast value, or {@code null} if the value is not an instance of the requiredType - */ - public static T cast(Object value, Class requiredType) { - return Optional.ofNullable(requiredType) - .filter((clazz) -> clazz.isInstance(value)) - .map((clazz) -> clazz.cast(value)) - .orElse(null); - } - private MapUtil() { } } diff --git a/devkit-utils/src/main/java/com/onixbyte/devkit/utils/ObjectMapAdapter.java b/devkit-utils/src/main/java/com/onixbyte/devkit/utils/ObjectMapAdapter.java index 863aa60..49d51b1 100644 --- a/devkit-utils/src/main/java/com/onixbyte/devkit/utils/ObjectMapAdapter.java +++ b/devkit-utils/src/main/java/com/onixbyte/devkit/utils/ObjectMapAdapter.java @@ -17,53 +17,32 @@ package com.onixbyte.devkit.utils; -import java.util.function.BiConsumer; -import java.util.function.Function; +import java.util.Map; /** * Adapts an Object to a Map, making conversion between Map and Object much more safe. * - * @param entity type * @param field type * @author zihluwang - * @version 1.4.2 + * @version 1.7.0 * @since 1.4.2 */ -public class ObjectMapAdapter { - - private final Function getter; - - private final BiConsumer setter; +public interface ObjectMapAdapter { /** - * Create an adapter. + * Convert an object to a map. * - * @param getter the getter of the field - * @param setter the setter of the field + * @param element the element that will be converted to Map + * @return a Map that is converted from the element */ - public ObjectMapAdapter(Function getter, BiConsumer setter) { - this.getter = getter; - this.setter = setter; - } + Map toMap(T element); /** - * Get data from the entity. + * Convert a Map to an object. * - * @param entity the source of the data - * @return the data + * @param map the map that will be converted to Object + * @return the object that is converted from the Map */ - public T fetch(E entity) { - return getter.apply(entity); - } - - /** - * Set value to the entity. - * - * @param entity the target of the data - * @param value the value - */ - public void setValue(E entity, Object value) { - setter.accept(entity, value); - } + T toObject(Map map); }