Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove Guava. #42

Merged
merged 1 commit into from
Jan 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions project.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
* Project-specific settings. Unfortunately we cannot put the name in there!
*/
group = "com.github.java-json-tools";
version = "1.12-SNAPSHOT";
version = "2.0-SNAPSHOT";
sourceCompatibility = JavaVersion.VERSION_1_7;
targetCompatibility = JavaVersion.VERSION_1_7; // defaults to sourceCompatibility

Expand All @@ -30,7 +30,6 @@ targetCompatibility = JavaVersion.VERSION_1_7; // defaults to sourceCompatibilit
*/
dependencies {
implementation(group: "com.fasterxml.jackson.core", name: "jackson-databind", version: "2.9.9");
implementation(group: "com.google.guava", name: "guava", version: "28.1-android");
implementation(group: "com.github.java-json-tools", name: "msg-simple", version: "1.2");
implementation(group: "com.google.code.findbugs", name: "jsr305", version: "2.0.1");
testImplementation(group: "org.testng", name: "testng", version: "6.8.7") {
Expand All @@ -51,7 +50,6 @@ javadoc {
links("https://docs.oracle.com/javase/7/docs/api/");
links("https://www.javadoc.io/doc/com.google.code.findbugs/jsr305/3.0.1/");
links("https://fasterxml.github.io/jackson-databind/javadoc/2.9/");
links("https://www.javadoc.io/doc/com.google.guava/guava/28.1-android/");
links("https://java-json-tools.github.io/msg-simple/");
}
}
9 changes: 5 additions & 4 deletions src/main/java/com/github/fge/jackson/JacksonUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@
import com.fasterxml.jackson.databind.ObjectWriter;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.google.common.collect.Maps;

import java.io.IOException;
import java.io.StringWriter;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

Expand Down Expand Up @@ -94,11 +95,11 @@ public static JsonNodeFactory nodeFactory()
*/
public static Map<String, JsonNode> asMap(final JsonNode node)
{
final Map<String, JsonNode> ret = Maps.newHashMap();
if (!node.isObject())
return ret;
return Collections.emptyMap();

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

Map.Entry<String, JsonNode> entry;

Expand All @@ -107,7 +108,7 @@ public static Map<String, JsonNode> asMap(final JsonNode node)
ret.put(entry.getKey(), entry.getValue());
}

return ret;
return Collections.unmodifiableMap(ret);
}

/**
Expand Down
65 changes: 29 additions & 36 deletions src/main/java/com/github/fge/jackson/JsonLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,8 @@
package com.github.fge.jackson;

import com.fasterxml.jackson.databind.JsonNode;
import com.google.common.base.Preconditions;
import com.google.common.io.Closer;

import javax.annotation.Nonnull;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
Expand Down Expand Up @@ -74,50 +71,44 @@ private JsonLoader()
public static JsonNode fromResource(@Nonnull final String resource)
throws IOException
{
Preconditions.checkNotNull(resource);
Preconditions.checkArgument(resource.startsWith("/"),
"resource path does not start with a '/'");
if (resource == null) {
throw new NullPointerException();
}
if (!resource.startsWith("/")) {
throw new IllegalArgumentException("resource path does not start with a '/'");
}
URL url;
url = JsonLoader.class.getResource(resource);
if (url == null) {
final ClassLoader classLoader = firstNonNull(
Thread.currentThread().getContextClassLoader(),
JsonLoader.class.getClassLoader()
);
final ClassLoader classLoader;
if (Thread.currentThread().getContextClassLoader() != null) {
classLoader = Thread.currentThread().getContextClassLoader();
} else if (JsonLoader.class.getClassLoader() != null) {
classLoader = JsonLoader.class.getClassLoader();
} else {
throw new NullPointerException();
}
final String s = INITIAL_SLASH.matcher(resource).replaceFirst("");
url = classLoader.getResource(s);
}
if (url == null)
throw new IOException("resource " + resource + " not found");

final Closer closer = Closer.create();
final JsonNode ret;
final InputStream in;
InputStream in = null;

try {
in = closer.register(url.openStream());
in = url.openStream();
ret = READER.fromInputStream(in);
} finally {
closer.close();
if (in != null) {
in.close();
}
}

return ret;
}

/**
* Returns the first non-null parameter.
*
* Implementation note: Avoids the Guava method of the same name, to mitigate 'Dependency Hell'.
* This can be replaced by {@code MoreObjects.firstNonNull} when moving to Guava >= 18.0
* (Tip: Guava 20 seems like a good choice if Java 6 support is still necessary.)
*
* @throws NullPointerException if both are null.
*/
private static ClassLoader firstNonNull(ClassLoader first, ClassLoader second)
{
return first != null ? first : Preconditions.checkNotNull(second);
}

