Skip to content

Commit a381e35

Browse files
committed
Remove Guava. Build and tests now run on Java 6-11.
1 parent ce6f3a8 commit a381e35

18 files changed

+235
-145
lines changed

project.gradle

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ targetCompatibility = JavaVersion.VERSION_1_7; // defaults to sourceCompatibilit
3030
*/
3131
dependencies {
3232
implementation(group: "com.fasterxml.jackson.core", name: "jackson-databind", version: "2.9.9");
33-
implementation(group: "com.google.guava", name: "guava", version: "28.1-android");
34-
implementation(group: "com.github.java-json-tools", name: "msg-simple", version: "1.2-SNAPSHOT");
33+
implementation(group: "com.github.fge", name: "msg-simple", version: "1.1");
3534
implementation(group: "com.google.code.findbugs", name: "jsr305", version: "2.0.1");
3635
testImplementation(group: "org.testng", name: "testng", version: "6.8.7") {
3736
exclude(group: "junit", module: "junit");
@@ -51,7 +50,6 @@ javadoc {
5150
links("https://docs.oracle.com/javase/7/docs/api/");
5251
links("https://www.javadoc.io/doc/com.google.code.findbugs/jsr305/3.0.1/");
5352
links("https://fasterxml.github.io/jackson-databind/javadoc/2.2.0/");
54-
links("https://www.javadoc.io/doc/com.google.guava/guava/28.1-android/");
5553
links("https://java-json-tools.github.io/msg-simple/");
5654
}
5755
}

src/main/java/com/github/fge/jackson/JacksonUtils.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,11 @@
2929
import com.fasterxml.jackson.databind.ObjectWriter;
3030
import com.fasterxml.jackson.databind.SerializationFeature;
3131
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
32-
import com.google.common.collect.Maps;
3332

3433
import java.io.IOException;
3534
import java.io.StringWriter;
35+
import java.util.Collections;
36+
import java.util.HashMap;
3637
import java.util.Iterator;
3738
import java.util.Map;
3839

@@ -94,11 +95,11 @@ public static JsonNodeFactory nodeFactory()
9495
*/
9596
public static Map<String, JsonNode> asMap(final JsonNode node)
9697
{
97-
final Map<String, JsonNode> ret = Maps.newHashMap();
9898
if (!node.isObject())
99-
return ret;
99+
return Collections.emptyMap();
100100

101101
final Iterator<Map.Entry<String, JsonNode>> iterator = node.fields();
102+
final Map<String, JsonNode> ret = new HashMap<String, JsonNode>();
102103

103104
Map.Entry<String, JsonNode> entry;
104105

src/main/java/com/github/fge/jackson/JsonLoader.java

Lines changed: 29 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,8 @@
2020
package com.github.fge.jackson;
2121

2222
import com.fasterxml.jackson.databind.JsonNode;
23-
import com.google.common.base.Preconditions;
24-
import com.google.common.io.Closer;
2523

2624
import javax.annotation.Nonnull;
27-
2825
import java.io.File;
2926
import java.io.FileInputStream;
3027
import java.io.IOException;
@@ -74,50 +71,44 @@ private JsonLoader()
7471
public static JsonNode fromResource(@Nonnull final String resource)
7572
throws IOException
7673
{
77-
Preconditions.checkNotNull(resource);
78-
Preconditions.checkArgument(resource.startsWith("/"),
79-
"resource path does not start with a '/'");
74+
if (resource == null) {
75+
throw new NullPointerException();
76+
}
77+
if (!resource.startsWith("/")) {
78+
throw new IllegalArgumentException("resource path does not start with a '/'");
79+
}
8080
URL url;
8181
url = JsonLoader.class.getResource(resource);
8282
if (url == null) {
83-
final ClassLoader classLoader = firstNonNull(
84-
Thread.currentThread().getContextClassLoader(),
85-
JsonLoader.class.getClassLoader()
86-
);
83+
final ClassLoader classLoader;
84+
if (Thread.currentThread().getContextClassLoader() != null) {
85+
classLoader = Thread.currentThread().getContextClassLoader();
86+
} else if (JsonLoader.class.getClassLoader() != null) {
87+
classLoader = JsonLoader.class.getClassLoader();
88+
} else {
89+
throw new NullPointerException();
90+
}
8791
final String s = INITIAL_SLASH.matcher(resource).replaceFirst("");
8892
url = classLoader.getResource(s);
8993
}
9094
if (url == null)
9195
throw new IOException("resource " + resource + " not found");
9296

93-
final Closer closer = Closer.create();
9497
final JsonNode ret;
95-
final InputStream in;
98+
InputStream in = null;
9699

97100
try {
98-
in = closer.register(url.openStream());
101+
in = url.openStream();
99102
ret = READER.fromInputStream(in);
100103
} finally {
101-
closer.close();
104+
if (in != null) {
105+
in.close();
106+
}
102107
}
103108

104109
return ret;
105110
}
106111

107-
/**
108-
* Returns the first non-null parameter.
109-
*
110-
* Implementation note: Avoids the Guava method of the same name, to mitigate 'Dependency Hell'.
111-
* This can be replaced by {@code MoreObjects.firstNonNull} when moving to Guava >= 18.0
112-
* (Tip: Guava 20 seems like a good choice if Java 6 support is still necessary.)
113-
*
114-
* @throws NullPointerException if both are null.
115-
*/
116-
private static ClassLoader firstNonNull(ClassLoader first, ClassLoader second)
117-
{
118-
return first != null ? first : Preconditions.checkNotNull(second);
119-
}
120-
121112
/**
122113
* Read a {@link JsonNode} from an URL.
123114
*
@@ -141,15 +132,16 @@ public static JsonNode fromURL(final URL url)
141132
public static JsonNode fromPath(final String path)
142133
throws IOException
143134
{
144-
final Closer closer = Closer.create();
145135
final JsonNode ret;
146-
final FileInputStream in;
136+
FileInputStream in = null;
147137

148138
try {
149-
in = closer.register(new FileInputStream(path));
139+
in = new FileInputStream(path);
150140
ret = READER.fromInputStream(in);
151141
} finally {
152-
closer.close();
142+
if (in != null) {
143+
in.close();
144+
}
153145
}
154146

155147
return ret;
@@ -166,15 +158,16 @@ public static JsonNode fromPath(final String path)
166158
public static JsonNode fromFile(final File file)
167159
throws IOException
168160
{
169-
final Closer closer = Closer.create();
170161
final JsonNode ret;
171-
final FileInputStream in;
162+
FileInputStream in = null;
172163

173164
try {
174-
in = closer.register(new FileInputStream(file));
165+
in = new FileInputStream(file);
175166
ret = READER.fromInputStream(in);
176167
} finally {
177-
closer.close();
168+
if (in != null) {
169+
in.close();
170+
}
178171
}
179172

180173
return ret;

src/main/java/com/github/fge/jackson/JsonNodeReader.java

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import com.github.fge.Builder;
3131
import com.github.fge.msgsimple.bundle.MessageBundle;
3232
import com.github.fge.msgsimple.bundle.PropertiesBundle;
33-
import com.google.common.io.Closer;
3433

3534
import javax.annotation.Nonnull;
3635
import javax.annotation.concurrent.ThreadSafe;
@@ -93,16 +92,20 @@ public JsonNodeReader()
9392
public JsonNode fromInputStream(final InputStream in)
9493
throws IOException
9594
{
96-
final Closer closer = Closer.create();
97-
final JsonParser parser;
98-
final MappingIterator<JsonNode> iterator;
95+
JsonParser parser = null;
96+
MappingIterator<JsonNode> iterator = null;
9997

10098
try {
101-
parser = closer.register(reader.getFactory().createParser(in));
99+
parser = reader.getFactory().createParser(in);
102100
iterator = reader.readValues(parser);
103-
return readNode(closer.register(iterator));
101+
return readNode(iterator);
104102
} finally {
105-
closer.close();
103+
if (parser != null) {
104+
parser.close();
105+
}
106+
if (iterator != null) {
107+
iterator.close();
108+
}
106109
}
107110
}
108111

@@ -117,16 +120,20 @@ public JsonNode fromInputStream(final InputStream in)
117120
public JsonNode fromReader(final Reader r)
118121
throws IOException
119122
{
120-
final Closer closer = Closer.create();
121-
final JsonParser parser;
122-
final MappingIterator<JsonNode> iterator;
123+
JsonParser parser = null;
124+
MappingIterator<JsonNode> iterator = null;
123125

124126
try {
125-
parser = closer.register(reader.getFactory().createParser(r));
127+
parser = reader.getFactory().createParser(r);
126128
iterator = reader.readValues(parser);
127-
return readNode(closer.register(iterator));
129+
return readNode(iterator);
128130
} finally {
129-
closer.close();
131+
if (parser != null) {
132+
parser.close();
133+
}
134+
if (iterator != null) {
135+
iterator.close();
136+
}
130137
}
131138
}
132139

src/main/java/com/github/fge/jackson/JsonNumEquals.java

Lines changed: 83 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@
2020
package com.github.fge.jackson;
2121

2222
import com.fasterxml.jackson.databind.JsonNode;
23-
import com.google.common.base.Equivalence;
24-
import com.google.common.collect.Sets;
2523

24+
import javax.annotation.Nullable;
25+
import java.util.HashSet;
2626
import java.util.Iterator;
2727
import java.util.Map;
2828
import java.util.Set;
2929

3030
/**
31-
* An {@link Equivalence} strategy for JSON Schema equality
31+
* An {@code com.google.common.base.Equivalence} like strategy for JSON Schema equality
3232
*
3333
* <p>{@link JsonNode} does a pretty good job of obeying the {@link
3434
* Object#equals(Object) equals()}/{@link Object#hashCode() hashCode()}
@@ -41,21 +41,50 @@
4141
* kind of equality.</p>
4242
*/
4343
public final class JsonNumEquals
44-
extends Equivalence<JsonNode>
4544
{
46-
private static final Equivalence<JsonNode> INSTANCE
45+
private static final JsonNumEquals INSTANCE
4746
= new JsonNumEquals();
4847

4948
private JsonNumEquals()
5049
{
5150
}
5251

53-
public static Equivalence<JsonNode> getInstance()
52+
public static JsonNumEquals getInstance()
5453
{
5554
return INSTANCE;
5655
}
5756

58-
@Override
57+
/**
58+
* Returns {@code true} if the given objects are considered equivalent.
59+
*
60+
* <p>The {@code equivalent} method implements an equivalence relation on object references:
61+
*
62+
* <ul>
63+
* <li>It is <i>reflexive</i>: for any reference {@code x}, including null, {@code
64+
* equivalent(x, x)} returns {@code true}.
65+
* <li>It is <i>symmetric</i>: for any references {@code x} and {@code y}, {@code
66+
* equivalent(x, y) == equivalent(y, x)}.
67+
* <li>It is <i>transitive</i>: for any references {@code x}, {@code y}, and {@code z}, if
68+
* {@code equivalent(x, y)} returns {@code true} and {@code equivalent(y, z)} returns {@code
69+
* true}, then {@code equivalent(x, z)} returns {@code true}.
70+
* <li>It is <i>consistent</i>: for any references {@code x} and {@code y}, multiple invocations
71+
* of {@code equivalent(x, y)} consistently return {@code true} or consistently return {@code
72+
* false} (provided that neither {@code x} nor {@code y} is modified).
73+
* </ul>
74+
* @param a x
75+
* @param b y
76+
* @return whether nodes are equal according to IETF RFCs
77+
*/
78+
public final boolean equivalent(@Nullable JsonNode a, @Nullable JsonNode b) {
79+
if (a == b) {
80+
return true;
81+
}
82+
if (a == null || b == null) {
83+
return false;
84+
}
85+
return doEquivalent(a, b);
86+
}
87+
5988
protected boolean doEquivalent(final JsonNode a, final JsonNode b)
6089
{
6190
/*
@@ -93,7 +122,31 @@ protected boolean doEquivalent(final JsonNode a, final JsonNode b)
93122
return typeA == NodeType.ARRAY ? arrayEquals(a, b) : objectEquals(a, b);
94123
}
95124

96-
@Override
125+
/**
126+
* Returns a hash code for {@code t}.
127+
*
128+
* <p>The {@code hash} has the following properties:
129+
* <ul>
130+
* <li>It is <i>consistent</i>: for any reference {@code x}, multiple invocations of
131+
* {@code hash(x}} consistently return the same value provided {@code x} remains unchanged
132+
* according to the definition of the equivalence. The hash need not remain consistent from
133+
* one execution of an application to another execution of the same application.
134+
* <li>It is <i>distributable across equivalence</i>: for any references {@code x} and {@code y},
135+
* if {@code equivalent(x, y)}, then {@code hash(x) == hash(y)}. It is <i>not</i> necessary
136+
* that the hash be distributable across <i>inequivalence</i>. If {@code equivalence(x, y)}
137+
* is false, {@code hash(x) == hash(y)} may still be true.
138+
* <li>{@code hash(null)} is {@code 0}.
139+
* </ul>
140+
* @param t node to hash
141+
* @return hash
142+
*/
143+
public final int hash(@Nullable JsonNode t) {
144+
if (t == null) {
145+
return 0;
146+
}
147+
return doHash(t);
148+
}
149+
97150
protected int doHash(final JsonNode t)
98151
{
99152
/*
@@ -183,13 +236,32 @@ private boolean objectEquals(final JsonNode a, final JsonNode b)
183236
/*
184237
* Grab the key set from the first node
185238
*/
186-
final Set<String> keys = Sets.newHashSet(a.fieldNames());
187-
239+
final Set<String> keys = new HashSet<String>();
240+
Iterator<String> iterator1 = a.fieldNames();
241+
while (iterator1.hasNext()) {
242+
final String next = iterator1.next();
243+
if (next != null) {
244+
keys.add(next);
245+
} else {
246+
throw new NullPointerException();
247+
}
248+
}
249+
// final Set<String> keys = Sets.newHashSet(a.fieldNames());
188250
/*
189251
* Grab the key set from the second node, and see if both sets are the
190252
* same. If not, objects are not equal, no need to check for children.
191253
*/
192-
final Set<String> set = Sets.newHashSet(b.fieldNames());
254+
final Set<String> set = new HashSet<String>();
255+
Iterator<String> iterator2 = b.fieldNames();
256+
while (iterator2.hasNext()) {
257+
final String next = iterator2.next();
258+
if (next != null) {
259+
set.add(next);
260+
} else {
261+
throw new NullPointerException();
262+
}
263+
}
264+
193265
if (!set.equals(keys))
194266
return false;
195267

0 commit comments

Comments
 (0)