Skip to content

Commit

Permalink
Added new @nonnull annotations for safety
Browse files Browse the repository at this point in the history
  • Loading branch information
whitfin committed Apr 27, 2017
1 parent cdce263 commit eda40f4
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 60 deletions.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
</distributionManagement>

<properties>
<findbugs.version>3.0.2</findbugs.version>
<jackson.version>2.8.7</jackson.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
Expand All @@ -65,6 +66,11 @@
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
<version>${findbugs.version}</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
Expand Down
74 changes: 28 additions & 46 deletions src/main/java/com/zackehh/jackson/Jive.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import com.fasterxml.jackson.databind.node.*;
import com.zackehh.jackson.scope.SafeExecution;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.BigInteger;
Expand All @@ -16,6 +18,7 @@

import static com.zackehh.jackson.stream.JiveCollectors.toArrayNode;
import static com.zackehh.jackson.stream.JiveCollectors.toObjectNode;
import static java.util.Objects.requireNonNull;

/**
* The Jive class provides various utilities for working with Jackson
Expand Down Expand Up @@ -305,8 +308,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, BigDecimal value) {
return newJsonEntry(key, newJsonNode(value));
public static Map.Entry<String, JsonNode> newJsonEntry(String key, @Nonnull BigDecimal value) {
return newJsonEntry(key, newJsonNode(requireNonNull(value)));
}

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

/**
Expand All @@ -327,7 +330,7 @@ public static Map.Entry<String, JsonNode> newJsonEntry(String key, BigInteger va
* @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 @@ -349,7 +352,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 @@ -360,7 +363,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 @@ -371,7 +374,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, Integer value) {
public static Map.Entry<String, JsonNode> newJsonEntry(String key, int value) {
return newJsonEntry(key, newJsonNode(value));
}

Expand All @@ -382,18 +385,7 @@ public static Map.Entry<String, JsonNode> newJsonEntry(String key, Integer 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) {
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) {
public static Map.Entry<String, JsonNode> newJsonEntry(String key, long value) {
return newJsonEntry(key, newJsonNode(value));
}

Expand All @@ -404,7 +396,7 @@ public static Map.Entry<String, JsonNode> newJsonEntry(String key, Object 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 @@ -415,8 +407,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, String value) {
return newJsonEntry(key, newJsonNode(value));
public static Map.Entry<String, JsonNode> newJsonEntry(String key, @Nonnull String value) {
return newJsonEntry(key, newJsonNode(requireNonNull(value)));
}

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

/**
Expand All @@ -435,8 +427,8 @@ public static DecimalNode newJsonNode(BigDecimal value){
* @param value the BigInteger value of this entry.
* @return a new JsonNode instance.
*/
public static BigIntegerNode newJsonNode(BigInteger value){
return BigIntegerNode.valueOf(value);
public static BigIntegerNode newJsonNode(@Nonnull BigInteger value) {
return BigIntegerNode.valueOf(requireNonNull(value));
}

/**
Expand All @@ -445,7 +437,7 @@ public static BigIntegerNode newJsonNode(BigInteger value){
* @param value the Boolean value of this entry.
* @return a new JsonNode instance.
*/
public static BooleanNode newJsonNode(Boolean value){
public static BooleanNode newJsonNode(boolean value) {
return BooleanNode.valueOf(value);
}

Expand All @@ -455,7 +447,7 @@ public static BooleanNode newJsonNode(Boolean value){
* @param value the byte[] value of this entry.
* @return a new JsonNode instance.
*/
public static BinaryNode newJsonNode(byte[] value){
public static BinaryNode newJsonNode(byte[] value) {
return BinaryNode.valueOf(value);
}

Expand All @@ -465,7 +457,7 @@ public static BinaryNode newJsonNode(byte[] value){
* @param value the Double value of this entry.
* @return a new JsonNode instance.
*/
public static DoubleNode newJsonNode(Double value){
public static DoubleNode newJsonNode(double value) {
return DoubleNode.valueOf(value);
}

Expand All @@ -475,7 +467,7 @@ public static DoubleNode newJsonNode(Double value){
* @param value the Float value of this entry.
* @return a new JsonNode instance.
*/
public static FloatNode newJsonNode(Float value){
public static FloatNode newJsonNode(float value) {
return FloatNode.valueOf(value);
}

Expand All @@ -485,7 +477,7 @@ public static FloatNode newJsonNode(Float value){
* @param value the Integer value of this entry.
* @return a new JsonNode instance.
*/
public static IntNode newJsonNode(Integer value){
public static IntNode newJsonNode(int value) {
return IntNode.valueOf(value);
}

Expand All @@ -495,27 +487,17 @@ public static IntNode newJsonNode(Integer value){
* @param value the Long value of this entry.
* @return a new JsonNode instance.
*/
public static LongNode newJsonNode(Long value){
public static LongNode newJsonNode(long value) {
return LongNode.valueOf(value);
}

/**
* Constructs a new JsonNode from an Object value.
*
* @param value the Object value of this entry.
* @return a new JsonNode instance.
*/
public static POJONode newJsonNode(Object value) {
return new POJONode(value);
}

/**
* Constructs a new JsonNode from a Short value.
*
* @param value the Short value of this entry.
* @return a new JsonNode instance.
*/
public static ShortNode newJsonNode(Short value){
public static ShortNode newJsonNode(short value) {
return ShortNode.valueOf(value);
}

Expand All @@ -525,8 +507,8 @@ public static ShortNode newJsonNode(Short value){
* @param value the String value of this entry.
* @return a new JsonNode instance.
*/
public static TextNode newJsonNode(String value){
return TextNode.valueOf(value);
public static TextNode newJsonNode(@Nonnull String value) {
return TextNode.valueOf(requireNonNull(value));
}

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

JsonNode node1 = Jive.newJsonNode(val1);
JsonNode node2 = Jive.newJsonNode(val2);
Expand All @@ -351,7 +350,6 @@ 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 @@ -362,9 +360,8 @@ public void testNewJsonNode() {
Assert.assertEquals(node7, FloatNode.valueOf(val7));
Assert.assertEquals(node8, IntNode.valueOf(val8));
Assert.assertEquals(node9, LongNode.valueOf(val9));
Assert.assertEquals(node10, new POJONode(node10));
Assert.assertEquals(node11, ShortNode.valueOf(val11));
Assert.assertEquals(node12, TextNode.valueOf(val12));
Assert.assertEquals(node10, ShortNode.valueOf(val10));
Assert.assertEquals(node11, TextNode.valueOf(val11));
}

@Test
Expand All @@ -378,9 +375,8 @@ public void testNewJsonEntry() {
Float val7 = 1.1f;
Integer val8 = 1;
Long val9 = 1L;
HashMap val10 = new HashMap();
Short val11 = Short.valueOf("1");
String val12 = "hello";
Short val10 = Short.valueOf("1");
String val11 = "hello";

Map.Entry<String, JsonNode> node1 = Jive.newJsonEntry("key", val1);
Map.Entry<String, JsonNode> node2 = Jive.newJsonEntry("key", val2);
Expand All @@ -393,7 +389,6 @@ 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 @@ -404,9 +399,8 @@ 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", new POJONode(val10)));
Assert.assertEquals(node11, new AbstractMap.SimpleEntry<>("key", ShortNode.valueOf(val11)));
Assert.assertEquals(node12, new AbstractMap.SimpleEntry<>("key", TextNode.valueOf(val12)));
Assert.assertEquals(node10, new AbstractMap.SimpleEntry<>("key", ShortNode.valueOf(val10)));
Assert.assertEquals(node11, new AbstractMap.SimpleEntry<>("key", TextNode.valueOf(val11)));
}

@Test
Expand Down

0 comments on commit eda40f4

Please sign in to comment.