/**
* Read a {@link JsonNode} from an URL.
*
Expand All @@ -141,15 +132,16 @@ public static JsonNode fromURL(final URL url)
public static JsonNode fromPath(final String path)
throws IOException
{
final Closer closer = Closer.create();
final JsonNode ret;
final FileInputStream in;
FileInputStream in = null;

try {
in = closer.register(new FileInputStream(path));
in = new FileInputStream(path);
ret = READER.fromInputStream(in);
} finally {
closer.close();
if (in != null) {
in.close();
}
}

return ret;
Expand All @@ -166,15 +158,16 @@ public static JsonNode fromPath(final String path)
public static JsonNode fromFile(final File file)
throws IOException
{
final Closer closer = Closer.create();
final JsonNode ret;
final FileInputStream in;
FileInputStream in = null;

try {
in = closer.register(new FileInputStream(file));
in = new FileInputStream(file);
ret = READER.fromInputStream(in);
} finally {
closer.close();
if (in != null) {
in.close();
}
}

return ret;
Expand Down
33 changes: 20 additions & 13 deletions src/main/java/com/github/fge/jackson/JsonNodeReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import com.github.fge.Builder;
import com.github.fge.msgsimple.bundle.MessageBundle;
import com.github.fge.msgsimple.bundle.PropertiesBundle;
import com.google.common.io.Closer;

import javax.annotation.Nonnull;
import javax.annotation.concurrent.ThreadSafe;
Expand Down Expand Up @@ -93,16 +92,20 @@ public JsonNodeReader()
public JsonNode fromInputStream(final InputStream in)
throws IOException
{
final Closer closer = Closer.create();
final JsonParser parser;
final MappingIterator<JsonNode> iterator;
JsonParser parser = null;
MappingIterator<JsonNode> iterator = null;

try {
parser = closer.register(reader.getFactory().createParser(in));
parser = reader.getFactory().createParser(in);
iterator = reader.readValues(parser);
return readNode(closer.register(iterator));
return readNode(iterator);
} finally {
closer.close();
if (parser != null) {
parser.close();
}
if (iterator != null) {
iterator.close();
}
}
}

Expand All @@ -117,16 +120,20 @@ public JsonNode fromInputStream(final InputStream in)
public JsonNode fromReader(final Reader r)
throws IOException
{
final Closer closer = Closer.create();
final JsonParser parser;
final MappingIterator<JsonNode> iterator;
JsonParser parser = null;
MappingIterator<JsonNode> iterator = null;

try {
parser = closer.register(reader.getFactory().createParser(r));
parser = reader.getFactory().createParser(r);
iterator = reader.readValues(parser);
return readNode(closer.register(iterator));
return readNode(iterator);
} finally {
closer.close();
if (parser != null) {
parser.close();
}
if (iterator != null) {
iterator.close();
}
}
}

Expand Down
55 changes: 44 additions & 11 deletions src/main/java/com/github/fge/jackson/JsonNumEquals.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,14 @@
package com.github.fge.jackson;

import com.fasterxml.jackson.databind.JsonNode;
import com.google.common.base.Equivalence;
import com.google.common.collect.Sets;

import javax.annotation.Nullable;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
* An {@link Equivalence} strategy for JSON Schema equality
* An {@code com.google.common.base.Equivalence} like strategy for JSON Schema equality
*
* <p>{@link JsonNode} does a pretty good job of obeying the {@link
* Object#equals(Object) equals()}/{@link Object#hashCode() hashCode()}
Expand All @@ -41,21 +40,30 @@
* kind of equality.</p>
*/
public final class JsonNumEquals
extends Equivalence<JsonNode>
{
private static final Equivalence<JsonNode> INSTANCE
private static final JsonNumEquals INSTANCE
= new JsonNumEquals();

private JsonNumEquals()
{
}

public static Equivalence<JsonNode> getInstance()
public static JsonNumEquals getInstance()
{
return INSTANCE;
}

@Override
@SuppressWarnings("ReferenceEquality")
public final boolean equivalent(@Nullable JsonNode a, @Nullable JsonNode b) {
if (a == b) {
return true;
}
if (a == null || b == null) {
return false;
}
return doEquivalent(a, b);
}

protected boolean doEquivalent(final JsonNode a, final JsonNode b)
{
/*
Expand Down Expand Up @@ -93,7 +101,13 @@ protected boolean doEquivalent(final JsonNode a, final JsonNode b)
return typeA == NodeType.ARRAY ? arrayEquals(a, b) : objectEquals(a, b);
}

@Override
public final int hash(@Nullable JsonNode t) {
if (t == null) {
return 0;
}
return doHash(t);
}

protected int doHash(final JsonNode t)
{
/*
Expand Down Expand Up @@ -183,13 +197,32 @@ private boolean objectEquals(final JsonNode a, final JsonNode b)
/*
* Grab the key set from the first node
*/
final Set<String> keys = Sets.newHashSet(a.fieldNames());
final Set<String> keys = new HashSet<>();
Iterator<String> iterator1 = a.fieldNames();
while (iterator1.hasNext()) {
final String next = iterator1.next();
if (next != null) {
keys.add(next);
} else {
throw new NullPointerException();
}
}

/*
* Grab the key set from the second node, and see if both sets are the
* same. If not, objects are not equal, no need to check for children.
*/
final Set<String> set = Sets.newHashSet(b.fieldNames());
final Set<String> set = new HashSet<>();
Iterator<String> iterator2 = b.fieldNames();
while (iterator2.hasNext()) {
final String next = iterator2.next();
if (next != null) {
set.add(next);
} else {
throw new NullPointerException();
}
}

if (!set.equals(keys))
return false;

Expand Down
Loading