@@ -97,6 +97,16 @@ private static void extractRawValues(List values, List<Object> part, String[] pa
97
97
}
98
98
}
99
99
100
+ /**
101
+ * For the provided path, return its value in the xContent map.
102
+ *
103
+ * Note that in contrast with {@link XContentMapValues#extractRawValues}, array and object values
104
+ * can be returned.
105
+ *
106
+ * @param path the value's path in the map.
107
+ *
108
+ * @return the value associated with the path in the map or 'null' if the path does not exist.
109
+ */
100
110
public static Object extractValue (String path , Map <?, ?> map ) {
101
111
return extractValue (map , path .split ("\\ ." ));
102
112
}
@@ -105,19 +115,51 @@ public static Object extractValue(Map<?, ?> map, String... pathElements) {
105
115
if (pathElements .length == 0 ) {
106
116
return null ;
107
117
}
108
- return extractValue (pathElements , 0 , map );
118
+ return XContentMapValues . extractValue (pathElements , 0 , map , null );
109
119
}
110
120
111
- @ SuppressWarnings ({"unchecked" })
112
- private static Object extractValue (String [] pathElements , int index , Object currentValue ) {
113
- if (index == pathElements .length ) {
114
- return currentValue ;
115
- }
116
- if (currentValue == null ) {
121
+ /**
122
+ * For the provided path, return its value in the xContent map.
123
+ *
124
+ * Note that in contrast with {@link XContentMapValues#extractRawValues}, array and object values
125
+ * can be returned.
126
+ *
127
+ * @param path the value's path in the map.
128
+ * @param nullValue a value to return if the path exists, but the value is 'null'. This helps
129
+ * in distinguishing between a path that doesn't exist vs. a value of 'null'.
130
+ *
131
+ * @return the value associated with the path in the map or 'null' if the path does not exist.
132
+ */
133
+ public static Object extractValue (String path , Map <?, ?> map , Object nullValue ) {
134
+ String [] pathElements = path .split ("\\ ." );
135
+ if (pathElements .length == 0 ) {
117
136
return null ;
118
137
}
138
+ return extractValue (pathElements , 0 , map , nullValue );
139
+ }
140
+
141
+ private static Object extractValue (String [] pathElements ,
142
+ int index ,
143
+ Object currentValue ,
144
+ Object nullValue ) {
145
+ if (currentValue instanceof List ) {
146
+ List <?> valueList = (List <?>) currentValue ;
147
+ List <Object > newList = new ArrayList <>(valueList .size ());
148
+ for (Object o : valueList ) {
149
+ Object listValue = extractValue (pathElements , index , o , nullValue );
150
+ if (listValue != null ) {
151
+ newList .add (listValue );
152
+ }
153
+ }
154
+ return newList ;
155
+ }
156
+
157
+ if (index == pathElements .length ) {
158
+ return currentValue != null ? currentValue : nullValue ;
159
+ }
160
+
119
161
if (currentValue instanceof Map ) {
120
- Map map = (Map ) currentValue ;
162
+ Map <?, ?> map = (Map <?, ?> ) currentValue ;
121
163
String key = pathElements [index ];
122
164
Object mapValue = map .get (key );
123
165
int nextIndex = index + 1 ;
@@ -126,18 +168,12 @@ private static Object extractValue(String[] pathElements, int index, Object curr
126
168
mapValue = map .get (key );
127
169
nextIndex ++;
128
170
}
129
- return extractValue (pathElements , nextIndex , mapValue );
130
- }
131
- if (currentValue instanceof List ) {
132
- List valueList = (List ) currentValue ;
133
- List newList = new ArrayList (valueList .size ());
134
- for (Object o : valueList ) {
135
- Object listValue = extractValue (pathElements , index , o );
136
- if (listValue != null ) {
137
- newList .add (listValue );
138
- }
171
+
172
+ if (map .containsKey (key ) == false ) {
173
+ return null ;
139
174
}
140
- return newList ;
175
+
176
+ return extractValue (pathElements , nextIndex , mapValue , nullValue );
141
177
}
142
178
return null ;
143
179
}
0 commit comments