Skip to content

Commit

Permalink
Fix up signatures and add missing coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
whitfin committed Apr 28, 2017
1 parent d7479d4 commit 3e94d59
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 23 deletions.
60 changes: 46 additions & 14 deletions src/main/java/com/zackehh/jackson/Jive.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.util.Optional;
import java.util.Set;
import java.util.function.BiFunction;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -439,8 +440,8 @@ public static Map.Entry<String, JsonNode> newJsonEntry(String key, JsonNode valu
* @param value the BigDecimal value of this entry.
* @return a new Map.Entry instance.
*/
public static Map.Entry<String, JsonNode> newJsonEntry(String key, @Nonnull BigDecimal value) {
return newJsonEntry(key, newJsonNode(requireNonNull(value)));
public static Map.Entry<String, JsonNode> newJsonEntry(String key, BigDecimal value) {
return newJsonEntry(key, newJsonNode(value));
}

/**
Expand All @@ -450,8 +451,8 @@ public static Map.Entry<String, JsonNode> newJsonEntry(String key, @Nonnull BigD
* @param value the BigInteger value of this entry.
* @return a new Map.Entry instance.
*/
public static Map.Entry<String, JsonNode> newJsonEntry(String key, @Nonnull BigInteger value) {
return newJsonEntry(key, newJsonNode(requireNonNull(value)));
public static Map.Entry<String, JsonNode> newJsonEntry(String key, BigInteger value) {
return newJsonEntry(key, newJsonNode(value));
}

/**
Expand All @@ -461,7 +462,7 @@ public static Map.Entry<String, JsonNode> newJsonEntry(String key, @Nonnull BigI
* @param value the Boolean value of this entry.
* @return a new Map.Entry instance.
*/
public static Map.Entry<String, JsonNode> newJsonEntry(String key, boolean value) {
public static Map.Entry<String, JsonNode> newJsonEntry(String key, Boolean value) {
return newJsonEntry(key, newJsonNode(value));
}

Expand All @@ -483,7 +484,7 @@ public static Map.Entry<String, JsonNode> newJsonEntry(String key, byte[] value)
* @param value the Double value of this entry.
* @return a new Map.Entry instance.
*/
public static Map.Entry<String, JsonNode> newJsonEntry(String key, double value) {
public static Map.Entry<String, JsonNode> newJsonEntry(String key, Double value) {
return newJsonEntry(key, newJsonNode(value));
}

Expand All @@ -494,7 +495,7 @@ public static Map.Entry<String, JsonNode> newJsonEntry(String key, double value)
* @param value the Float value of this entry.
* @return a new Map.Entry instance.
*/
public static Map.Entry<String, JsonNode> newJsonEntry(String key, float value) {
public static Map.Entry<String, JsonNode> newJsonEntry(String key, Float value) {
return newJsonEntry(key, newJsonNode(value));
}

Expand All @@ -505,7 +506,7 @@ public static Map.Entry<String, JsonNode> newJsonEntry(String key, float value)
* @param value the Integer value of this entry.
* @return a new Map.Entry instance.
*/
public static Map.Entry<String, JsonNode> newJsonEntry(String key, int value) {
public static Map.Entry<String, JsonNode> newJsonEntry(String key, Integer value) {
return newJsonEntry(key, newJsonNode(value));
}

Expand All @@ -516,7 +517,18 @@ public static Map.Entry<String, JsonNode> newJsonEntry(String key, int value) {
* @param value the Long value of this entry.
* @return a new Map.Entry instance.
*/
public static Map.Entry<String, JsonNode> newJsonEntry(String key, long value) {
public static Map.Entry<String, JsonNode> newJsonEntry(String key, Long value) {
return newJsonEntry(key, newJsonNode(value));
}

/**
* Constructs a new JSON Map.Entry from an Object value.
*
* @param key the key of this entry as a String.
* @param value the Object value of this entry.
* @return a new Map.Entry instance.
*/
public static Map.Entry<String, JsonNode> newJsonEntry(String key, Object value) {
return newJsonEntry(key, newJsonNode(value));
}

Expand All @@ -527,7 +539,7 @@ public static Map.Entry<String, JsonNode> newJsonEntry(String key, long value) {
* @param value the Short value of this entry.
* @return a new Map.Entry instance.
*/
public static Map.Entry<String, JsonNode> newJsonEntry(String key, short value) {
public static Map.Entry<String, JsonNode> newJsonEntry(String key, Short value) {
return newJsonEntry(key, newJsonNode(value));
}

Expand All @@ -538,8 +550,8 @@ public static Map.Entry<String, JsonNode> newJsonEntry(String key, short value)
* @param value the String value of this entry.
* @return a new Map.Entry instance.
*/
public static Map.Entry<String, JsonNode> newJsonEntry(String key, @Nonnull String value) {
return newJsonEntry(key, newJsonNode(requireNonNull(value)));
public static Map.Entry<String, JsonNode> newJsonEntry(String key, String value) {
return newJsonEntry(key, newJsonNode(value));
}

/**
Expand Down Expand Up @@ -758,7 +770,7 @@ public static ObjectNode pick(ObjectNode node, Collection<String> keys) {
* @return a new instance of type T.
*/
public static <T> T reduce(ArrayNode node, T initial, BiFunction<T, JsonNode, T> function) {
return stream(node).reduce(initial, function, (l, r) -> r);
return stream(node).reduce(initial, function, new RightOperator<>());
}

/**
Expand All @@ -772,7 +784,7 @@ public static <T> T reduce(ArrayNode node, T initial, BiFunction<T, JsonNode, T>
* @return a new instance of type T.
*/
public static <T> T reduce(ObjectNode node, T initial, BiFunction<T, Map.Entry<String, JsonNode>, T> function) {
return stream(node).reduce(initial, function, (l, r) -> r);
return stream(node).reduce(initial, function, new RightOperator<>());
}

/**
Expand Down Expand Up @@ -962,4 +974,24 @@ private static <T> JsonNode safeNode(T value, Function<T, JsonNode> mapper) {
return value == null ? NullNode.getInstance() : mapper.apply(value);
}

/**
* Internal BinaryOperator implementation for reduction methods.
*
* @param <T> the type of the values.
*/
static class RightOperator<T> implements BinaryOperator<T> {

/**
* Overwrites the left value with the right value.
*
* @param left the left value (ignored).
* @param right the right value (returned).
* @return the value on the right side.
*/
@Override
public T apply(T left, T right) {
return right;
}
}

}
36 changes: 27 additions & 9 deletions src/test/java/com/zackehh/jackson/JiveTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -336,8 +336,9 @@ public void testNewJsonNode() {
Float val7 = 1.1f;
Integer val8 = 1;
Long val9 = 1L;
Short val10 = Short.valueOf("1");
String val11 = "hello";
Object val10 = new HashMap<>();
Short val11 = Short.valueOf("1");
String val12 = "hello";

JsonNode node1 = Jive.newJsonNode(val1);
JsonNode node2 = Jive.newJsonNode(val2);
Expand All @@ -350,6 +351,7 @@ public void testNewJsonNode() {
JsonNode node9 = Jive.newJsonNode(val9);
JsonNode node10 = Jive.newJsonNode(val10);
JsonNode node11 = Jive.newJsonNode(val11);
JsonNode node12 = Jive.newJsonNode(val12);

Assert.assertEquals(node1, DecimalNode.valueOf(val1));
Assert.assertEquals(node2, BigIntegerNode.valueOf(val2));
Expand All @@ -360,8 +362,9 @@ public void testNewJsonNode() {
Assert.assertEquals(node7, FloatNode.valueOf(val7));
Assert.assertEquals(node8, IntNode.valueOf(val8));
Assert.assertEquals(node9, LongNode.valueOf(val9));
Assert.assertEquals(node10, ShortNode.valueOf(val10));
Assert.assertEquals(node11, TextNode.valueOf(val11));
Assert.assertEquals(node10, new POJONode(val10));
Assert.assertEquals(node11, ShortNode.valueOf(val11));
Assert.assertEquals(node12, TextNode.valueOf(val12));
}

@Test
Expand All @@ -375,7 +378,7 @@ public void testNewJsonNodeWithNulls() {
JsonNode node7 = Jive.newJsonNode((Integer) null);
JsonNode node8 = Jive.newJsonNode((Object) null);
JsonNode node9 = Jive.newJsonNode((Long) null);
JsonNode node10 = Jive.newJsonNode((Short) null);
JsonNode node10 = Jive.newJsonNode((Short) null);
JsonNode node11 = Jive.newJsonNode((String) null);

Assert.assertTrue(node1.isNull());
Expand Down Expand Up @@ -403,8 +406,9 @@ public void testNewJsonEntry() {
Float val7 = 1.1f;
Integer val8 = 1;
Long val9 = 1L;
Short val10 = Short.valueOf("1");
String val11 = "hello";
Object val10 = new HashMap<>();
Short val11 = Short.valueOf("1");
String val12 = "hello";

Map.Entry<String, JsonNode> node1 = Jive.newJsonEntry("key", val1);
Map.Entry<String, JsonNode> node2 = Jive.newJsonEntry("key", val2);
Expand All @@ -417,6 +421,7 @@ public void testNewJsonEntry() {
Map.Entry<String, JsonNode> node9 = Jive.newJsonEntry("key", val9);
Map.Entry<String, JsonNode> node10 = Jive.newJsonEntry("key", val10);
Map.Entry<String, JsonNode> node11 = Jive.newJsonEntry("key", val11);
Map.Entry<String, JsonNode> node12 = Jive.newJsonEntry("key", val12);

Assert.assertEquals(node1, new AbstractMap.SimpleEntry<>("key", DecimalNode.valueOf(val1)));
Assert.assertEquals(node2, new AbstractMap.SimpleEntry<>("key", BigIntegerNode.valueOf(val2)));
Expand All @@ -427,8 +432,9 @@ public void testNewJsonEntry() {
Assert.assertEquals(node7, new AbstractMap.SimpleEntry<>("key", FloatNode.valueOf(val7)));
Assert.assertEquals(node8, new AbstractMap.SimpleEntry<>("key", IntNode.valueOf(val8)));
Assert.assertEquals(node9, new AbstractMap.SimpleEntry<>("key", LongNode.valueOf(val9)));
Assert.assertEquals(node10, new AbstractMap.SimpleEntry<>("key", ShortNode.valueOf(val10)));
Assert.assertEquals(node11, new AbstractMap.SimpleEntry<>("key", TextNode.valueOf(val11)));
Assert.assertEquals(node10, new AbstractMap.SimpleEntry<>("key", new POJONode(val10)));
Assert.assertEquals(node11, new AbstractMap.SimpleEntry<>("key", ShortNode.valueOf(val11)));
Assert.assertEquals(node12, new AbstractMap.SimpleEntry<>("key", TextNode.valueOf(val12)));
}

@Test
Expand Down Expand Up @@ -563,6 +569,18 @@ public void testRejectObjectNode() {
Assert.assertEquals(obj2, obj3);
}

@Test
public void testRightOperator() {
Jive.RightOperator<JsonNode> operator = new Jive.RightOperator<>();

JsonNode left = Jive.newTextNode("test");
JsonNode right = Jive.newIntNode(5);

JsonNode result = operator.apply(left, right);

Assert.assertEquals(result, right);
}

@Test
public void testSomeArrayNode() {
ArrayNode arr1 = arrayNode()
Expand Down

0 comments on commit 3e94d59

Please sign in to comment.