Skip to content

Commit 8e66487

Browse files
committed
Fixed the coupling and God class issue in 'DiffUtils.java'
1 parent 11edc3a commit 8e66487

12 files changed

Lines changed: 127 additions & 84 deletions
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.github.difflib;
2+
3+
import com.github.difflib.algorithm.DiffAlgorithmFactory;
4+
import com.github.difflib.algorithm.myers.MyersDiff;
5+
6+
/**
7+
* Default algorithm configuration for DiffUtils.
8+
*/
9+
public final class DiffAlgorithmDefaults {
10+
public static DiffAlgorithmFactory getDefault() {
11+
return MyersDiff.factory();
12+
}
13+
14+
private DiffAlgorithmDefaults() {}
15+
}

java-diff-utils/src/main/java/com/github/difflib/DiffUtils.java

Lines changed: 3 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
77
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
8+
* http://www.apache.org/licenses/LICENSE-2.0
99
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -18,26 +18,21 @@
1818
import com.github.difflib.algorithm.DiffAlgorithmFactory;
1919
import com.github.difflib.algorithm.DiffAlgorithmI;
2020
import com.github.difflib.algorithm.DiffAlgorithmListener;
21-
import com.github.difflib.algorithm.myers.MyersDiff;
22-
import com.github.difflib.patch.AbstractDelta;
2321
import com.github.difflib.patch.Patch;
24-
import com.github.difflib.patch.PatchFailedException;
25-
import java.util.ArrayList;
2622
import java.util.Arrays;
27-
import java.util.Collections;
2823
import java.util.List;
2924
import java.util.Objects;
3025
import java.util.function.BiPredicate;
3126

