| 1 | /*
|
|---|
| 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
|
|---|
| 3 | *
|
|---|
| 4 | * Copyright (c) 2015-2017 Oracle and/or its affiliates. All rights reserved.
|
|---|
| 5 | *
|
|---|
| 6 | * The contents of this file are subject to the terms of either the GNU
|
|---|
| 7 | * General Public License Version 2 only ("GPL") or the Common Development
|
|---|
| 8 | * and Distribution License("CDDL") (collectively, the "License"). You
|
|---|
| 9 | * may not use this file except in compliance with the License. You can
|
|---|
| 10 | * obtain a copy of the License at
|
|---|
| 11 | * https://oss.oracle.com/licenses/CDDL+GPL-1.1
|
|---|
| 12 | * or LICENSE.txt. See the License for the specific
|
|---|
| 13 | * language governing permissions and limitations under the License.
|
|---|
| 14 | *
|
|---|
| 15 | * When distributing the software, include this License Header Notice in each
|
|---|
| 16 | * file and include the License file at LICENSE.txt.
|
|---|
| 17 | *
|
|---|
| 18 | * GPL Classpath Exception:
|
|---|
| 19 | * Oracle designates this particular file as subject to the "Classpath"
|
|---|
| 20 | * exception as provided by Oracle in the GPL Version 2 section of the License
|
|---|
| 21 | * file that accompanied this code.
|
|---|
| 22 | *
|
|---|
| 23 | * Modifications:
|
|---|
| 24 | * If applicable, add the following below the License Header, with the fields
|
|---|
| 25 | * enclosed by brackets [] replaced by your own identifying information:
|
|---|
| 26 | * "Portions Copyright [year] [name of copyright owner]"
|
|---|
| 27 | *
|
|---|
| 28 | * Contributor(s):
|
|---|
| 29 | * If you wish your version of this file to be governed by only the CDDL or
|
|---|
| 30 | * only the GPL Version 2, indicate your decision by adding "[Contributor]
|
|---|
| 31 | * elects to include this software in this distribution under the [CDDL or GPL
|
|---|
| 32 | * Version 2] license." If you don't indicate a single choice of license, a
|
|---|
| 33 | * recipient has the option to distribute your version of this file under
|
|---|
| 34 | * either the CDDL, the GPL Version 2 or to extend the choice of license to
|
|---|
| 35 | * its licensees as provided above. However, if you add GPL Version 2 code
|
|---|
| 36 | * and therefore, elected the GPL Version 2 license, then the option applies
|
|---|
| 37 | * only if the new code is made subject to such option by the copyright
|
|---|
| 38 | * holder.
|
|---|
| 39 | */
|
|---|
| 40 |
|
|---|
| 41 | package javax.json.stream;
|
|---|
| 42 |
|
|---|
| 43 | import java.util.Map;
|
|---|
| 44 | import java.util.HashMap;
|
|---|
| 45 | import java.util.stream.Collector;
|
|---|
| 46 | import java.util.function.BinaryOperator;
|
|---|
| 47 | import java.util.function.Function;
|
|---|
| 48 | import java.util.function.BiConsumer;
|
|---|
| 49 |
|
|---|
| 50 | import javax.json.Json;
|
|---|
| 51 | import javax.json.JsonArray;
|
|---|
| 52 | import javax.json.JsonArrayBuilder;
|
|---|
| 53 | import javax.json.JsonObject;
|
|---|
| 54 | import javax.json.JsonObjectBuilder;
|
|---|
| 55 | import javax.json.JsonValue;
|
|---|
| 56 | import javax.json.JsonException;
|
|---|
| 57 |
|
|---|
| 58 | /**
|
|---|
| 59 | * This class contains some implementations of {@code java.util.stream.Collector} for accumulating
|
|---|
| 60 | * {@link JsonValue}s into {@link JsonArray} and {@link JsonObject}.
|
|---|
| 61 | *
|
|---|
| 62 | * @since 1.1
|
|---|
| 63 | */
|
|---|
| 64 |
|
|---|
| 65 | public final class JsonCollectors {
|
|---|
| 66 |
|
|---|
| 67 | private JsonCollectors() {
|
|---|
| 68 | }
|
|---|
| 69 |
|
|---|
| 70 | /**
|
|---|
| 71 | * Constructs a {@code java.util.stream.Collector} that accumulates the input {@code JsonValue}
|
|---|
| 72 | * elements into a {@code JsonArray}.
|
|---|
| 73 | *
|
|---|
| 74 | * @return the constructed Collector
|
|---|
| 75 | */
|
|---|
| 76 | public static Collector<JsonValue, JsonArrayBuilder, JsonArray> toJsonArray() {
|
|---|
| 77 | return Collector.of(
|
|---|
| 78 | Json::createArrayBuilder,
|
|---|
| 79 | JsonArrayBuilder::add,
|
|---|
| 80 | JsonArrayBuilder::addAll,
|
|---|
| 81 | JsonArrayBuilder::build);
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 | /**
|
|---|
| 85 | * Constructs a {@code java.util.stream.Collector} that accumulates the input {@code Map.Entry<String,JsonValue>}
|
|---|
| 86 | * elements into a {@code JsonObject}.
|
|---|
| 87 | *
|
|---|
| 88 | * @return the constructed Collector
|
|---|
| 89 | */
|
|---|
| 90 | public static Collector<Map.Entry<String, JsonValue>, JsonObjectBuilder, JsonObject> toJsonObject() {
|
|---|
| 91 | return Collector.of(
|
|---|
| 92 | Json::createObjectBuilder,
|
|---|
| 93 | (JsonObjectBuilder b, Map.Entry<String, JsonValue> v) -> b.add(v.getKey(), v.getValue()),
|
|---|
| 94 | JsonObjectBuilder::addAll,
|
|---|
| 95 | JsonObjectBuilder::build);
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 | /**
|
|---|
| 99 | * Constructs a {@code java.util.stream.Collector} that accumulates the input {@code JsonValue}
|
|---|
| 100 | * elements into a {@code JsonObject}. The name/value pairs of the {@code JsonObject} are computed
|
|---|
| 101 | * by applying the provided mapping functions.
|
|---|
| 102 | *
|
|---|
| 103 | * @param keyMapper a mapping function to produce names.
|
|---|
| 104 | * @param valueMapper a mapping function to produce values
|
|---|
| 105 | * @return the constructed Collector
|
|---|
| 106 | */
|
|---|
| 107 | public static Collector<JsonValue, JsonObjectBuilder, JsonObject>
|
|---|
| 108 | toJsonObject(Function<JsonValue, String> keyMapper,
|
|---|
| 109 | Function<JsonValue, JsonValue> valueMapper) {
|
|---|
| 110 | return Collector.of(
|
|---|
| 111 | Json::createObjectBuilder,
|
|---|
| 112 | (b, v) -> b.add(keyMapper.apply(v), valueMapper.apply(v)),
|
|---|
| 113 | JsonObjectBuilder::addAll,
|
|---|
| 114 | JsonObjectBuilder::build,
|
|---|
| 115 | Collector.Characteristics.UNORDERED);
|
|---|
| 116 | }
|
|---|
| 117 |
|
|---|
| 118 | /**
|
|---|
| 119 | * Constructs a {@code java.util.stream.Collector} that implements a "group by" operation on the
|
|---|
| 120 | * input {@code JsonValue} elements. A classifier function maps the input {@code JsonValue}s to keys, and
|
|---|
| 121 | * the {@code JsonValue}s are partitioned into groups according to the value of the key.
|
|---|
| 122 | * A reduction operation is performed on the {@code JsonValue}s in each group, using the
|
|---|
| 123 | * downstream {@code Collector}. For each group, the key and the results of the reduction operation
|
|---|
| 124 | * become the name/value pairs of the resultant {@code JsonObject}.
|
|---|
| 125 | *
|
|---|
| 126 | * @param <T> the intermediate accumulation {@code JsonArrayBuilder} of the downstream collector
|
|---|
| 127 | * @param classifier a function mapping the input {@code JsonValue}s to a String, producing keys
|
|---|
| 128 | * @param downstream a {@code Collector} that implements a reduction operation on the
|
|---|
| 129 | * {@code JsonValue}s in each group.
|
|---|
| 130 | * @return the constructed {@code Collector}
|
|---|
| 131 | */
|
|---|
| 132 | public static <T extends JsonArrayBuilder> Collector<JsonValue, Map<String, T>, JsonObject>
|
|---|
| 133 | groupingBy(Function<JsonValue, String> classifier,
|
|---|
| 134 | Collector<JsonValue, T, JsonArray> downstream) {
|
|---|
| 135 |
|
|---|
| 136 | BiConsumer<Map<String, T>, JsonValue> accumulator =
|
|---|
| 137 | (map, value) -> {
|
|---|
| 138 | String key = classifier.apply(value);
|
|---|
| 139 | if (key == null) {
|
|---|
| 140 | throw new JsonException("element cannot be mapped to a null key");
|
|---|
| 141 | }
|
|---|
| 142 | // Build a map of key to JsonArrayBuilder
|
|---|
| 143 | T arrayBuilder =
|
|---|
| 144 | map.computeIfAbsent(key, v->downstream.supplier().get());
|
|---|
| 145 | // Add elements from downstream Collector to the arrayBuilder.
|
|---|
| 146 | downstream.accumulator().accept(arrayBuilder, value);
|
|---|
| 147 | };
|
|---|
| 148 | Function<Map<String, T>, JsonObject> finisher =
|
|---|
| 149 | map -> {
|
|---|
| 150 | // transform the map of name: JsonArrayBuilder to
|
|---|
| 151 | // name: JsonArray
|
|---|
| 152 | // using the downstream collector for reducing the JsonArray
|
|---|
| 153 | JsonObjectBuilder objectBuilder = Json.createObjectBuilder();
|
|---|
| 154 | map.forEach((k, v) -> {
|
|---|
| 155 | JsonArray array = downstream.finisher().apply(v);
|
|---|
| 156 | objectBuilder.add(k, array);
|
|---|
| 157 | });
|
|---|
| 158 | return objectBuilder.build();
|
|---|
| 159 | };
|
|---|
| 160 | BinaryOperator<Map<String, T>> combiner =
|
|---|
| 161 | (map1, map2) -> {
|
|---|
| 162 | map1.putAll(map2);
|
|---|
| 163 | return map1;
|
|---|
| 164 | };
|
|---|
| 165 | return Collector.of(HashMap::new, accumulator, combiner, finisher,
|
|---|
| 166 | Collector.Characteristics.UNORDERED);
|
|---|
| 167 | }
|
|---|
| 168 |
|
|---|
| 169 | /**
|
|---|
| 170 | * Constructs a {@code java.util.stream.Collector} that implements a "group by" operation on the
|
|---|
| 171 | * input {@code JsonValue} elements. A classifier function maps the input {@code JsonValue}s to keys, and
|
|---|
| 172 | * the {@code JsonValue}s are partitioned into groups according to the value of the key.
|
|---|
| 173 | * The {@code JsonValue}s in each group are added to a {@code JsonArray}. The key and the
|
|---|
| 174 | * {@code JsonArray} in each group becomes the name/value pair of the resultant {@code JsonObject}.
|
|---|
| 175 | *
|
|---|
| 176 | * @param classifier a function mapping the input {@code JsonValue}s to a String, producing keys
|
|---|
| 177 | * @return the constructed {@code Collector}
|
|---|
| 178 | */
|
|---|
| 179 | public static Collector<JsonValue, Map<String, JsonArrayBuilder>, JsonObject>
|
|---|
| 180 | groupingBy(Function<JsonValue, String> classifier) {
|
|---|
| 181 | return groupingBy(classifier, toJsonArray());
|
|---|
| 182 | }
|
|---|
| 183 | }
|
|---|
| 184 |
|
|---|