31
31
import com .github .fge .jsonpatch .JsonPatchMessages ;
32
32
import com .github .fge .msgsimple .bundle .MessageBundle ;
33
33
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 ;
38
34
39
35
import javax .annotation .ParametersAreNonnullByDefault ;
40
36
import java .io .IOException ;
41
- import java .util .Iterator ;
42
- import java .util .Map ;
43
- import java .util .Set ;
37
+ import java .util .*;
44
38
45
39
/**
46
40
* JSON "diff" implementation
@@ -70,7 +64,7 @@ public final class JsonDiff
70
64
= MessageBundles .getBundle (JsonPatchMessages .class );
71
65
private static final ObjectMapper MAPPER = JacksonUtils .newMapper ();
72
66
73
- private static final Equivalence < JsonNode > EQUIVALENCE
67
+ private static final JsonNumEquals EQUIVALENCE
74
68
= JsonNumEquals .getInstance ();
75
69
76
70
private JsonDiff ()
@@ -164,21 +158,46 @@ private static void generateObjectDiffs(final DiffProcessor processor,
164
158
final ObjectNode target )
165
159
{
166
160
final Set <String > firstFields
167
- = Sets . newTreeSet ( Sets . newHashSet ( source .fieldNames ()));
161
+ = collect ( source .fieldNames (), new TreeSet < String >( ));
168
162
final Set <String > secondFields
169
- = Sets . newTreeSet ( Sets . newHashSet ( target .fieldNames ()));
163
+ = collect ( target .fieldNames (), new TreeSet < String >( ));
170
164
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 ))
172
169
processor .valueRemoved (pointer .append (field ), source .get (field ));
173
170
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 ))
175
176
processor .valueAdded (pointer .append (field ), target .get (field ));
176
177
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 )
178
182
generateDiffs (processor , pointer .append (field ), source .get (field ),
179
183
target .get (field ));
180
184
}
181
185
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
+
182
201
private static void generateArrayDiffs (final DiffProcessor processor ,
183
202
final JsonPointer pointer , final ArrayNode source ,
184
203
final ArrayNode target )
@@ -204,11 +223,10 @@ private static void generateArrayDiffs(final DiffProcessor processor,
204
223
}
205
224
206
225
207
- @ VisibleForTesting
208
226
static Map <JsonPointer , JsonNode > getUnchangedValues (final JsonNode source ,
209
227
final JsonNode target )
210
228
{
211
- final Map <JsonPointer , JsonNode > ret = Maps . newHashMap ();
229
+ final Map <JsonPointer , JsonNode > ret = new HashMap < JsonPointer , JsonNode > ();
212
230
computeUnchanged (ret , JsonPointer .empty (), source , target );
213
231
return ret ;
214
232
}
0 commit comments