Package javax.json.stream
Class JsonCollectors
- java.lang.Object
-
- javax.json.stream.JsonCollectors
-
public final class JsonCollectors extends java.lang.Object
This class contains some implementations ofjava.util.stream.Collector
for accumulatingJsonValue
s intoJsonArray
andJsonObject
.
This is a near-exact copy of the same-titled Java EE 8 Class:javax.json.stream.JsonCollectors
Commenting has been slightly modified to accompany HiLited Code Examples.
Java Source Code remains identical to the Sun-Oracle & 'GlassFish' Released Distributions.
Read included License:HERE
, and theCDDL+GPL-1.1
Alljavax.json.stream.*
Code Obtained From:GitHub JavaEE jsonp
Public Archive.- Since:
- 1.1
Hi-Lited Source-Code:- View Here: javax/json/stream/JsonCollectors.java
- Open New Browser-Tab: javax/json/stream/JsonCollectors.java
File Size: 8,405 Bytes Line Count: 185 '\n' Characters Found
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static Collector<JsonValue,
Map<String,
JsonArrayBuilder>,
JsonObject>groupingBy(Function<JsonValue,String> classifier)
Constructs aCollector
that implements a "group by" operation on the inputJsonValue
elements.static <T extends JsonArrayBuilder>
Collector<JsonValue,
Map<String,
T>,
JsonObject>groupingBy(Function<JsonValue,String> classifier, Collector<JsonValue,T,JsonArray> downstream)
Constructs aCollector
that implements a "group by" operation on the inputJsonValue
elements.static Collector<JsonValue,
JsonArrayBuilder,
JsonArray>toJsonArray()
Constructs aCollector
that accumulates the inputJsonValue
elements into aJsonArray
.static Collector<Map.Entry<String,
JsonValue>,
JsonObjectBuilder,
JsonObject>toJsonObject()
Constructs aCollector
that accumulates the inputMap.Entry<String,JsonValue>
elements into aJsonObject
.static Collector<JsonValue,
JsonObjectBuilder,
JsonObject>toJsonObject(Function<JsonValue,String> keyMapper, Function<JsonValue,JsonValue> valueMapper)
Constructs aCollector
that accumulates the inputJsonValue
elements into aJsonObject
.
-
-
-
Method Detail
-
toJsonArray
public static java.util.stream.Collector<JsonValue,JsonArrayBuilder,JsonArray> toJsonArray ()
Constructs ajava.util.stream.Collector
that accumulates the inputJsonValue
elements into aJsonArray
.- Returns:
- the constructed Collector
- Code:
- Exact Method Body:
return Collector.of( Json::createArrayBuilder, JsonArrayBuilder::add, JsonArrayBuilder::addAll, JsonArrayBuilder::build);
-
toJsonObject
public static java.util.stream.Collector<java.util.Map.Entry<java.lang.String,JsonValue>,JsonObjectBuilder,JsonObject> toJsonObject ()
Constructs ajava.util.stream.Collector
that accumulates the inputMap.Entry<String,JsonValue>
elements into aJsonObject
.- Returns:
- the constructed Collector
- Code:
- Exact Method Body:
return Collector.of( Json::createObjectBuilder, (JsonObjectBuilder b, Map.Entry<String, JsonValue> v) -> b.add(v.getKey(), v.getValue()), JsonObjectBuilder::addAll, JsonObjectBuilder::build);
-
toJsonObject
public static java.util.stream.Collector<JsonValue,JsonObjectBuilder,JsonObject> toJsonObject (java.util.function.Function<JsonValue,java.lang.String> keyMapper, java.util.function.Function<JsonValue,JsonValue> valueMapper)
Constructs ajava.util.stream.Collector
that accumulates the inputJsonValue
elements into aJsonObject
. The name/value pairs of theJsonObject
are computed by applying the provided mapping functions.- Parameters:
keyMapper
- a mapping function to produce names.valueMapper
- a mapping function to produce values- Returns:
- the constructed Collector
- Code:
- Exact Method Body:
return Collector.of( Json::createObjectBuilder, (b, v) -> b.add(keyMapper.apply(v), valueMapper.apply(v)), JsonObjectBuilder::addAll, JsonObjectBuilder::build, Collector.Characteristics.UNORDERED);
-
groupingBy
public static <T extends JsonArrayBuilder> java.util.stream.Collector<JsonValue,java.util.Map<java.lang.String,T>,JsonObject> groupingBy (java.util.function.Function<JsonValue,java.lang.String> classifier, java.util.stream.Collector<JsonValue,T,JsonArray> downstream)
Constructs ajava.util.stream.Collector
that implements a "group by" operation on the inputJsonValue
elements. A classifier function maps the inputJsonValue
s to keys, and theJsonValue
s are partitioned into groups according to the value of the key. A reduction operation is performed on theJsonValue
s in each group, using the downstreamCollector
. For each group, the key and the results of the reduction operation become the name/value pairs of the resultantJsonObject
.- Type Parameters:
T
- the intermediate accumulationJsonArrayBuilder
of the downstream collector- Parameters:
classifier
- a function mapping the inputJsonValue
s to a String, producing keysdownstream
- aCollector
that implements a reduction operation on theJsonValue
s in each group.- Returns:
- the constructed
Collector
- Code:
- Exact Method Body:
BiConsumer<Map<String, T>, JsonValue> accumulator = (map, value) -> { String key = classifier.apply(value); if (key == null) { throw new JsonException("element cannot be mapped to a null key"); } // Build a map of key to JsonArrayBuilder T arrayBuilder = map.computeIfAbsent(key, v->downstream.supplier().get()); // Add elements from downstream Collector to the arrayBuilder. downstream.accumulator().accept(arrayBuilder, value); }; Function<Map<String, T>, JsonObject> finisher = map -> { // transform the map of name: JsonArrayBuilder to // name: JsonArray // using the downstream collector for reducing the JsonArray JsonObjectBuilder objectBuilder = Json.createObjectBuilder(); map.forEach((k, v) -> { JsonArray array = downstream.finisher().apply(v); objectBuilder.add(k, array); }); return objectBuilder.build(); }; BinaryOperator<Map<String, T>> combiner = (map1, map2) -> { map1.putAll(map2); return map1; }; return Collector.of(HashMap::new, accumulator, combiner, finisher, Collector.Characteristics.UNORDERED);
-
groupingBy
public static java.util.stream.Collector<JsonValue,java.util.Map<java.lang.String,JsonArrayBuilder>,JsonObject> groupingBy (java.util.function.Function<JsonValue,java.lang.String> classifier)
Constructs ajava.util.stream.Collector
that implements a "group by" operation on the inputJsonValue
elements. A classifier function maps the inputJsonValue
s to keys, and theJsonValue
s are partitioned into groups according to the value of the key. TheJsonValue
s in each group are added to aJsonArray
. The key and theJsonArray
in each group becomes the name/value pair of the resultantJsonObject
.- Parameters:
classifier
- a function mapping the inputJsonValue
s to a String, producing keys- Returns:
- the constructed
Collector
- Code:
- Exact Method Body:
return groupingBy(classifier, toJsonArray());
-
-