Skip to content

Commit 2d3a074

Browse files
author
zihluwang
committed
feat: implement map-object conversion with a safer method
If you still want to use the unsafe version, please use com.onixbyte:map-util-unsafe.
1 parent 8f6dfce commit 2d3a074

File tree

2 files changed

+21
-92
lines changed

2 files changed

+21
-92
lines changed

devkit-utils/src/main/java/com/onixbyte/devkit/utils/MapUtil.java

Lines changed: 10 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
* Please see documentation for more information.
3232
*
3333
* @author zihluwang
34-
* @version 1.4.2
34+
* @version 1.7.0
3535
* @since 1.0.0
3636
*/
3737
@Slf4j
@@ -40,81 +40,31 @@ public final class MapUtil {
4040
/**
4141
* Converts an object to a map by mapping the field names to their corresponding values.
4242
*
43-
* @param <T> the type of the object
44-
* @param entity the object to be converted to a map
45-
* @param adapters adapts the entity for mapping to a map
43+
* @param <T> the type of the object
44+
* @param entity the object to be converted to a map
45+
* @param adapter adapts the entity for mapping to a map
4646
* @return a map representing the fields and their values of the object
4747
*/
48-
public static <T> Map<String, Object> objectToMap(T entity,
49-
Map<String, ObjectMapAdapter<T, ?>> adapters) {
50-
var resultMap = new HashMap<String, Object>();
51-
adapters.forEach((fieldName, adapter) -> resultMap.put(fieldName, adapter.fetch(entity)));
52-
return resultMap;
48+
public static <T> Map<String, Object> objectToMap(T entity, ObjectMapAdapter<T> adapter) {
49+
return adapter.toMap(entity);
5350
}
5451

5552
/**
5653
* Converts a map to an object of the specified type by setting the field values using the
5754
* map entries.
5855
*
5956
* @param objectMap the map representing the fields and their values
60-
* @param entity an empty entity of the target class
61-
* @param adapters the adapters to execute the setter for the entity
57+
* @param adapter the adapter to execute the setter for the entity
6258
* @param <T> the type of the object to be created
6359
* @return an object of the specified type with the field values set from the map
6460
*/
65-
public static <T> T mapToObject(Map<String, Object> objectMap,
66-
T entity,
67-
Map<String, ObjectMapAdapter<T, ?>> adapters) {
68-
adapters.forEach((fieldName, adapter) -> Optional.ofNullable(objectMap)
69-
.map((data) -> data.get(fieldName))
70-
.ifPresent((fieldValue) -> adapter.setValue(entity, fieldValue)));
71-
return entity;
61+
public static <T> T mapToObject(Map<String, Object> objectMap, ObjectMapAdapter<T> adapter) {
62+
return adapter.toObject(objectMap);
7263
}
7364

7465
/**
75-
* Retrieves the value of a field from an object using reflection.
76-
*
77-
* @param <E> the type of the entity
78-
* @param <T> the type of the field value
79-
* @param entity the object from which to retrieve the field value
80-
* @param adapter the adapter to execute the getter
81-
* @return the value of the field in the object, or null if the field does not exist or cannot
82-
* be accessed
66+
* Private constructor prevent class being instantiated.
8367
*/
84-
public static <E, T> T getFieldValue(E entity, ObjectMapAdapter<E, T> adapter) {
85-
return adapter.fetch(entity);
86-
}
87-
88-
/**
89-
* Sets the value of a field in an object using reflection.
90-
*
91-
* @param <E> the type of the entity
92-
* @param <T> the type of the field value
93-
* @param entity the object in which to set the field value
94-
* @param adapter the adapter to execute the setter
95-
* @param fieldValue the value to be set
96-
*/
97-
public static <E, T> void setFieldValue(E entity,
98-
ObjectMapAdapter<E, T> adapter,
99-
Object fieldValue) {
100-
adapter.setValue(entity, fieldValue);
101-
}
102-
103-
/**
104-
* Casts the specified value to the required type with Optional.
105-
*
106-
* @param value the value to be cast
107-
* @param requiredType the type to which the value should be cast
108-
* @param <T> the type to which the value should be cast
109-
* @return the cast value, or {@code null} if the value is not an instance of the requiredType
110-
*/
111-
public static <T> T cast(Object value, Class<T> requiredType) {
112-
return Optional.ofNullable(requiredType)
113-
.filter((clazz) -> clazz.isInstance(value))
114-
.map((clazz) -> clazz.cast(value))
115-
.orElse(null);
116-
}
117-
11868
private MapUtil() {
11969
}
12070
}

devkit-utils/src/main/java/com/onixbyte/devkit/utils/ObjectMapAdapter.java

Lines changed: 11 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -17,53 +17,32 @@
1717

1818
package com.onixbyte.devkit.utils;
1919

20-
import java.util.function.BiConsumer;
21-
import java.util.function.Function;
20+
import java.util.Map;
2221

2322
/**
2423
* Adapts an Object to a Map, making conversion between Map and Object much more safe.
2524
*
26-
* @param <E> entity type
2725
* @param <T> field type
2826
* @author zihluwang
29-
* @version 1.4.2
27+
* @version 1.7.0
3028
* @since 1.4.2
3129
*/
32-
public class ObjectMapAdapter<E, T> {
33-
34-
private final Function<E, T> getter;
35-
36-
private final BiConsumer<E, Object> setter;
37-
38-
/**
39-
* Create an adapter.
40-
*
41-
* @param getter the getter of the field
42-
* @param setter the setter of the field
43-
*/
44-
public ObjectMapAdapter(Function<E, T> getter, BiConsumer<E, Object> setter) {
45-
this.getter = getter;
46-
this.setter = setter;
47-
}
30+
public interface ObjectMapAdapter<T> {
4831

4932
/**
50-
* Get data from the entity.
33+
* Convert an object to a map.
5134
*
52-
* @param entity the source of the data
53-
* @return the data
35+
* @param element the element that will be converted to Map
36+
* @return a Map that is converted from the element
5437
*/
55-
public T fetch(E entity) {
56-
return getter.apply(entity);
57-
}
38+
Map<String, Object> toMap(T element);
5839

5940
/**
60-
* Set value to the entity.
41+
* Convert a Map to an object.
6142
*
62-
* @param entity the target of the data
63-
* @param value the value
43+
* @param map the map that will be converted to Object
44+
* @return the object that is converted from the Map
6445
*/
65-
public void setValue(E entity, Object value) {
66-
setter.accept(entity, value);
67-
}
46+
T toObject(Map<String, Object> map);
6847

6948
}

0 commit comments

Comments
 (0)