3131import com .github .fge .jsonpatch .JsonPatchMessages ;
3232import com .github .fge .msgsimple .bundle .MessageBundle ;
3333import 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
3935import javax .annotation .ParametersAreNonnullByDefault ;
4036import 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 }
0 commit comments