Skip to content

Removed Guava (left in tests) #66

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

Merged
merged 1 commit into from
Jan 7, 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: 2 additions & 2 deletions project.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,10 @@ project.ext.description = "JSON Patch (RFC 6902) and JSON Merge Patch (RFC 7386)
*/
dependencies {
provided(group: "com.google.code.findbugs", name: "jsr305", version: "3.0.2");
compile(group: "com.google.guava", name: "guava", version: "28.1-android");
compile(group: "com.fasterxml.jackson.core", name: "jackson-databind", version: "2.9.9");
compile(group: "com.github.java-json-tools", name: "msg-simple", version: "1.2");

compile(group: "com.github.java-json-tools", name: "jackson-coreutils", version: "1.11");
compile(group: "com.github.java-json-tools", name: "jackson-coreutils", version: "2.0-SNAPSHOT");
testCompile(group: "org.testng", name: "testng", version: "6.8.7") {
exclude(group: "junit", module: "junit");
exclude(group: "org.beanshell", module: "bsh");
Expand All @@ -44,6 +43,7 @@ dependencies {
testCompile(group: "org.mockito", name: "mockito-core", version: "2.28.2");
// FIXME: update to 3.x once we're off of Java 7
testCompile(group: "org.assertj", name: "assertj-core", version: "2.9.1");
testCompile(group: "com.google.guava", name: "guava", version: "28.1-android");
}

javadoc.options {
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/com/github/fge/jsonpatch/AddOperation.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
import com.github.fge.jackson.jsonpointer.JsonPointer;
import com.github.fge.jackson.jsonpointer.ReferenceToken;
import com.github.fge.jackson.jsonpointer.TokenResolver;
import com.google.common.collect.Iterables;

import java.util.NoSuchElementException;


/**
Expand Down Expand Up @@ -105,6 +106,7 @@ private JsonNode addToArray(final JsonPointer path, final JsonNode node)
{
final JsonNode ret = node.deepCopy();
final ArrayNode target = (ArrayNode) path.parent().get(ret);

final TokenResolver<JsonNode> token = Iterables.getLast(path);

if (token.getToken().equals(LAST_ARRAY_ELEMENT)) {
Expand All @@ -131,9 +133,10 @@ private JsonNode addToArray(final JsonPointer path, final JsonNode node)

private JsonNode addToObject(final JsonPointer path, final JsonNode node)
{
final TokenResolver<JsonNode> token = Iterables.getLast(path);
final JsonNode ret = node.deepCopy();
final ObjectNode target = (ObjectNode) path.parent().get(ret);
target.set(Iterables.getLast(path).getToken().getRaw(), value);
target.set(token.getToken().getRaw(), value);
return ret;
}
}
30 changes: 30 additions & 0 deletions src/main/java/com/github/fge/jsonpatch/Iterables.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.github.fge.jsonpatch;

import java.util.Iterator;
import java.util.NoSuchElementException;

/**
* @author {@literal @}soberich on 30-Nov-18
*/
public final class Iterables {

private Iterables() {}

/**
* Returns the last element of {@code iterable}.
*
* @param <T> underlying type being iterated
* @param iterable type of iterable
* @return the last element of {@code iterable}
* @throws NoSuchElementException if the iterable is empty
*/
public static <T> T getLast(Iterable<T> iterable) {
Iterator<T> iterator = iterable.iterator();
while (true) {
T current = iterator.next();
if (!iterator.hasNext()) {
return current;
}
}
}
}
5 changes: 3 additions & 2 deletions src/main/java/com/github/fge/jsonpatch/JsonPatch.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@
import com.github.fge.jackson.JacksonUtils;
import com.github.fge.msgsimple.bundle.MessageBundle;
import com.github.fge.msgsimple.load.MessageBundles;
import com.google.common.collect.ImmutableList;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
Expand Down Expand Up @@ -111,7 +112,7 @@ public final class JsonPatch
@JsonCreator
public JsonPatch(final List<JsonPatchOperation> operations)
{
this.operations = ImmutableList.copyOf(operations);
this.operations = Collections.unmodifiableList(new ArrayList<JsonPatchOperation>(operations));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import com.fasterxml.jackson.databind.node.MissingNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.github.fge.jackson.jsonpointer.JsonPointer;
import com.google.common.collect.Iterables;

import java.io.IOException;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.github.fge.jackson.jsonpointer.JsonPointer;
import com.google.common.collect.Iterables;

/**
* JSON Patch {@code replace} operation
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/com/github/fge/jsonpatch/TestOperation.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import com.fasterxml.jackson.databind.JsonNode;
import com.github.fge.jackson.JsonNumEquals;
import com.github.fge.jackson.jsonpointer.JsonPointer;
import com.google.common.base.Equivalence;

/**
* JSON Patch {@code test} operation
Expand All @@ -42,7 +41,7 @@
public final class TestOperation
extends PathValueOperation
{
private static final Equivalence<JsonNode> EQUIVALENCE
private static final JsonNumEquals EQUIVALENCE
= JsonNumEquals.getInstance();

@JsonCreator
Expand Down
22 changes: 7 additions & 15 deletions src/main/java/com/github/fge/jsonpatch/diff/DiffProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,28 +24,23 @@
import com.github.fge.jackson.jsonpointer.JsonPointer;
import com.github.fge.jsonpatch.JsonPatch;
import com.github.fge.jsonpatch.JsonPatchOperation;
import com.google.common.base.Equivalence;
import com.google.common.base.Predicate;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;

import javax.annotation.Nullable;
import java.util.List;
import java.util.Map;
import java.util.*;

// TODO: cleanup
final class DiffProcessor
{
private static final Equivalence<JsonNode> EQUIVALENCE
private static final JsonNumEquals EQUIVALENCE
= JsonNumEquals.getInstance();

private final Map<JsonPointer, JsonNode> unchanged;

private final List<DiffOperation> diffs = Lists.newArrayList();
private final List<DiffOperation> diffs = new ArrayList<DiffOperation>();

DiffProcessor(final Map<JsonPointer, JsonNode> unchanged)
{
this.unchanged = ImmutableMap.copyOf(unchanged);
this.unchanged = Collections.unmodifiableMap(new HashMap<JsonPointer, JsonNode>(unchanged));
}

void valueReplaced(final JsonPointer pointer, final JsonNode oldValue,
Expand Down Expand Up @@ -79,7 +74,7 @@ void valueAdded(final JsonPointer pointer, final JsonNode value)

JsonPatch getPatch()
{
final List<JsonPatchOperation> list = Lists.newArrayList();
final List<JsonPatchOperation> list = new ArrayList<JsonPatchOperation>();

for (final DiffOperation op: diffs)
list.add(op.asJsonPatchOperation());
Expand All @@ -90,23 +85,20 @@ JsonPatch getPatch()
@Nullable
private JsonPointer findUnchangedValue(final JsonNode value)
{
final Predicate<JsonNode> predicate = EQUIVALENCE.equivalentTo(value);
for (final Map.Entry<JsonPointer, JsonNode> entry: unchanged.entrySet())
if (predicate.apply(entry.getValue()))
if (EQUIVALENCE.equivalent(value, entry.getValue()))
return entry.getKey();
return null;
}

private int findPreviouslyRemoved(final JsonNode value)
{
final Predicate<JsonNode> predicate = EQUIVALENCE.equivalentTo(value);

DiffOperation op;

for (int i = 0; i < diffs.size(); i++) {
op = diffs.get(i);
if (op.getType() == DiffOperation.Type.REMOVE
&& predicate.apply(op.getOldValue()))
&& EQUIVALENCE.equivalent(value, op.getOldValue()))
return i;
}
return -1;
Expand Down
48 changes: 33 additions & 15 deletions src/main/java/com/github/fge/jsonpatch/diff/JsonDiff.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,10 @@
import com.github.fge.jsonpatch.JsonPatchMessages;
import com.github.fge.msgsimple.bundle.MessageBundle;
import com.github.fge.msgsimple.load.MessageBundles;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Equivalence;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;

import javax.annotation.ParametersAreNonnullByDefault;
import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.*;

/**
* JSON "diff" implementation
Expand Down Expand Up @@ -70,7 +64,7 @@ public final class JsonDiff
= MessageBundles.getBundle(JsonPatchMessages.class);
private static final ObjectMapper MAPPER = JacksonUtils.newMapper();

private static final Equivalence<JsonNode> EQUIVALENCE
private static final JsonNumEquals EQUIVALENCE
= JsonNumEquals.getInstance();

private JsonDiff()
Expand Down Expand Up @@ -164,21 +158,46 @@ private static void generateObjectDiffs(final DiffProcessor processor,
final ObjectNode target)
{
final Set<String> firstFields
= Sets.newTreeSet(Sets.newHashSet(source.fieldNames()));
= collect(source.fieldNames(), new TreeSet<String>());
final Set<String> secondFields
= Sets.newTreeSet(Sets.newHashSet(target.fieldNames()));
= collect(target.fieldNames(), new TreeSet<String>());

for (final String field: Sets.difference(firstFields, secondFields))
final Set<String> copy1 = new HashSet<String>(firstFields);
copy1.removeAll(secondFields);

for (final String field: Collections.unmodifiableSet(copy1))
processor.valueRemoved(pointer.append(field), source.get(field));

for (final String field: Sets.difference(secondFields, firstFields))
final Set<String> copy2 = new HashSet<String>(secondFields);
copy2.removeAll(firstFields);


for (final String field: Collections.unmodifiableSet(copy2))
processor.valueAdded(pointer.append(field), target.get(field));

for (final String field: Sets.intersection(firstFields, secondFields))
final Set<String> intersection = new HashSet<String>(firstFields);
intersection.retainAll(secondFields);

for (final String field: intersection)
generateDiffs(processor, pointer.append(field), source.get(field),
target.get(field));
}

private static <T> Set<T> collect(Iterator<T> from, Set<T> to) {
if (from == null) {
throw new NullPointerException();
}
if (to == null) {
throw new NullPointerException();
}
while (from.hasNext()) {
to.add(from.next());
}
return Collections.unmodifiableSet(to);
}



private static void generateArrayDiffs(final DiffProcessor processor,
final JsonPointer pointer, final ArrayNode source,
final ArrayNode target)
Expand All @@ -204,11 +223,10 @@ private static void generateArrayDiffs(final DiffProcessor processor,
}


@VisibleForTesting
static Map<JsonPointer, JsonNode> getUnchangedValues(final JsonNode source,
final JsonNode target)
{
final Map<JsonPointer, JsonNode> ret = Maps.newHashMap();
final Map<JsonPointer, JsonNode> ret = new HashMap<JsonPointer, JsonNode>();
computeUnchanged(ret, JsonPointer.empty(), source, target);
return ret;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.NullNode;
import com.github.fge.jackson.JacksonUtils;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;

import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -71,8 +72,8 @@ public JsonMergePatch deserialize(final JsonParser jp,
* members.
*/

final Set<String> removedMembers = Sets.newHashSet();
final Map<String, JsonMergePatch> modifiedMembers = Maps.newHashMap();
final Set<String> removedMembers = new HashSet<String>();
final Map<String, JsonMergePatch> modifiedMembers = new HashMap<String, JsonMergePatch>();
final Iterator<Map.Entry<String, JsonNode>> iterator = node.fields();

Map.Entry<String, JsonNode> entry;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
import com.github.fge.jsonpatch.JsonPatchException;
import com.google.common.base.Preconditions;

import javax.annotation.ParametersAreNonnullByDefault;
import java.io.IOException;
Expand All @@ -38,7 +37,10 @@ final class NonObjectMergePatch

NonObjectMergePatch(final JsonNode node)
{
this.node = Preconditions.checkNotNull(node);
if (node == null) {
throw new NullPointerException();
}
this.node = node;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.github.fge.jackson.JacksonUtils;
import com.github.fge.jsonpatch.JsonPatchException;
import com.google.common.base.Optional;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;

import javax.annotation.ParametersAreNonnullByDefault;
import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

Expand All @@ -47,8 +47,8 @@ final class ObjectMergePatch
ObjectMergePatch(final Set<String> removedMembers,
final Map<String, JsonMergePatch> modifiedMembers)
{
this.removedMembers = ImmutableSet.copyOf(removedMembers);
this.modifiedMembers = ImmutableMap.copyOf(modifiedMembers);
this.removedMembers = Collections.unmodifiableSet(new HashSet<String>(removedMembers));
this.modifiedMembers = Collections.unmodifiableMap(new HashMap<String, JsonMergePatch>(modifiedMembers));
}

@Override
Expand Down Expand Up @@ -82,8 +82,8 @@ public JsonNode apply(final JsonNode input)
* * if it is an ObjectMergePatch, we get back here; the value will
* be replaced with a JSON Object anyway before being processed.
*/
value = Optional.fromNullable(ret.get(key))
.or(NullNode.getInstance());
final JsonNode jsonNode = ret.get(key);
value = jsonNode != null ? jsonNode : NullNode.getInstance();
ret.replace(key, entry.getValue().apply(value));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import com.github.fge.jackson.JsonNumEquals;
import com.github.fge.msgsimple.bundle.MessageBundle;
import com.github.fge.msgsimple.load.MessageBundles;
import com.google.common.base.Equivalence;
import com.google.common.collect.Lists;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
Expand All @@ -43,7 +42,7 @@ public abstract class JsonPatchOperationTest
private static final MessageBundle BUNDLE
= MessageBundles.getBundle(JsonPatchMessages.class);

private static final Equivalence<JsonNode> EQUIVALENCE
private static final JsonNumEquals EQUIVALENCE
= JsonNumEquals.getInstance();

private final JsonNode errors;
Expand Down
Loading