Monday, 31 March 2014

An example of how to use DiffUtils

Might want to look at the newer blog post about using DiffMatchPatch instead of DiffUtils?

Summary:

The code that follows is a JUnit test example of how to use the DiffUtils API.

This example has two variables and two goals. 

The first variable is the original String (firstVersion).
The second variable is how the original String has been edited (secondVersion).

The first goal is to print out the Delta's (differences) of the edited String compared to the original String.

The second goal is to initiate a "rebuilding process". Taking the original edited String, applying the new found Delta's (changes if you like) to the original String to return a result String. If the "rebuilding process"(ie: the Patch object methods) is successful, the result String should be identical to the secondVersion (edited String).

Code:


@Test
public void diffUtilsExample()
{
 String firstVersion = "This is a sentence with no change.\n" +
   "\n" +
   "No change here.\n" +
   "No change here. No change here. No change here. No change here.\n" +
   "No change here. No change here.\n" +
   "No change here.\n" +
   "No change here.\n" +
   "No change here. No change here. No change here.\n" +
   "No change here.\n" +
   "\n" +
   "No change here.\n" +
   "No change here. No change here. No change here.";

 String secondVersion = "This is a sentence with some change.\n" +
   "\n" +
   "No change here.\n" +
   "No change here. No change here. No change here. No change here.\n" +
   "Changed over here. No change here.\n" +
   "No change here.\n" +
   "No change here.\n" +
   "No change here. Also changed here. No change here.\n" +
   "No change here.\n" +
   "\n" +
   "No change here.\n" +
   "No change here. Some changes over here. No change here.";

 String result = getDiff(firstVersion, secondVersion, "\n");
 if(!result.equals(secondVersion))
 {
  fail("diff test has failed, they are not the same. Check the getDiff() method");
 }
}

public String getDiff(String firstVersion, String secondVersion, String splitValue)
{
 List<String> original = new ArrayList(Arrays.asList(firstVersion.split(splitValue)));
 List<String> revised = new ArrayList(Arrays.asList(secondVersion.split(splitValue)));

 Patch patch = DiffUtils.diff(original, revised);

 for(Delta delta : patch.getDeltas())
 {
  System.out.println(delta);
 }

 try {
  List<String> result = (List<String>) patch.applyTo(original);

  if(!result.equals(revised))
  {
   fail("the patch.applyTo 'rebuild from diffs' method has not produced a result that matches the revised string-list");
   return null;
  }

  StringBuilder stringList = new StringBuilder();

  for(int i = 0; i < result.size(); i++)
  {
   String s = result.get(i);
   if(i != result.size() - 1)
    stringList.append(s + splitValue);
   else
    stringList.append(s);
  }

  String merge = String.valueOf(stringList);

  return merge;
 } catch (PatchFailedException e) {
  e.printStackTrace();
 }
 return null;
}

Notes:

The java-diff-utils libraries can be found here .

I haven't investigated a way to use DiffUtils without using two List<String> as the comparison variables. I wish I could simply use two Strings as the arguments for the DiffUtils.diff() method. Hope I'll come across this answer sometime in the future. Found a solution for this problem in my newer post .