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}
0 commit comments