Repository: incubator-edgent
Updated Branches:
  refs/heads/master f43ec0846 -> 6fc7b66f6


[Edgent-407] JsonFunctions additions

Project: http://git-wip-us.apache.org/repos/asf/incubator-edgent/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-edgent/commit/3812c985
Tree: http://git-wip-us.apache.org/repos/asf/incubator-edgent/tree/3812c985
Diff: http://git-wip-us.apache.org/repos/asf/incubator-edgent/diff/3812c985

Branch: refs/heads/master
Commit: 3812c985a741b25465fde59c06212ca518051b04
Parents: f43ec08
Author: Dale LaBossiere <dlab...@us.ibm.com>
Authored: Thu Apr 13 10:44:15 2017 -0400
Committer: Dale LaBossiere <dlab...@us.ibm.com>
Committed: Thu Apr 13 10:44:15 2017 -0400

----------------------------------------------------------------------
 .../edgent/topology/json/JsonFunctions.java     | 112 +++++++++++++++++--
 .../edgent/test/topology/JsonFunctionsTest.java |  46 ++++++++
 2 files changed, 149 insertions(+), 9 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-edgent/blob/3812c985/api/topology/src/main/java/org/apache/edgent/topology/json/JsonFunctions.java
----------------------------------------------------------------------
diff --git 
a/api/topology/src/main/java/org/apache/edgent/topology/json/JsonFunctions.java 
b/api/topology/src/main/java/org/apache/edgent/topology/json/JsonFunctions.java
index d18a234..37a9eea 100644
--- 
a/api/topology/src/main/java/org/apache/edgent/topology/json/JsonFunctions.java
+++ 
b/api/topology/src/main/java/org/apache/edgent/topology/json/JsonFunctions.java
@@ -22,6 +22,7 @@ import java.nio.charset.StandardCharsets;
 
 import org.apache.edgent.function.Function;
 
+import com.google.gson.JsonElement;
 import com.google.gson.JsonObject;
 import com.google.gson.JsonParser;
 