3227
/**
33-
* Utility class to implement the difference and patching engine.
28+
* Utility class to implement the difference engine.
3429
*/
3530
public final class DiffUtils {
3631

3732
/**
3833
* This factory generates the DEFAULT_DIFF algorithm for all these routines.
3934
*/
40-
static DiffAlgorithmFactory DEFAULT_DIFF = MyersDiff.factory();
35+
static DiffAlgorithmFactory DEFAULT_DIFF = DiffAlgorithmDefaults.getDefault();
4136

4237
/**
4338
* Sets the default diff algorithm factory to be used by all diff routines.
@@ -167,63 +162,5 @@ public static <T> Patch<T> diff(
167162
return diff(original, revised, algorithm, null);
168163
}
169164

170-
/**
171-
* Computes the difference between the given texts inline. This one uses the
172-
* "trick" to make out of texts lists of characters, like DiffRowGenerator
173-
* does and merges those changes at the end together again.
174-
*
175-
* @param original a {@link String} representing the original text. Must not be {@code null}.
176-
* @param revised a {@link String} representing the revised text. Must not be {@code null}.
177-
* @return The patch describing the difference between the original and
178-
* revised sequences. Never {@code null}.
179-
*/
180-
public static Patch<String> diffInline(String original, String revised) {
181-
List<String> origList = new ArrayList<>();
182-
List<String> revList = new ArrayList<>();
183-
for (Character character : original.toCharArray()) {
184-
origList.add(character.toString());
185-
}
186-
for (Character character : revised.toCharArray()) {
187-
revList.add(character.toString());
188-
}
189-
Patch<String> patch = DiffUtils.diff(origList, revList);
190-
for (AbstractDelta<String> delta : patch.getDeltas()) {
191-
delta.getSource().setLines(compressLines(delta.getSource().getLines(), ""));
192-
delta.getTarget().setLines(compressLines(delta.getTarget().getLines(), ""));
193-
}
194-
return patch;
195-
}
196-
197-
/**
198-
* Applies the given patch to the original list and returns the revised list.
199-
*
200-
* @param original a {@link List} representing the original list.
201-
* @param patch a {@link List} representing the patch to apply.
202-
* @return the revised list.
203-
* @throws PatchFailedException if the patch cannot be applied.
204-
*/
205-
public static <T> List<T> patch(List<? extends T> original, Patch<T> patch) throws PatchFailedException {
206-
return patch.applyTo(original);
207-
}
208-
209-
/**
210-
* Applies the given patch to the revised list and returns the original list.
211-
*
212-
* @param revised a {@link List} representing the revised list.
213-
* @param patch a {@link Patch} representing the patch to apply.
214-
* @return the original list.
215-
* @throws PatchFailedException if the patch cannot be applied.
216-
*/
217-
public static <T> List<T> unpatch(List<? extends T> revised, Patch<T> patch) {
218-
return patch.restore(revised);
219-
}
220-
221-
private static List<String> compressLines(List<String> lines, String delimiter) {
222-
if (lines.isEmpty()) {
223-
return Collections.emptyList();
224-
}
225-
return Collections.singletonList(String.join(delimiter, lines));
226-
}
227-
228165
private DiffUtils() {}
229166
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.github.difflib;
2+
3+
import com.github.difflib.patch.AbstractDelta;
4+
import com.github.difflib.patch.Patch;
5+
import java.util.ArrayList;
6+
import java.util.Collections;
7+
import java.util.List;
8+
9+
/**
10+
* Utility class to implement inline character-level differences.
11+
*/
12+
public final class InlineDiffUtils {
13+
14+
/**
15+
* Computes the difference between the given texts inline. This one uses the
16+
* "trick" to make out of texts lists of characters, like DiffRowGenerator
17+
* does and merges those changes at the end together again.
18+
*
19+
* @param original a {@link String} representing the original text. Must not be {@code null}.
20+
* @param revised a {@link String} representing the revised text. Must not be {@code null}.
21+
* @return The patch describing the difference between the original and
22+
* revised sequences. Never {@code null}.
23+
*/
24+
public static Patch<String> diffInline(String original, String revised) {
25+
List<String> origList = new ArrayList<>();
26+
List<String> revList = new ArrayList<>();
27+
for (Character character : original.toCharArray()) {
28+
origList.add(character.toString());
29+
}
30+
for (Character character : revised.toCharArray()) {
31+
revList.add(character.toString());
32+
}
33+
Patch<String> patch = DiffUtils.diff(origList, revList);
34+
for (AbstractDelta<String> delta : patch.getDeltas()) {
35+
delta.getSource().setLines(compressLines(delta.getSource().getLines(), ""));
36+
delta.getTarget().setLines(compressLines(delta.getTarget().getLines(), ""));
37+
}
38+
return patch;
39+
}
40+
41+
private static List<String> compressLines(List<String> lines, String delimiter) {
42+
if (lines.isEmpty()) {
43+
return Collections.emptyList();
44+
}
45+
return Collections.singletonList(String.join(delimiter, lines));
46+
}
47+
48+
private InlineDiffUtils() {}
49+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.github.difflib;
2+
3+
import com.github.difflib.patch.Patch;
4+
import com.github.difflib.patch.PatchFailedException;
5+
import java.util.List;
6+
7+
/**
8+
* Utility class to implement the patching engine.
9+
*/
10+
public final class PatchUtils {
11+
12+
/**
13+
* Applies the given patch to the original list and returns the revised list.
14+
*
15+
* @param original a {@link List} representing the original list.
16+
* @param patch a {@link List} representing the patch to apply.
17+
* @return the revised list.
18+
* @throws PatchFailedException if the patch cannot be applied.
19+
*/
20+
public static <T> List<T> patch(List<? extends T> original, Patch<T> patch) throws PatchFailedException {
21+
return patch.applyTo(original);
22+
}
23+
24+
/**
25+
* Applies the given patch to the revised list and returns the original list.
26+
*
27+
* @param revised a {@link List} representing the revised list.
28+
* @param patch a {@link Patch} representing the patch to apply.
29+
* @return the original list.
30+
* @throws PatchFailedException if the patch cannot be applied.
31+
*/
32+
public static <T> List<T> unpatch(List<? extends T> revised, Patch<T> patch) {
33+
return patch.restore(revised);
34+
}
35+
36+
private PatchUtils() {}
37+
}

java-diff-utils/src/test/java/com/github/difflib/DiffUtilsTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public void testDiff_EmptyListWithNonEmpty() {
8585

8686
@Test
8787
public void testDiffInline() {
88-
final Patch<String> patch = DiffUtils.diffInline("", "test");
88+
final Patch<String> patch = InlineDiffUtils.diffInline("", "test");
8989
assertEquals(1, patch.getDeltas().size());
9090
assertTrue(patch.getDeltas().get(0) instanceof InsertDelta);
9191
assertEquals(0, patch.getDeltas().get(0).getSource().getPosition());
@@ -95,7 +95,7 @@ public void testDiffInline() {
9595

9696
@Test
9797
public void testDiffInline2() {
98-
final Patch<String> patch = DiffUtils.diffInline("es", "fest");
98+
final Patch<String> patch = InlineDiffUtils.diffInline("es", "fest");
9999
assertEquals(2, patch.getDeltas().size());
100100
assertTrue(patch.getDeltas().get(0) instanceof InsertDelta);
101101
assertEquals(0, patch.getDeltas().get(0).getSource().getPosition());

java-diff-utils/src/test/java/com/github/difflib/GenerateUnifiedDiffTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public void testDiff_Issue10() throws IOException {
7070
final List<String> patchLines = fileToLines(TestConstants.MOCK_FOLDER + "issue10_patch.txt");
7171
final Patch<String> p = UnifiedDiffUtils.parseUnifiedDiff(patchLines);
7272
try {
73-
DiffUtils.patch(baseLines, p);
73+
PatchUtils.patch(baseLines, p);
7474
} catch (PatchFailedException e) {
7575
fail(e.getMessage());
7676
}
@@ -208,7 +208,7 @@ public void testFailingPatchByException() throws IOException {
208208
// make original not fitting
209209
baseLines.set(40, baseLines.get(40) + " corrupted ");
210210

211-
assertThrows(PatchFailedException.class, () -> DiffUtils.patch(baseLines, p));
211+
assertThrows(PatchFailedException.class, () -> PatchUtils.patch(baseLines, p));
212212
}
213213

214214
@Test

java-diff-utils/src/test/java/com/github/difflib/algorithm/myers/WithMyersDiffWithLinearSpacePatchTest.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import static org.junit.jupiter.api.Assertions.fail;
66

77
import com.github.difflib.DiffUtils;
8+
import com.github.difflib.PatchUtils;
89
import com.github.difflib.patch.*;
910
import java.io.ByteArrayInputStream;
1011
import java.io.ByteArrayOutputStream;
@@ -30,7 +31,7 @@ public void testPatch_Insert() {
3031
final Patch<String> patch =
3132
DiffUtils.diff(insertTest_from, insertTest_to, new MyersDiffWithLinearSpace<String>());
3233
try {
33-
assertEquals(insertTest_to, DiffUtils.patch(insertTest_from, patch));
34+
assertEquals(insertTest_to, PatchUtils.patch(insertTest_from, patch));
3435
} catch (PatchFailedException e) {
3536
fail(e.getMessage());
3637
}
@@ -44,7 +45,7 @@ public void testPatch_Delete() {
4445
final Patch<String> patch =
4546
DiffUtils.diff(deleteTest_from, deleteTest_to, new MyersDiffWithLinearSpace<String>());
4647
try {
47-
assertEquals(deleteTest_to, DiffUtils.patch(deleteTest_from, patch));
48+
assertEquals(deleteTest_to, PatchUtils.patch(deleteTest_from, patch));
4849
} catch (PatchFailedException e) {
4950
fail(e.getMessage());
5051
}
@@ -58,7 +59,7 @@ public void testPatch_Change() {
5859
final Patch<String> patch =
5960
DiffUtils.diff(changeTest_from, changeTest_to, new MyersDiffWithLinearSpace<String>());
6061
try {
61-
assertEquals(changeTest_to, DiffUtils.patch(changeTest_from, patch));
62+
assertEquals(changeTest_to, PatchUtils.patch(changeTest_from, patch));
6263
} catch (PatchFailedException e) {
6364
fail(e.getMessage());
6465
}
@@ -167,7 +168,7 @@ public void testPatch_Serializable() throws IOException, ClassNotFoundException
167168
in.close();
168169

169170
try {
170-
assertEquals(changeTest_to, DiffUtils.patch(changeTest_from, result));
171+
assertEquals(changeTest_to, PatchUtils.patch(changeTest_from, result));
171172
} catch (PatchFailedException e) {
172173
fail(e.getMessage());
173174
}
@@ -186,7 +187,7 @@ public void testPatch_Change_withExceptionProcessor() {
186187
patch.withConflictOutput(Patch.CONFLICT_PRODUCES_MERGE_CONFLICT);
187188

188189
try {
189-
List<String> data = DiffUtils.patch(changeTest_from, patch);
190+
List<String> data = PatchUtils.patch(changeTest_from, patch);
190191
assertEquals(11, data.size());
191192

192193
assertEquals(

java-diff-utils/src/test/java/com/github/difflib/examples/ApplyPatch.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.github.difflib.examples;
22

3-
import com.github.difflib.DiffUtils;
3+
import com.github.difflib.PatchUtils;
44
import com.github.difflib.TestConstants;
55
import com.github.difflib.UnifiedDiffUtils;
66
import com.github.difflib.patch.Patch;
@@ -23,7 +23,7 @@ public static void main(String[] args) throws PatchFailedException, IOException
2323
Patch<String> patch = UnifiedDiffUtils.parseUnifiedDiff(patched);
2424

2525
// Then apply the computed patch to the given text
26-
List<String> result = DiffUtils.patch(original, patch);
26+
List<String> result = PatchUtils.patch(original, patch);
2727
System.out.println(result);
2828
// / Or we can call patch.applyTo(original). There is no difference.
2929
}

java-diff-utils/src/test/java/com/github/difflib/patch/PatchWithAllDiffAlgorithmsTest.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import static org.junit.jupiter.api.Assertions.fail;
55

66
import com.github.difflib.DiffUtils;
7+
import com.github.difflib.PatchUtils;
78
import com.github.difflib.algorithm.DiffAlgorithmFactory;
89
import com.github.difflib.algorithm.myers.MyersDiff;
910
import com.github.difflib.algorithm.myers.MyersDiffWithLinearSpace;
@@ -41,7 +42,7 @@ public void testPatch_Insert(DiffAlgorithmFactory factory) {
4142

4243
final Patch<String> patch = DiffUtils.diff(insertTest_from, insertTest_to);
4344
try {
44-
assertEquals(insertTest_to, DiffUtils.patch(insertTest_from, patch));
45+
assertEquals(insertTest_to, PatchUtils.patch(insertTest_from, patch));
4546
} catch (PatchFailedException e) {
4647
fail(e.getMessage());
4748
}
@@ -57,7 +58,7 @@ public void testPatch_Delete(DiffAlgorithmFactory factory) {
5758

5859
final Patch<String> patch = DiffUtils.diff(deleteTest_from, deleteTest_to);
5960
try {
60-
assertEquals(deleteTest_to, DiffUtils.patch(deleteTest_from, patch));
61+
assertEquals(deleteTest_to, PatchUtils.patch(deleteTest_from, patch));
6162
} catch (PatchFailedException e) {
6263
fail(e.getMessage());
6364
}
@@ -73,7 +74,7 @@ public void testPatch_Change(DiffAlgorithmFactory factory) {
7374

7475
final Patch<String> patch = DiffUtils.diff(changeTest_from, changeTest_to);
7576
try {
76-
assertEquals(changeTest_to, DiffUtils.patch(changeTest_from, patch));
77+
assertEquals(changeTest_to, PatchUtils.patch(changeTest_from, patch));
7778
} catch (PatchFailedException e) {
7879
fail(e.getMessage());
7980
}
@@ -98,7 +99,7 @@ public void testPatch_Serializable(DiffAlgorithmFactory factory) throws IOExcept
9899
in.close();
99100

100101
try {
101-
assertEquals(changeTest_to, DiffUtils.patch(changeTest_from, result));
102+
assertEquals(changeTest_to, PatchUtils.patch(changeTest_from, result));
102103
} catch (PatchFailedException e) {
103104
fail(e.getMessage());
104105
}

java-diff-utils/src/test/java/com/github/difflib/patch/PatchWithMyerDiffTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import static org.junit.jupiter.api.Assertions.fail;
2222

2323
import com.github.difflib.DiffUtils;
24+
import com.github.difflib.PatchUtils;
2425
import java.util.Arrays;
2526
import java.util.List;
2627
import org.junit.jupiter.api.Test;
@@ -43,7 +44,7 @@ public void testPatch_Change_withExceptionProcessor() {
4344
patch.withConflictOutput(Patch.CONFLICT_PRODUCES_MERGE_CONFLICT);
4445

4546
try {
46-
List<String> data = DiffUtils.patch(changeTest_from, patch);
47+
List<String> data = PatchUtils.patch(changeTest_from, patch);
4748
assertEquals(9, data.size());
4849

4950
assertEquals(

0 commit comments

Comments
 (0)