Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package javax.json.stream;
import javax.json.Json;
import javax.json.JsonArray;
import javax.json.JsonArrayBuilder;
import javax.json.JsonObject;
import javax.json.JsonObjectBuilder;
import javax.json.JsonValue;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collector;
public final class JsonCollectors {
private JsonCollectors() {
// no instantiation for utility classes
}
public static Collector toJsonArray() {
return Collector.of(Json::createArrayBuilder,
JsonArrayBuilder::add,
JsonArrayBuilder::addAll,
JsonArrayBuilder::build);
}
public static Collector, JsonObjectBuilder, JsonObject> toJsonObject() {
return Collector.of(Json::createObjectBuilder,
JsonCollectors::addEntry,
JsonObjectBuilder::addAll,
JsonObjectBuilder::build);
}
public static Collector toJsonObject(Function keyMapper,
Function valueMapper) {
return Collector.of(Json::createObjectBuilder,
(b, v) -> b.add(keyMapper.apply(v), valueMapper.apply(v)),
JsonObjectBuilder::addAll,
JsonObjectBuilder::build);
}
public static Collector, JsonObject> groupingBy(Function classifier,
Collector downstream) {
return Collector.of(HashMap::new,
(map, value) -> accumulator(map, value, classifier, downstream),
JsonCollectors::combiner,
m -> finisher(m, downstream.finisher()));
}
public static Collector, JsonObject> groupingBy(Function classifier) {
return groupingBy(classifier, toJsonArray());
}
private static void addEntry(JsonObjectBuilder objectBuilder, Map.Entry entry) {
objectBuilder.add(entry.getKey(), entry.getValue());
}
private static void accumulator(Map map,
JsonValue value,
Function classifier,
Collector downstream) {
String key = classifier.apply(value);
T arrayBuilder = map.computeIfAbsent(key, k -> downstream.supplier().get());
downstream.accumulator().accept(arrayBuilder, value);
}
private static Map combiner(Map target, Map source) {
target.putAll(source);
return target;
}
private static JsonObject finisher(Map arrayBuilders,
Function downstream) {
JsonObjectBuilder builder = Json.createObjectBuilder();
arrayBuilders.forEach((key, value) -> builder.add(key, downstream.apply(value)));
return builder.build();
}
}