Skip to content

Commit 5bdf981

Browse files
committed
New class DiffProcessor
This is the class which will hold diff operations and perform factorization/etc when possible.
1 parent eeb2279 commit 5bdf981

1 file changed

Lines changed: 69 additions & 0 deletions

File tree

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright (c) 2014, Francis Galiegue (fgaliegue@gmail.com)
3+
*
4+
* This software is dual-licensed under:
5+
*
6+
* - the Lesser General Public License (LGPL) version 3.0 or, at your option, any
7+
* later version;
8+
* - the Apache Software License (ASL) version 2.0.
9+
*
10+
* The text of this file and of both licenses is available at the root of this
11+
* project or, if you have the jar distribution, in directory META-INF/, under
12+
* the names LGPL-3.0.txt and ASL-2.0.txt respectively.
13+
*
14+
* Direct link to the sources:
15+
*
16+
* - LGPL 3.0: https://www.gnu.org/licenses/lgpl-3.0.txt
17+
* - ASL 2.0: http://www.apache.org/licenses/LICENSE-2.0.txt
18+
*/
19+
20+
package com.github.fge.jsonpatch.diff2;
21+
22+
import com.fasterxml.jackson.databind.JsonNode;
23+
import com.github.fge.jackson.jsonpointer.JsonPointer;
24+
import com.github.fge.jsonpatch.JsonPatch;
25+
import com.github.fge.jsonpatch.JsonPatchOperation;
26+
import com.google.common.collect.ImmutableMap;
27+
import com.google.common.collect.Lists;
28+
29+
import java.util.List;
30+
import java.util.Map;
31+
32+
// Non final because we want to be able to Mockito.spy() on it
33+
class DiffProcessor
34+
{
35+
private final Map<JsonPointer, JsonNode> unchanged;
36+
37+
private final List<DiffOperation> diffs = Lists.newArrayList();
38+
39+
DiffProcessor(final Map<JsonPointer, JsonNode> unchanged)
40+
{
41+
this.unchanged = ImmutableMap.copyOf(unchanged);
42+
}
43+
44+
void valueReplaced(final JsonPointer pointer, final JsonNode oldValue,
45+
final JsonNode newValue)
46+
{
47+
diffs.add(DiffOperation.replace(pointer, oldValue, newValue));
48+
}
49+
50+
void valueRemoved(final JsonPointer pointer, final JsonNode value)
51+
{
52+
diffs.add(DiffOperation.remove(pointer, value));
53+
}
54+
55+
void valueAdded(final JsonPointer pointer, final JsonNode value)
56+
{
57+
diffs.add(DiffOperation.add(pointer, value));
58+
}
59+
60+
JsonPatch getPatch()
61+
{
62+
final List<JsonPatchOperation> list = Lists.newArrayList();
63+
64+
for (final DiffOperation op: diffs)
65+
list.add(op.asJsonPatchOperation());
66+
67+
return new JsonPatch(list);
68+
}
69+
}

0 commit comments

Comments
 (0)