Skip to content

Commit 094a74c

Browse files
committed
Nullability annotations, javadoc and test coverage
1 parent f081939 commit 094a74c

18 files changed

+1902
-686
lines changed

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ repositories {
3333
}
3434

3535
dependencies {
36+
implementation 'org.jetbrains:annotations:26.0.2'
3637
implementation 'commons-codec:commons-codec:1.18.0'
3738

3839
testImplementation 'org.junit.jupiter:junit-jupiter:5.7.1'

src/main/java/io/nats/json/ArrayBuilder.java

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,58 @@
1313

1414
package io.nats.json;
1515

16+
import org.jetbrains.annotations.NotNull;
17+
import org.jetbrains.annotations.Nullable;
18+
1619
import java.util.ArrayList;
1720
import java.util.Collection;
1821

22+
/**
23+
* Utility class to build a JsonValue for an array
24+
*/
1925
public class ArrayBuilder implements JsonSerializable {
2026

21-
public final JsonValue jv = new JsonValue(new ArrayList<>());
27+
/**
28+
* The JsonValue backing this ArrayBuilder
29+
*/
30+
@NotNull
31+
public final JsonValue jv;
32+
33+
/**
34+
* Get a new instance of ArrayBuilder
35+
*/
36+
public ArrayBuilder() {
37+
jv = new JsonValue(new ArrayList<>());
38+
}
2239

40+
/**
41+
* Get an instance of ArrayBuilder
42+
* @return an ArrayBuilder instance
43+
*/
44+
@NotNull
2345
public static ArrayBuilder instance() {
2446
return new ArrayBuilder();
2547
}
2648

27-
public ArrayBuilder add(Object o) {
49+
/**
50+
* Add an object to the array. The object is converted to a JsonValue if it isn't one already.
51+
* @param o the object
52+
* @return the builder
53+
*/
54+
@NotNull
55+
public ArrayBuilder add(@Nullable Object o) {
2856
//noinspection DataFlowIssue // NO ISSUE, WE KNOW jv.array is NOT NULL
2957
jv.array.add(JsonValue.instance(o));
3058
return this;
3159
}
3260

33-
public ArrayBuilder addItems(Collection<?> c) {
61+
/**
62+
* Add all items in the collection to the array, unless an item is null;
63+
* @param c the collection
64+
* @return the builder
65+
*/
66+
@NotNull
67+
public ArrayBuilder addItems(@Nullable Collection<?> c) {
3468
if (c != null) {
3569
for (Object o : c) {
3670
//noinspection DataFlowIssue // NO ISSUE, WE KNOW jv.array is NOT NULL
@@ -40,12 +74,20 @@ public ArrayBuilder addItems(Collection<?> c) {
4074
return this;
4175
}
4276

77+
/**
78+
* {@inheritDoc}
79+
*/
4380
@Override
81+
@NotNull
4482
public String toJson() {
4583
return jv.toJson();
4684
}
4785

86+
/**
87+
* {@inheritDoc}
88+
*/
4889
@Override
90+
@NotNull
4991
public JsonValue toJsonValue() {
5092
return jv;
5193
}

src/main/java/io/nats/json/DateTimeUtils.java

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313

1414
package io.nats.json;
1515

16+
import org.jetbrains.annotations.NotNull;
17+
import org.jetbrains.annotations.Nullable;
18+
1619
import java.time.Duration;
1720
import java.time.Instant;
1821
import java.time.ZoneId;
@@ -29,31 +32,36 @@ private DateTimeUtils() {} /* ensures cannot be constructed */
2932
/**
3033
* The ZoneId for GMT
3134
*/
35+
@NotNull
3236
public static final ZoneId ZONE_ID_GMT = ZoneId.of("GMT");
3337

3438
/**
3539
* The ZoneDateTime uses as a default, can be used instead of null
3640
*/
41+
@NotNull
3742
public static final ZonedDateTime DEFAULT_TIME = ZonedDateTime.of(1, 1, 1, 0, 0, 0, 0, ZONE_ID_GMT);
3843

3944
/**
4045
* The formatter to crate RFC 3339 strings from dates.
4146
*/
47+
@NotNull
4248
public static final DateTimeFormatter RFC3339_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.nnnnnnnnn'Z'");
4349

4450
/**
4551
* Convert a ZoneDateTime to a GMT version of the ZoneDateTime
4652
* @param zonedDateTime the input
4753
* @return the output
4854
*/
49-
public static ZonedDateTime toGmt(ZonedDateTime zonedDateTime) {
55+
@NotNull
56+
public static ZonedDateTime toGmt(@NotNull ZonedDateTime zonedDateTime) {
5057
return zonedDateTime.withZoneSameInstant(ZONE_ID_GMT);
5158
}
5259

5360
/**
5461
* Get an instance of ZonedDateTime.now(), but for the GMT timezone
5562
* @return the current date-time using the system clock in GMT
5663
*/
64+
@NotNull
5765
public static ZonedDateTime gmtNow() {
5866
return ZonedDateTime.now().withZoneSameInstant(ZONE_ID_GMT);
5967
}
@@ -64,7 +72,7 @@ public static ZonedDateTime gmtNow() {
6472
* @param zdt2 the second ZonedDateTime
6573
* @return true if they are equal
6674
*/
67-
public static boolean equals(ZonedDateTime zdt1, ZonedDateTime zdt2) {
75+
public static boolean equals(@Nullable ZonedDateTime zdt1, @Nullable ZonedDateTime zdt2) {
6876
if (zdt1 == zdt2) return true;
6977
if (zdt1 == null || zdt2 == null) return false;
7078
return zdt1.withZoneSameInstant(ZONE_ID_GMT).equals(zdt2.withZoneSameInstant(ZONE_ID_GMT));
@@ -75,7 +83,8 @@ public static boolean equals(ZonedDateTime zdt1, ZonedDateTime zdt2) {
7583
* @param zonedDateTime the input
7684
* @return the formatted string
7785
*/
78-
public static String toRfc3339(ZonedDateTime zonedDateTime) {
86+
@NotNull
87+
public static String toRfc3339(@NotNull ZonedDateTime zonedDateTime) {
7988
return RFC3339_FORMATTER.format(toGmt(zonedDateTime));
8089
}
8190

@@ -84,7 +93,8 @@ public static String toRfc3339(ZonedDateTime zonedDateTime) {
8493
* @param dateTime - date time from the server.
8594
* @return a Zoned Date time.
8695
*/
87-
public static ZonedDateTime parseDateTime(String dateTime) {
96+
@NotNull
97+
public static ZonedDateTime parseDateTime(@NotNull String dateTime) {
8898
return parseDateTime(dateTime, DEFAULT_TIME);
8999
}
90100

@@ -94,7 +104,8 @@ public static ZonedDateTime parseDateTime(String dateTime) {
94104
* @param dflt - the default ZoneDateTime to use if the input string exceptions while parsing
95105
* @return a ZonedDateTime.
96106
*/
97-
public static ZonedDateTime parseDateTime(String dateTime, ZonedDateTime dflt) {
107+
@NotNull
108+
public static ZonedDateTime parseDateTime(@NotNull String dateTime, @NotNull ZonedDateTime dflt) {
98109
try {
99110
return toGmt(ZonedDateTime.parse(dateTime));
100111
}
@@ -108,7 +119,8 @@ public static ZonedDateTime parseDateTime(String dateTime, ZonedDateTime dflt) {
108119
* @param dateTime - date time from the server.
109120
* @return a ZonedDateTime.
110121
*/
111-
public static ZonedDateTime parseDateTimeThrowParseError(String dateTime) {
122+
@NotNull
123+
public static ZonedDateTime parseDateTimeThrowParseError(@NotNull String dateTime) {
112124
return toGmt(ZonedDateTime.parse(dateTime));
113125
}
114126

@@ -117,6 +129,7 @@ public static ZonedDateTime parseDateTimeThrowParseError(String dateTime) {
117129
* @param millis the millis from now value
118130
* @return a ZonedDateTime.
119131
*/
132+
@NotNull
120133
public static ZonedDateTime fromNow(long millis) {
121134
return ZonedDateTime.ofInstant(Instant.now().plusMillis(millis), ZONE_ID_GMT);
122135
}
@@ -126,7 +139,8 @@ public static ZonedDateTime fromNow(long millis) {
126139
* @param dur the duration to use the millis for from now
127140
* @return a ZonedDateTime.
128141
*/
129-
public static ZonedDateTime fromNow(Duration dur) {
142+
@NotNull
143+
public static ZonedDateTime fromNow(@NotNull Duration dur) {
130144
return ZonedDateTime.ofInstant(Instant.now().plusMillis(dur.toMillis()), ZONE_ID_GMT);
131145
}
132146
}

0 commit comments

Comments
 (0)