Debian Codegraph @

src/jakarta/json/stream/JsonCollectors.java

raw symbols SCIP
1 /*
2 * Copyright (c) 2015, 2021 Oracle and/or its affiliates. All rights reserved.
3 *
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v. 2.0, which is available at
6 * http://www.eclipse.org/legal/epl-2.0.
7 *
8 * This Source Code may also be made available under the following Secondary
9 * Licenses when the conditions for such availability set forth in the
10 * Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
11 * version 2 with the GNU Classpath Exception, which is available at
12 * https://www.gnu.org/software/classpath/license.html.
13 *
14 * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15 */
16
17 package jakarta.json.stream;
18
19 import java.util.Map;
20 import java.util.HashMap;
21 import java.util.stream.Collector;
22 import java.util.function.BinaryOperator;
23 import java.util.function.Function;
24 import java.util.function.BiConsumer;
25
26 import jakarta.json.Json;
27 import jakarta.json.JsonArray;
28 import jakarta.json.JsonArrayBuilder;
29 import jakarta.json.JsonObject;
30 import jakarta.json.JsonObjectBuilder;
31 import jakarta.json.JsonValue;
32 import jakarta.json.JsonException;
33
34 /**
35 * This class contains some implementations of {@code java.util.stream.Collector} for accumulating
36 * {@link JsonValue}s into {@link JsonArray} and {@link JsonObject}.
37 *
38 * @since 1.1
39 */
40
41 public final class JsonCollectors {
42
43 /**
44 * No instantiation.
45 */
46 private JsonCollectors() {
47 }
48
49 /**
50 * Constructs a {@code java.util.stream.Collector} that accumulates the input {@code JsonValue}
51 * elements into a {@code JsonArray}.
52 *
53 * @return the constructed Collector
54 */
55 public static Collector<JsonValue, JsonArrayBuilder, JsonArray> toJsonArray() {
56 return Collector.of(
57 Json::createArrayBuilder,
58 JsonArrayBuilder::add,
59 JsonArrayBuilder::addAll,
60 JsonArrayBuilder::build);
61 }
62
63 /**
64 * Constructs a {@code java.util.stream.Collector} that accumulates the input {@code Map.Entry<String,JsonValue>}
65 * elements into a {@code JsonObject}.
66 *
67 * @return the constructed Collector
68 */
69 public static Collector<Map.Entry<String, JsonValue>, JsonObjectBuilder, JsonObject> toJsonObject() {
70 return Collector.of(
71 Json::createObjectBuilder,
72 (JsonObjectBuilder b, Map.Entry<String, JsonValue> v) -> b.add(v.getKey(), v.getValue()),
73 JsonObjectBuilder::addAll,
74 JsonObjectBuilder::build);
75 }
76
77 /**
78 * Constructs a {@code java.util.stream.Collector} that accumulates the input {@code JsonValue}
79 * elements into a {@code JsonObject}. The name/value pairs of the {@code JsonObject} are computed
80 * by applying the provided mapping functions.
81 *
82 * @param keyMapper a mapping function to produce names.
83 * @param valueMapper a mapping function to produce values
84 * @return the constructed Collector
85 */
86 public static Collector<JsonValue, JsonObjectBuilder, JsonObject>
87 toJsonObject(Function<JsonValue, String> keyMapper,
88 Function<JsonValue, JsonValue> valueMapper) {
89 return Collector.of(
90 Json::createObjectBuilder,
91 (b, v) -> b.add(keyMapper.apply(v), valueMapper.apply(v)),
92 JsonObjectBuilder::addAll,
93 JsonObjectBuilder::build,
94 Collector.Characteristics.UNORDERED);
95 }
96
97 /**
98 * Constructs a {@code java.util.stream.Collector} that implements a "group by" operation on the
99 * input {@code JsonValue} elements. A classifier function maps the input {@code JsonValue}s to keys, and
100 * the {@code JsonValue}s are partitioned into groups according to the value of the key.
101 * A reduction operation is performed on the {@code JsonValue}s in each group, using the
102 * downstream {@code Collector}. For each group, the key and the results of the reduction operation
103 * become the name/value pairs of the resultant {@code JsonObject}.
104 *
105 * @param <T> the intermediate accumulation {@code JsonArrayBuilder} of the downstream collector
106 * @param classifier a function mapping the input {@code JsonValue}s to a String, producing keys
107 * @param downstream a {@code Collector} that implements a reduction operation on the
108 * {@code JsonValue}s in each group.
109 * @return the constructed {@code Collector}
110 */
111 public static <T extends JsonArrayBuilder> Collector<JsonValue, Map<String, T>, JsonObject>
112 groupingBy(Function<JsonValue, String> classifier,
113 Collector<JsonValue, T, JsonArray> downstream) {
114
115 BiConsumer<Map<String, T>, JsonValue> accumulator =
116 (map, value) -> {
117 String key = classifier.apply(value);
118 if (key == null) {
119 throw new JsonException("element cannot be mapped to a null key");
120 }
121 // Build a map of key to JsonArrayBuilder
122 T arrayBuilder =
123 map.computeIfAbsent(key, v->downstream.supplier().get());
124 // Add elements from downstream Collector to the arrayBuilder.
125 downstream.accumulator().accept(arrayBuilder, value);
126 };
127 Function<Map<String, T>, JsonObject> finisher =
128 map -> {
129 // transform the map of name: JsonArrayBuilder to
130 // name: JsonArray
131 // using the downstream collector for reducing the JsonArray
132 JsonObjectBuilder objectBuilder = Json.createObjectBuilder();
133 map.forEach((k, v) -> {
134 JsonArray array = downstream.finisher().apply(v);
135 objectBuilder.add(k, array);
136 });
137 return objectBuilder.build();
138 };
139 BinaryOperator<Map<String, T>> combiner =
140 (map1, map2) -> {
141 map1.putAll(map2);
142 return map1;
143 };
144 return Collector.of(HashMap::new, accumulator, combiner, finisher,
145 Collector.Characteristics.UNORDERED);
146 }
147
148 /**
149 * Constructs a {@code java.util.stream.Collector} that implements a "group by" operation on the
150 * input {@code JsonValue} elements. A classifier function maps the input {@code JsonValue}s to keys, and
151 * the {@code JsonValue}s are partitioned into groups according to the value of the key.
152 * The {@code JsonValue}s in each group are added to a {@code JsonArray}. The key and the
153 * {@code JsonArray} in each group becomes the name/value pair of the resultant {@code JsonObject}.
154 *
155 * @param classifier a function mapping the input {@code JsonValue}s to a String, producing keys
156 * @return the constructed {@code Collector}
157 */
158 public static Collector<JsonValue, Map<String, JsonArrayBuilder>, JsonObject>
159 groupingBy(Function<JsonValue, String> classifier) {
160 return groupingBy(classifier, toJsonArray());
161 }
162 }
163