Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import com.google.common.base.Equivalence;
import com.google.common.collect.Lists;

import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;

Expand Down Expand Up @@ -106,6 +108,7 @@ public static void factorizeDiffs(final List<Diff> diffs)
private static void findPairs(final List<Diff> diffs)
{
final int diffsSize = diffs.size();
Collection<Diff> alreadyPaired = new HashSet<Diff>();

Diff addition, removal;

Expand All @@ -119,7 +122,7 @@ private static void findPairs(final List<Diff> diffs)
*/
for (int removeIndex = 0; removeIndex < diffsSize; removeIndex++) {
removal = diffs.get(removeIndex);
if (removal.operation != DiffOperation.REMOVE)
if (removal.operation != DiffOperation.REMOVE || alreadyPaired.contains(removal))
continue;
if (!EQUIVALENCE.equivalent(removal.value, addition.value))
continue;
Expand All @@ -131,6 +134,10 @@ private static void findPairs(final List<Diff> diffs)
addition.firstOfPair = addIndex < removeIndex;
removal.pairedDiff = addition;
removal.firstOfPair = removeIndex < addIndex;

alreadyPaired.add(removal);
alreadyPaired.add(addition);

break;
}
}
Expand Down
8 changes: 8 additions & 0 deletions src/test/resources/jsonpatch/factorizing-diff.json
Original file line number Diff line number Diff line change
Expand Up @@ -296,5 +296,13 @@
{ "op": "remove", "path": "/0" },
{ "op": "remove", "path": "/1" }
]
},
{
"first": { "b": [0, 1, 2] },
"second": { "b": [1, 2], "c": 0 ,"d":0},
"patch": [
{ "op": "move", "from": "/b/0", "path": "/c"},
{ "op": "add", "path": "/d", "value": 0}
]
}
]