Skip to content

Commit 1d2b28c

Browse files
committed
JsonDiff: more Javadoc; enforce non null arguments
1 parent fe0f21a commit 1d2b28c

File tree

4 files changed

+51
-4
lines changed

4 files changed

+51
-4
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import java.util.List;
3434
import java.util.Map;
3535

36+
// TODO: cleanup
3637
final class DiffProcessor
3738
{
3839
private static final Equivalence<JsonNode> EQUIVALENCE

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

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828
import com.github.fge.jackson.NodeType;
2929
import com.github.fge.jackson.jsonpointer.JsonPointer;
3030
import com.github.fge.jsonpatch.JsonPatch;
31+
import com.github.fge.jsonpatch.JsonPatchMessages;
32+
import com.github.fge.msgsimple.bundle.MessageBundle;
33+
import com.github.fge.msgsimple.load.MessageBundles;
3134
import com.google.common.annotations.VisibleForTesting;
3235
import com.google.common.base.Equivalence;
3336
import com.google.common.collect.Maps;
@@ -50,11 +53,21 @@
5053
* patch for any other source/target combination than the one used to generate
5154
* the patch.</p>
5255
*
56+
* <p>This class always performs operations in the following order: removals,
57+
* additions and replacements. It then factors removal/addition pairs into
58+
* move operations, or copy operations if a common element exists, at the same
59+
* {@link JsonPointer pointer}, in both the source and destination.</p>
60+
*
61+
* <p>You can obtain a diff either as a {@link JsonPatch} directly or, for
62+
* backwards compatibility, as a {@link JsonNode}.</p>
63+
*
5364
* @since 1.2
5465
*/
5566
@ParametersAreNonnullByDefault
5667
public final class JsonDiff
5768
{
69+
private static final MessageBundle BUNDLE
70+
= MessageBundles.getBundle(JsonPatchMessages.class);
5871
private static final ObjectMapper MAPPER = JacksonUtils.newMapper();
5972

6073
private static final Equivalence<JsonNode> EQUIVALENCE
@@ -77,6 +90,8 @@ private JsonDiff()
7790
public static JsonPatch asJsonPatch(final JsonNode source,
7891
final JsonNode target)
7992
{
93+
BUNDLE.checkNotNull(source, "common.nullArgument");
94+
BUNDLE.checkNotNull(target, "common.nullArgument");
8095
final Map<JsonPointer, JsonNode> unchanged
8196
= getUnchangedValues(source, target);
8297
final DiffProcessor processor = new DiffProcessor(unchanged);
@@ -172,17 +187,17 @@ private static void generateArrayDiffs(final DiffProcessor processor,
172187
final int secondSize = target.size();
173188
final int size = Math.min(firstSize, secondSize);
174189

175-
for (int index = 0; index < size; index++)
176-
generateDiffs(processor, pointer.append(index), source.get(index),
177-
target.get(index));
178-
179190
/*
180191
* Source array is larger; in this case, elements are removed from the
181192
* target; the index of removal is always the original arrays's length.
182193
*/
183194
for (int index = size; index < firstSize; index++)
184195
processor.valueRemoved(pointer.append(size), source.get(index));
185196

197+
for (int index = 0; index < size; index++)
198+
generateDiffs(processor, pointer.append(index), source.get(index),
199+
target.get(index));
200+
186201
// Deal with the destination array being larger...
187202
for (int index = size; index < secondSize; index++)
188203
processor.valueAdded(pointer.append("-"), target.get(index));
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright (c) 2014, Francis Galiegue ([email protected])
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+
/**
21+
* JSON "diff" implementation
22+
*
23+
* <p>The main class, {@link com.github.fge.jsonpatch.diff.JsonDiff}, does the
24+
* reverse of what JSON Patch does: given two JSON values, it generates a patch
25+
* (as JSON) to apply to the first node in order to obtain the second node.</p>
26+
*
27+
* <p>This implementation is able to factorize additions and removals into
28+
* moves and copies.</p>
29+
*/
30+
package com.github.fge.jsonpatch.diff2;

src/main/resources/com/github/fge/jsonpatch/messages.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
# - ASL 2.0: http://www.apache.org/licenses/LICENSE-2.0.txt
1818
#
1919

20+
common.nullArgument=argument cannot be null
2021
jsonPatch.deserFailed=unable to deserialize JSON input
2122
jsonPatch.nullInput=input cannot be null
2223
jsonPatch.nullValue=value cannot be null

0 commit comments

Comments
 (0)