Skip to content

Commit c416799

Browse files
soberichCapstan
authored andcommitted
Removed Guava (left in tests)
1 parent b4f9ccd commit c416799

18 files changed

+105
-76
lines changed

project.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,10 @@ project.ext.description = "JSON Patch (RFC 6902) and JSON Merge Patch (RFC 7386)
3131
*/
3232
dependencies {
3333
provided(group: "com.google.code.findbugs", name: "jsr305", version: "3.0.2");
34-
compile(group: "com.google.guava", name: "guava", version: "28.1-android");
3534
compile(group: "com.fasterxml.jackson.core", name: "jackson-databind", version: "2.9.9");
3635
compile(group: "com.github.java-json-tools", name: "msg-simple", version: "1.2");
3736

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

4949
javadoc.options {

src/main/java/com/github/fge/jsonpatch/AddOperation.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
import com.github.fge.jackson.jsonpointer.JsonPointer;
2828
import com.github.fge.jackson.jsonpointer.ReferenceToken;
2929
import com.github.fge.jackson.jsonpointer.TokenResolver;
30-
import com.google.common.collect.Iterables;
30+
31+
import java.util.NoSuchElementException;
3132

3233

3334
/**
@@ -105,6 +106,7 @@ private JsonNode addToArray(final JsonPointer path, final JsonNode node)
105106
{
106107
final JsonNode ret = node.deepCopy();
107108
final ArrayNode target = (ArrayNode) path.parent().get(ret);
109+
108110
final TokenResolver<JsonNode> token = Iterables.getLast(path);
109111

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

132134
private JsonNode addToObject(final JsonPointer path, final JsonNode node)
133135
{
136+
final TokenResolver<JsonNode> token = Iterables.getLast(path);
134137
final JsonNode ret = node.deepCopy();
135138
final ObjectNode target = (ObjectNode) path.parent().get(ret);
136-
target.set(Iterables.getLast(path).getToken().getRaw(), value);
139+
target.set(token.getToken().getRaw(), value);
137140
return ret;
138141
}
139142
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.github.fge.jsonpatch;
2+
3+
import java.util.Iterator;
4+
import java.util.NoSuchElementException;
5+
6+
/**
7+
* @author {@literal @}soberich on 30-Nov-18
8+
*/
9+
public final class Iterables {
10+
11+
private Iterables() {}
12+
13+
/**
14+
* Returns the last element of {@code iterable}.
15+
*
16+
* @param <T> underlying type being iterated
17+
* @param iterable type of iterable
18+
* @return the last element of {@code iterable}
19+
* @throws NoSuchElementException if the iterable is empty
20+
*/
21+
public static <T> T getLast(Iterable<T> iterable) {
22+
Iterator<T> iterator = iterable.iterator();
23+
while (true) {
24+
T current = iterator.next();
25+
if (!iterator.hasNext()) {
26+
return current;
27+
}
28+
}
29+
}
30+
}

src/main/java/com/github/fge/jsonpatch/JsonPatch.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,10 @@
2828
import com.github.fge.jackson.JacksonUtils;
2929
import com.github.fge.msgsimple.bundle.MessageBundle;
3030
import com.github.fge.msgsimple.load.MessageBundles;
31-
import com.google.common.collect.ImmutableList;
3231

3332
import java.io.IOException;
33+
import java.util.ArrayList;
34+
import java.util.Collections;
3435
import java.util.List;
3536

3637
/**
@@ -111,7 +112,7 @@ public final class JsonPatch
111112
@JsonCreator
112113
public JsonPatch(final List<JsonPatchOperation> operations)
113114
{
114-
this.operations = ImmutableList.copyOf(operations);
115+
this.operations = Collections.unmodifiableList(new ArrayList<JsonPatchOperation>(operations));
115116
}
116117

117118
/**

src/main/java/com/github/fge/jsonpatch/RemoveOperation.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import com.fasterxml.jackson.databind.node.MissingNode;
3131
import com.fasterxml.jackson.databind.node.ObjectNode;
3232
import com.github.fge.jackson.jsonpointer.JsonPointer;
33-
import com.google.common.collect.Iterables;
3433

3534
import java.io.IOException;
3635

src/main/java/com/github/fge/jsonpatch/ReplaceOperation.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import com.fasterxml.jackson.databind.node.ArrayNode;
2626
import com.fasterxml.jackson.databind.node.ObjectNode;
2727
import com.github.fge.jackson.jsonpointer.JsonPointer;
28-
import com.google.common.collect.Iterables;
2928

3029
/**
3130
* JSON Patch {@code replace} operation

src/main/java/com/github/fge/jsonpatch/TestOperation.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import com.fasterxml.jackson.databind.JsonNode;
2525
import com.github.fge.jackson.JsonNumEquals;
2626
import com.github.fge.jackson.jsonpointer.JsonPointer;
27-
import com.google.common.base.Equivalence;
2827

2928
/**
3029
* JSON Patch {@code test} operation
@@ -42,7 +41,7 @@
4241
public final class TestOperation
4342
extends PathValueOperation
4443
{
45-
private static final Equivalence<JsonNode> EQUIVALENCE
44+
private static final JsonNumEquals EQUIVALENCE
4645
= JsonNumEquals.getInstance();
4746

4847
@JsonCreator

src/main/java/com/github/fge/jsonpatch/diff/DiffProcessor.java

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,28 +24,23 @@
2424
import com.github.fge.jackson.jsonpointer.JsonPointer;
2525
import com.github.fge.jsonpatch.JsonPatch;
2626
import com.github.fge.jsonpatch.JsonPatchOperation;
27-
import com.google.common.base.Equivalence;
28-
import com.google.common.base.Predicate;
29-
import com.google.common.collect.ImmutableMap;
30-
import com.google.common.collect.Lists;
3127

3228
import javax.annotation.Nullable;
33-
import java.util.List;
34-
import java.util.Map;
29+
import java.util.*;
3530

3631
// TODO: cleanup
3732
final class DiffProcessor
3833
{
39-
private static final Equivalence<JsonNode> EQUIVALENCE
34+
private static final JsonNumEquals EQUIVALENCE
4035
= JsonNumEquals.getInstance();
4136

4237
private final Map<JsonPointer, JsonNode> unchanged;
4338

44-
private final List<DiffOperation> diffs = Lists.newArrayList();
39+
private final List<DiffOperation> diffs = new ArrayList<DiffOperation>();
4540

4641
DiffProcessor(final Map<JsonPointer, JsonNode> unchanged)
4742
{
48-
this.unchanged = ImmutableMap.copyOf(unchanged);
43+
this.unchanged = Collections.unmodifiableMap(new HashMap<JsonPointer, JsonNode>(unchanged));
4944
}
5045

5146
void valueReplaced(final JsonPointer pointer, final JsonNode oldValue,
@@ -79,7 +74,7 @@ void valueAdded(final JsonPointer pointer, final JsonNode value)
7974

8075
JsonPatch getPatch()
8176
{
82-
final List<JsonPatchOperation> list = Lists.newArrayList();
77+
final List<JsonPatchOperation> list = new ArrayList<JsonPatchOperation>();
8378

8479
for (final DiffOperation op: diffs)
8580
list.add(op.asJsonPatchOperation());
@@ -90,23 +85,20 @@ JsonPatch getPatch()
9085
@Nullable
9186
private JsonPointer findUnchangedValue(final JsonNode value)
9287
{
93-
final Predicate<JsonNode> predicate = EQUIVALENCE.equivalentTo(value);
9488
for (final Map.Entry<JsonPointer, JsonNode> entry: unchanged.entrySet())
95-
if (predicate.apply(entry.getValue()))
89+
if (EQUIVALENCE.equivalent(value, entry.getValue()))
9690
return entry.getKey();
9791
return null;
9892
}
9993

10094
private int findPreviouslyRemoved(final JsonNode value)
10195
{
102-
final Predicate<JsonNode> predicate = EQUIVALENCE.equivalentTo(value);
103-
10496
DiffOperation op;
10597

10698
for (int i = 0; i < diffs.size(); i++) {
10799
op = diffs.get(i);
108100
if (op.getType() == DiffOperation.Type.REMOVE
109-
&& predicate.apply(op.getOldValue()))
101+
&& EQUIVALENCE.equivalent(value, op.getOldValue()))
110102
return i;
111103
}
112104
return -1;

src/main/java/com/github/fge/jsonpatch/diff/JsonDiff.java

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,10 @@
3131
import com.github.fge.jsonpatch.JsonPatchMessages;
3232
import com.github.fge.msgsimple.bundle.MessageBundle;
3333
import com.github.fge.msgsimple.load.MessageBundles;
34-
import com.google.common.annotations.VisibleForTesting;
35-
import com.google.common.base.Equivalence;
36-
import com.google.common.collect.Maps;
37-
import com.google.common.collect.Sets;
3834

3935
import javax.annotation.ParametersAreNonnullByDefault;
4036
import java.io.IOException;
41-
import java.util.Iterator;
42-
import java.util.Map;
43-
import java.util.Set;
37+
import java.util.*;
4438

4539
/**
4640
* JSON "diff" implementation
@@ -70,7 +64,7 @@ public final class JsonDiff
7064
= MessageBundles.getBundle(JsonPatchMessages.class);
7165
private static final ObjectMapper MAPPER = JacksonUtils.newMapper();
7266

73-
private static final Equivalence<JsonNode> EQUIVALENCE
67+
private static final JsonNumEquals EQUIVALENCE
7468
= JsonNumEquals.getInstance();
7569

7670
private JsonDiff()
@@ -164,21 +158,46 @@ private static void generateObjectDiffs(final DiffProcessor processor,
164158
final ObjectNode target)
165159
{
166160
final Set<String> firstFields
167-
= Sets.newTreeSet(Sets.newHashSet(source.fieldNames()));
161+
= collect(source.fieldNames(), new TreeSet<String>());
168162
final Set<String> secondFields
169-
= Sets.newTreeSet(Sets.newHashSet(target.fieldNames()));
163+
= collect(target.fieldNames(), new TreeSet<String>());
170164

171-
for (final String field: Sets.difference(firstFields, secondFields))
165+
final Set<String> copy1 = new HashSet<String>(firstFields);
166+
copy1.removeAll(secondFields);
167+
168+
for (final String field: Collections.unmodifiableSet(copy1))
172169
processor.valueRemoved(pointer.append(field), source.get(field));
173170

174-
for (final String field: Sets.difference(secondFields, firstFields))
171+
final Set<String> copy2 = new HashSet<String>(secondFields);
172+
copy2.removeAll(firstFields);
173+
174+
175+
for (final String field: Collections.unmodifiableSet(copy2))
175176
processor.valueAdded(pointer.append(field), target.get(field));
176177

177-
for (final String field: Sets.intersection(firstFields, secondFields))
178+
final Set<String> intersection = new HashSet<String>(firstFields);
179+
intersection.retainAll(secondFields);
180+
181+
for (final String field: intersection)
178182
generateDiffs(processor, pointer.append(field), source.get(field),
179183
target.get(field));
180184
}
181185

186+
private static <T> Set<T> collect(Iterator<T> from, Set<T> to) {
187+
if (from == null) {
188+
throw new NullPointerException();
189+
}
190+
if (to == null) {
191+
throw new NullPointerException();
192+
}
193+
while (from.hasNext()) {
194+
to.add(from.next());
195+
}
196+
return Collections.unmodifiableSet(to);
197+
}
198+
199+
200+
182201
private static void generateArrayDiffs(final DiffProcessor processor,
183202
final JsonPointer pointer, final ArrayNode source,
184203
final ArrayNode target)
@@ -204,11 +223,10 @@ private static void generateArrayDiffs(final DiffProcessor processor,
204223
}
205224

206225

207-
@VisibleForTesting
208226
static Map<JsonPointer, JsonNode> getUnchangedValues(final JsonNode source,
209227
final JsonNode target)
210228
{
211-
final Map<JsonPointer, JsonNode> ret = Maps.newHashMap();
229+
final Map<JsonPointer, JsonNode> ret = new HashMap<JsonPointer, JsonNode>();
212230
computeUnchanged(ret, JsonPointer.empty(), source, target);
213231
return ret;
214232
}

src/main/java/com/github/fge/jsonpatch/mergepatch/JsonMergePatchDeserializer.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,11 @@
2727
import com.fasterxml.jackson.databind.JsonNode;
2828
import com.fasterxml.jackson.databind.node.NullNode;
2929
import com.github.fge.jackson.JacksonUtils;
30-
import com.google.common.collect.Maps;
31-
import com.google.common.collect.Sets;
3230

3331
import java.io.IOException;
32+
import java.util.Collections;
33+
import java.util.HashMap;
34+
import java.util.HashSet;
3435
import java.util.Iterator;
3536
import java.util.Map;
3637
import java.util.Set;
@@ -71,8 +72,8 @@ public JsonMergePatch deserialize(final JsonParser jp,
7172
* members.
7273
*/
7374

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

7879
Map.Entry<String, JsonNode> entry;

src/main/java/com/github/fge/jsonpatch/mergepatch/NonObjectMergePatch.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import com.fasterxml.jackson.databind.SerializerProvider;
2626
import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
2727
import com.github.fge.jsonpatch.JsonPatchException;
28-
import com.google.common.base.Preconditions;
2928

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

3938
NonObjectMergePatch(final JsonNode node)
4039
{
41-
this.node = Preconditions.checkNotNull(node);
40+
if (node == null) {
41+
throw new NullPointerException();
42+
}
43+
this.node = node;
4244
}
4345

4446
@Override

src/main/java/com/github/fge/jsonpatch/mergepatch/ObjectMergePatch.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@
2828
import com.fasterxml.jackson.databind.node.ObjectNode;
2929
import com.github.fge.jackson.JacksonUtils;
3030
import com.github.fge.jsonpatch.JsonPatchException;
31-
import com.google.common.base.Optional;
32-
import com.google.common.collect.ImmutableMap;
33-
import com.google.common.collect.ImmutableSet;
3431

3532
import javax.annotation.ParametersAreNonnullByDefault;
3633
import java.io.IOException;
34+
import java.util.Collections;
35+
import java.util.HashMap;
36+
import java.util.HashSet;
3737
import java.util.Map;
3838
import java.util.Set;
3939

@@ -47,8 +47,8 @@ final class ObjectMergePatch
4747
ObjectMergePatch(final Set<String> removedMembers,
4848
final Map<String, JsonMergePatch> modifiedMembers)
4949
{
50-
this.removedMembers = ImmutableSet.copyOf(removedMembers);
51-
this.modifiedMembers = ImmutableMap.copyOf(modifiedMembers);
50+
this.removedMembers = Collections.unmodifiableSet(new HashSet<String>(removedMembers));
51+
this.modifiedMembers = Collections.unmodifiableMap(new HashMap<String, JsonMergePatch>(modifiedMembers));
5252
}
5353

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

src/test/java/com/github/fge/jsonpatch/JsonPatchOperationTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import com.github.fge.jackson.JsonNumEquals;
2727
import com.github.fge.msgsimple.bundle.MessageBundle;
2828
import com.github.fge.msgsimple.load.MessageBundles;
29-
import com.google.common.base.Equivalence;
3029
import com.google.common.collect.Lists;
3130
import org.testng.annotations.DataProvider;
3231
import org.testng.annotations.Test;
@@ -43,7 +42,7 @@ public abstract class JsonPatchOperationTest
4342
private static final MessageBundle BUNDLE
4443
= MessageBundles.getBundle(JsonPatchMessages.class);
4544

46-
private static final Equivalence<JsonNode> EQUIVALENCE
45+
private static final JsonNumEquals EQUIVALENCE
4746
= JsonNumEquals.getInstance();
4847

4948
private final JsonNode errors;

0 commit comments

Comments
 (0)