geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rsandt...@apache.org
Subject svn commit: r1789209 - in /geronimo/specs/trunk/geronimo-json_1.1_spec: pom.xml src/main/java/javax/json/stream/JsonCollectors.java
Date Tue, 28 Mar 2017 20:21:51 GMT
Author: rsandtner
Date: Tue Mar 28 20:21:51 2017
New Revision: 1789209

URL: http://svn.apache.org/viewvc?rev=1789209&view=rev
Log:
GERONIMO-6558 implemented JsonCollectors, upgrade bundle-plugin

Modified:
    geronimo/specs/trunk/geronimo-json_1.1_spec/pom.xml
    geronimo/specs/trunk/geronimo-json_1.1_spec/src/main/java/javax/json/stream/JsonCollectors.java

Modified: geronimo/specs/trunk/geronimo-json_1.1_spec/pom.xml
URL: http://svn.apache.org/viewvc/geronimo/specs/trunk/geronimo-json_1.1_spec/pom.xml?rev=1789209&r1=1789208&r2=1789209&view=diff
==============================================================================
--- geronimo/specs/trunk/geronimo-json_1.1_spec/pom.xml (original)
+++ geronimo/specs/trunk/geronimo-json_1.1_spec/pom.xml Tue Mar 28 20:21:51 2017
@@ -77,7 +77,7 @@
       <plugin>
         <groupId>org.apache.felix</groupId>
         <artifactId>maven-bundle-plugin</artifactId>
-        <version>2.0.1</version>
+        <version>3.3.0</version>
         <configuration>
           <instructions>
             <Export-Package>

Modified: geronimo/specs/trunk/geronimo-json_1.1_spec/src/main/java/javax/json/stream/JsonCollectors.java
URL: http://svn.apache.org/viewvc/geronimo/specs/trunk/geronimo-json_1.1_spec/src/main/java/javax/json/stream/JsonCollectors.java?rev=1789209&r1=1789208&r2=1789209&view=diff
==============================================================================
--- geronimo/specs/trunk/geronimo-json_1.1_spec/src/main/java/javax/json/stream/JsonCollectors.java
(original)
+++ geronimo/specs/trunk/geronimo-json_1.1_spec/src/main/java/javax/json/stream/JsonCollectors.java
Tue Mar 28 20:21:51 2017
@@ -17,52 +17,88 @@
 
 package javax.json.stream;
 
-import java.util.Map;
-import java.util.function.Function;
-import java.util.stream.Collector;
-
+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<JsonValue, JsonArrayBuilder, JsonArray> toJsonArray() {
+
+        return Collector.of(Json::createArrayBuilder,
+                            JsonArrayBuilder::add,
+                            JsonArrayBuilder::addAll,
+                            JsonArrayBuilder::build);
     }
 
-    public static Collector<JsonValue, JsonArrayBuilder, JsonArray>
-                toJsonArray() {
-        //X TODO implement!
-        return null;
+    public static Collector<Map.Entry<String, JsonValue>, JsonObjectBuilder, JsonObject>
toJsonObject() {
+
+        return Collector.of(Json::createObjectBuilder,
+                            JsonCollectors::addEntry,
+                            JsonObjectBuilder::addAll,
+                            JsonObjectBuilder::build);
+    }
+
+    public static Collector<JsonValue, JsonObjectBuilder, JsonObject> toJsonObject(Function<JsonValue,
String> keyMapper,
+                                                                                   Function<JsonValue,
JsonValue> valueMapper) {
+
+        return Collector.of(Json::createObjectBuilder,
+                            (b, v) -> b.add(keyMapper.apply(v), valueMapper.apply(v)),
+                            JsonObjectBuilder::addAll,
+                            JsonObjectBuilder::build);
     }
 
-    public static Collector<Map.Entry<String, JsonValue>, JsonObjectBuilder, JsonObject>
-                toJsonObject() {
-        //X TODO implement!
-        return null;
+    public static <T extends JsonArrayBuilder> Collector<JsonValue, Map<String,
T>, JsonObject> groupingBy(Function<JsonValue, String> classifier,
+                                                                                        
                  Collector<JsonValue, T, JsonArray> downstream) {
+        return Collector.of(HashMap::new,
+                            (map, value) -> accumulator(map, value, classifier, downstream),
+                            JsonCollectors::combiner,
+                            m -> finisher(m, downstream.finisher()));
     }
 
-    public static Collector<JsonValue, JsonObjectBuilder, JsonObject>
-                toJsonObject(Function<JsonValue, String> keyMapper,
-                             Function<JsonValue, JsonValue> valueMapper) {
-        //X TODO implement!
-        return null;
+    public static Collector<JsonValue, Map<String, JsonArrayBuilder>, JsonObject>
groupingBy(Function<JsonValue, String> classifier) {
+        return groupingBy(classifier, toJsonArray());
     }
 
-    public static Collector<JsonValue, Map<String, JsonArrayBuilder>, JsonObject>
-                groupingBy(Function<JsonValue, String> classifier,
-                           Collector<JsonValue, JsonArrayBuilder, JsonArray> downstream)
{
 
-        //X TODO implement!
-        return null;
+    private static void addEntry(JsonObjectBuilder objectBuilder, Map.Entry<String, JsonValue>
entry) {
+        objectBuilder.add(entry.getKey(), entry.getValue());
     }
 
-    public static Collector<JsonValue, Map<String, JsonArrayBuilder>, JsonObject>
-                groupingBy(Function<JsonValue, String> classifier) {
-        //X TODO implement!
-        return null;
+    private static <T extends JsonArrayBuilder> void accumulator(Map<String, T>
map,
+                                                                 JsonValue value,
+                                                                 Function<JsonValue, String>
classifier,
+                                                                 Collector<JsonValue,
T, JsonArray> downstream) {
+
+        String key = classifier.apply(value);
+        T arrayBuilder = map.computeIfAbsent(key, k -> downstream.supplier().get());
+        downstream.accumulator().accept(arrayBuilder, value);
+    }
+
+    private static <T extends JsonArrayBuilder> Map<String, T> combiner(Map<String,
T> target, Map<String, T> source) {
+        target.putAll(source);
+        return target;
+    }
+
+    private static <T extends JsonArrayBuilder> JsonObject finisher(Map<String,
T> arrayBuilders,
+                                                                    Function<T, JsonArray>
downstream) {
+
+        JsonObjectBuilder builder = Json.createObjectBuilder();
+        arrayBuilders.forEach((key, value) -> builder.add(key, downstream.apply(value)));
+
+        return builder.build();
     }
 }
 



Mime
View raw message