@@ -30,22 +31,28 @@ import com.google.gson.JsonParser;
  */
 public class JsonFunctions {
 
+    private static final JsonElement ZERO_ELEMENT = new 
JsonParser().parse("0");
+    private static final Function<JsonObject,JsonElement> ZERO = jo -> 
ZERO_ELEMENT;
+
     /**
      * Get the JSON for a JsonObject.
      * 
-     * TODO consider adding an override where the caller can specify
-     * the number of significant digits to include in the string representation
-     * of floating point types.
+     * <p>Returns a Function whose {@code apply(JsonObject jo)} returns the 
JSON
+     * for the {@code jo}.
      * 
-     * @return the JSON
+     * @return the Function
      */
     public static Function<JsonObject,String> asString() {
         return jo -> jo.toString();
     }
 
     /**
-     * Create a new JsonObject from JSON
-     * @return the JsonObject
+     * Create a new JsonObject from JSON.
+     * 
+     * <p>Returns a Function whose {@code apply(String json)} creates a 
JsonObject
+     * from the {@code json}.
+     * 
+     * @return the Function
      */
     public static Function<String,JsonObject> fromString() {
         JsonParser jp = new JsonParser();
@@ -54,19 +61,106 @@ public class JsonFunctions {
 
     /**
      * Get the UTF-8 bytes representation of the JSON for a JsonObject.
-     * @return the byte[]
+     * 
+     * <p>Returns a Function whose {@code apply(JsonObject jo)} returns
+     * the UTF-8 bytes for the JSON of {@code jo}.
+     * 
+     * @return the Function
      */
     public static Function<JsonObject,byte[]> asBytes() {
         return jo -> jo.toString().getBytes(StandardCharsets.UTF_8);
     }
 
     /**
-     * Create a new JsonObject from the UTF8 bytes representation of JSON
-     * @return the JsonObject
+     * Create a new JsonObject from the UTF8 bytes representation of JSON.
+     * 
+     * <p>Returns a Function whose {@code apply(byte[] bytes)} returns
+     * a JsonObject from the {@code bytes}.
+     * 
+     * @return the Function
      */
     public static Function<byte[],JsonObject> fromBytes() {
         JsonParser jp = new JsonParser();
         return jsonbytes -> jp.parse(new String(jsonbytes, 
StandardCharsets.UTF_8)).getAsJsonObject();
     }
+  
+    /**
+     * Returns a constant function that returns a zero (0) JsonElement.
+     * 
+     * <p>Useful for an unpartitioned {@code TWindow<JsonObject,JsonElement>}. 
+     * 
+     * @return Constant function that returns a zero (0) JsonElement.
+     */
+    public static Function<JsonObject,JsonElement> unpartitioned() {
+      return ZERO;
+    }
+
+    /**
+     * Create a JsonObject with a {@code Number} property.
+     * 
+     * <p>Returns a Function whose {@code apply(T v)} returns a JsonObject 
having 
+     * a single property named {@code propName} with the value of {@code v}.
+     * 
+     * @param propName property name
+     * @return the Function
+     */
+    public static <T extends Number> Function<T,JsonObject> 
valueOfNumber(String propName) {
+      return v -> {
+        JsonObject jo = new JsonObject();
+        jo.addProperty(propName, v);
+        return jo;
+      };
+    }
+    
+    /**
+     * Create a JsonObject with a {@code Boolean} property.
+     * 
+     * <p>Returns a Function whose {@code apply(Boolean v)} creates a new 
JsonObject having 
+     * a single property named {@code propName} with the value of {@code v}.
+     *  
+     * @param propName property name
+     * @return the Function
+     */
+    public static Function<Boolean,JsonObject> valueOfBoolean(String propName) 
{
+      return v -> {
+        JsonObject jo = new JsonObject();
+        jo.addProperty(propName, v);
+        return jo;
+      };
+    }
+    
+    /**
+     * Create a JsonObject with a {@code String} property.
+     * 
+     * <p>Returns a Function whose {@code apply(String v)} creates a new 
JsonObject having 
+     * a single property named {@code propName} with the value of {@code v}.
+     * 
+     * @param propName property name
+     * @return the Function
+     */
+    public static Function<String,JsonObject> valueOfString(String propName) {
+      return v -> {
+        JsonObject jo = new JsonObject();
+        jo.addProperty(propName, v);
+        return jo;
+      };
+    }
+    
+    /**
+     * Create a JsonObject with a {@code Character} property.
+     * 
+     * <p>Returns a Function whose {@code apply(Character v)} creates a new 
JsonObject having 
+     * a single property named {@code propName} with the value of {@code v}.
+     * 
+     * @param propName property name
+     * @return the Function
+     */
+    public static Function<Character,JsonObject> valueOfCharacter(String 
propName) {
+      return v -> {
+        JsonObject jo = new JsonObject();
+        jo.addProperty(propName, v);
+        return jo;
+      };
+    }
 
 }

http://git-wip-us.apache.org/repos/asf/incubator-edgent/blob/3812c985/api/topology/src/test/java/org/apache/edgent/test/topology/JsonFunctionsTest.java
----------------------------------------------------------------------
diff --git 
a/api/topology/src/test/java/org/apache/edgent/test/topology/JsonFunctionsTest.java
 
b/api/topology/src/test/java/org/apache/edgent/test/topology/JsonFunctionsTest.java
index a9ff459..e9a031e 100644
--- 
a/api/topology/src/test/java/org/apache/edgent/test/topology/JsonFunctionsTest.java
+++ 
b/api/topology/src/test/java/org/apache/edgent/test/topology/JsonFunctionsTest.java
@@ -25,6 +25,7 @@ import org.apache.edgent.topology.json.JsonFunctions;
 import org.junit.Test;
 
 import com.google.gson.JsonArray;
+import com.google.gson.JsonElement;
 import com.google.gson.JsonObject;
 import com.google.gson.JsonPrimitive;
 
@@ -75,4 +76,49 @@ public class JsonFunctionsTest {
         
         assertEquals(jo2, jo1);
     }
+    
+    @Test
+    public void testUnpartitioned() {
+        Function<JsonObject,JsonElement> unpartitionedFn = 
JsonFunctions.unpartitioned();
+        assertEquals(0, unpartitionedFn.apply(new JsonObject()).getAsInt());
+    }
+    
+    @Test
+    public void testValueOfNumber() {
+      JsonObject joShort = 
JsonFunctions.valueOfNumber("propName").apply(Short.MAX_VALUE);
+      assertEquals(Short.MAX_VALUE, joShort.get("propName").getAsShort());
+      
+      JsonObject joInt = 
JsonFunctions.valueOfNumber("propName").apply(Integer.MAX_VALUE);
+      assertEquals(Integer.MAX_VALUE, joInt.get("propName").getAsInt());
+      
+      JsonObject joLong = 
JsonFunctions.valueOfNumber("propName").apply(Long.MAX_VALUE);
+      assertEquals(Long.MAX_VALUE, joLong.get("propName").getAsLong());
+      
+      JsonObject joFloat = 
JsonFunctions.valueOfNumber("propName").apply(Float.MAX_VALUE);
+      assertEquals(Float.MAX_VALUE, joFloat.get("propName").getAsFloat(), 
0.0f);
+      
+      JsonObject joDouble = 
JsonFunctions.valueOfNumber("propName").apply(Double.MAX_VALUE);
+      assertEquals(Double.MAX_VALUE, joDouble.get("propName").getAsDouble(), 
0.0d);
+    }
+    
+    @Test
+    public void testValueOfBoolean() {
+      JsonObject joTrue = JsonFunctions.valueOfBoolean("propName").apply(true);
+      assertEquals(true, joTrue.get("propName").getAsBoolean());
+
+      JsonObject joFalse = 
JsonFunctions.valueOfBoolean("propName").apply(false);
+      assertEquals(false, joFalse.get("propName").getAsBoolean());
+    }
+    
+    @Test
+    public void testValueOfString() {
+      JsonObject jo = JsonFunctions.valueOfString("propName").apply("str1");
+      assertEquals("str1", jo.get("propName").getAsString());
+    }
+    
+    @Test
+    public void testValueOfCharacter() {
+      JsonObject jo = JsonFunctions.valueOfCharacter("propName").apply('c');
+      assertEquals('c', jo.get("propName").getAsCharacter());
+    }
 }

Reply via email to