Skip to content
This repository was archived by the owner on Nov 7, 2024. It is now read-only.

Commit 7a40191

Browse files
committed
JSON_PROCESSING_SPEC-68: Support JSON queries using JDK's stream operations - allow usage of custom JsonArrayBuilder
Signed-off-by: Lukas Jungmann <lukas.jungmann@oracle.com>
1 parent 32a0fea commit 7a40191

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

api/src/main/java/javax/json/stream/JsonCollectors.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -123,28 +123,29 @@ public static Collector<Map.Entry<String, JsonValue>, JsonObjectBuilder, JsonObj
123123
* downstream {@code Collector}. For each group, the key and the results of the reduction operation
124124
* become the name/value pairs of the resultant {@code JsonObject}.
125125
*
126+
* @param <T> the intermediate accumulation {@code JsonArrayBuilder} of the downstream collector
126127
* @param classifier a function mapping the input {@code JsonValue}s to a String, producing keys
127128
* @param downstream a {@code Collector} that implements a reduction operation on the
128129
* {@code JsonValue}s in each group.
129130
* @return the constructed {@code Collector}
130131
*/
131-
public static Collector<JsonValue, Map<String, JsonArrayBuilder>, JsonObject>
132+
public static <T extends JsonArrayBuilder> Collector<JsonValue, Map<String, T>, JsonObject>
132133
groupingBy(Function<JsonValue, String> classifier,
133-
Collector<JsonValue, JsonArrayBuilder, JsonArray> downstream) {
134+
Collector<JsonValue, T, JsonArray> downstream) {
134135

135-
BiConsumer<Map<String, JsonArrayBuilder>, JsonValue> accumulator =
136+
BiConsumer<Map<String, T>, JsonValue> accumulator =
136137
(map, value) -> {
137138
String key = classifier.apply(value);
138139
if (key == null) {
139140
throw new JsonException("element cannot be mapped to a null key");
140141
}
141142
// Build a map of key to JsonArrayBuilder
142-
JsonArrayBuilder arrayBuilder =
143+
T arrayBuilder =
143144
map.computeIfAbsent(key, v->downstream.supplier().get());
144145
// Add elements from downstream Collector to the arrayBuilder.
145146
downstream.accumulator().accept(arrayBuilder, value);
146147
};
147-
Function<Map<String, JsonArrayBuilder>, JsonObject> finisher =
148+
Function<Map<String, T>, JsonObject> finisher =
148149
map -> {
149150
// transform the map of name: JsonArrayBuilder to
150151
// name: JsonArray
@@ -156,7 +157,7 @@ public static Collector<Map.Entry<String, JsonValue>, JsonObjectBuilder, JsonObj
156157
});
157158
return objectBuilder.build();
158159
};
159-
BinaryOperator<Map<String, JsonArrayBuilder>> combiner =
160+
BinaryOperator<Map<String, T>> combiner =
160161
(map1, map2) -> {
161162
map1.putAll(map2);
162163
return map1;

0 commit comments

Comments
 (0)