@@ -123,28 +123,29 @@ public static Collector<Map.Entry<String, JsonValue>, JsonObjectBuilder, JsonObj
123
123
* downstream {@code Collector}. For each group, the key and the results of the reduction operation
124
124
* become the name/value pairs of the resultant {@code JsonObject}.
125
125
*
126
+ * @param <T> the intermediate accumulation {@code JsonArrayBuilder} of the downstream collector
126
127
* @param classifier a function mapping the input {@code JsonValue}s to a String, producing keys
127
128
* @param downstream a {@code Collector} that implements a reduction operation on the
128
129
* {@code JsonValue}s in each group.
129
130
* @return the constructed {@code Collector}
130
131
*/
131
- public static Collector <JsonValue , Map <String , JsonArrayBuilder >, JsonObject >
132
+ public static < T extends JsonArrayBuilder > Collector <JsonValue , Map <String , T >, JsonObject >
132
133
groupingBy (Function <JsonValue , String > classifier ,
133
- Collector <JsonValue , JsonArrayBuilder , JsonArray > downstream ) {
134
+ Collector <JsonValue , T , JsonArray > downstream ) {
134
135
135
- BiConsumer <Map <String , JsonArrayBuilder >, JsonValue > accumulator =
136
+ BiConsumer <Map <String , T >, JsonValue > accumulator =
136
137
(map , value ) -> {
137
138
String key = classifier .apply (value );
138
139
if (key == null ) {
139
140
throw new JsonException ("element cannot be mapped to a null key" );
140
141
}
141
142
// Build a map of key to JsonArrayBuilder
142
- JsonArrayBuilder arrayBuilder =
143
+ T arrayBuilder =
143
144
map .computeIfAbsent (key , v ->downstream .supplier ().get ());
144
145
// Add elements from downstream Collector to the arrayBuilder.
145
146
downstream .accumulator ().accept (arrayBuilder , value );
146
147
};
147
- Function <Map <String , JsonArrayBuilder >, JsonObject > finisher =
148
+ Function <Map <String , T >, JsonObject > finisher =
148
149
map -> {
149
150
// transform the map of name: JsonArrayBuilder to
150
151
// name: JsonArray
@@ -156,7 +157,7 @@ public static Collector<Map.Entry<String, JsonValue>, JsonObjectBuilder, JsonObj
156
157
});
157
158
return objectBuilder .build ();
158
159
};
159
- BinaryOperator <Map <String , JsonArrayBuilder >> combiner =
160
+ BinaryOperator <Map <String , T >> combiner =
160
161
(map1 , map2 ) -> {
161
162
map1 .putAll (map2 );
162
163
return map1 ;
0 commit comments