12
12
import java .util .HashMap ;
13
13
import java .util .List ;
14
14
import java .util .Map ;
15
+ import java .util .Objects ;
15
16
import java .util .Optional ;
16
17
import java .util .Stack ;
17
18
@@ -117,7 +118,7 @@ public static void removeTargetFieldFromSource(final Map<String, Object> sourceA
117
118
if (key .equals (lastKey )) {
118
119
break ;
119
120
}
120
- currentMap = ( Map < String , Object >) currentMap .get (key );
121
+ currentMap = castToMap ( currentMap .get (key ) );
121
122
}
122
123
123
124
// Remove the last key this is guaranteed
@@ -132,8 +133,7 @@ public static void removeTargetFieldFromSource(final Map<String, Object> sourceA
132
133
parentMap = currentParentMapWithChild .v1 ();
133
134
key = currentParentMapWithChild .v2 ();
134
135
135
- @ SuppressWarnings ("unchecked" )
136
- Map <String , Object > innerMap = (Map <String , Object >) parentMap .get (key );
136
+ Map <String , Object > innerMap = castToMap (parentMap .get (key ));
137
137
138
138
if (innerMap != null && innerMap .isEmpty ()) {
139
139
parentMap .remove (key );
@@ -167,13 +167,13 @@ public static Optional<Object> getValueFromSource(final Map<String, Object> sour
167
167
for (String key : keys ) {
168
168
currentValue = currentValue .flatMap (value -> {
169
169
if (value instanceof ArrayList <?> && index != -1 ) {
170
- Object listValue = (( ArrayList ) value ).get (index );
170
+ Object listValue = (castToObjectList ( value ) ).get (index );
171
171
if (listValue instanceof Map ) {
172
- Map <String , Object > currentMap = ( Map < String , Object >) listValue ;
172
+ Map <String , Object > currentMap = castToMap ( listValue ) ;
173
173
return Optional .ofNullable (currentMap .get (key ));
174
174
}
175
175
} else if (value instanceof Map <?, ?>) {
176
- Map <String , Object > currentMap = ( Map < String , Object >) value ;
176
+ Map <String , Object > currentMap = castToMap ( value ) ;
177
177
return Optional .ofNullable (currentMap .get (key ));
178
178
}
179
179
return Optional .empty ();
@@ -207,7 +207,7 @@ public static void setValueToSource(Map<String, Object> sourceAsMap, String targ
207
207
*/
208
208
209
209
public static void setValueToSource (Map <String , Object > sourceAsMap , String targetKey , Object targetValue , int index ) {
210
- if (sourceAsMap == null || targetKey == null ) return ;
210
+ if (Objects . isNull ( sourceAsMap ) || Objects . isNull ( targetKey ) ) return ;
211
211
212
212
String [] keys = targetKey .split ("\\ ." );
213
213
Map <String , Object > current = sourceAsMap ;
@@ -217,10 +217,10 @@ public static void setValueToSource(Map<String, Object> sourceAsMap, String targ
217
217
if (next instanceof ArrayList <?> list ) {
218
218
if (index < 0 || index >= list .size ()) return ;
219
219
if (list .get (index ) instanceof Map ) {
220
- current = ( Map < String , Object >) list .get (index );
220
+ current = castToMap ( list .get (index ) );
221
221
}
222
222
} else if (next instanceof Map ) {
223
- current = ( Map < String , Object >) next ;
223
+ current = castToMap ( next ) ;
224
224
} else {
225
225
throw new IllegalStateException ("Unexpected data structure at " + keys [i ]);
226
226
}
@@ -274,4 +274,11 @@ public static boolean isNumeric(Object value) {
274
274
public static Map <String , Object > castToMap (Object obj ) {
275
275
return (Map <String , Object >) obj ;
276
276
}
277
+
278
+ // This method should be used only when you are certain the object is a `List<Object>`.
279
+ // It is recommended to use this method as a last resort.
280
+ @ SuppressWarnings ("unchecked" )
281
+ public static List <Object > castToObjectList (Object obj ) {
282
+ return (List <Object >) obj ;
283
+ }
277
284
}
0 commit comments