Skip to content
SQiShER edited this page Apr 24, 2012 · 20 revisions

Maven Dependency

  1. First you need to add this repository to your POM:

     <repositories>
     	<repository>
     		<id>raw.github.com/SQiShER</id>
     		<url>https://raw.github.com/SQiShER/mvn-repos/master/releases/</url>
     		<snapshots>
     			<enabled>false</enabled>
     		</snapshots>
     	</repository>
     	…
     </repositories>
    
  2. And then this dependency:

     <dependency>
     	<groupId>de.danielbechler</groupId>
     	<artifactId>java-object-diff</artifactId>
     	<version>0.8</version>
     </dependency>
    
  3. Now you should be ready to go!

First Steps

Now that you have the framework at hand, let’s have a look at the API. The most important class you need to know about is the ObjectDifferFactory. This is the one and only way to create new ObjectDiffer instances.

ObjectDiffer objectDiffer = ObjectDifferFactory.getInstance();

Great, so there we have our ObjectDiffer. Now what? Let’s see what we can do with it! The only method you need to care about right now is <T> Node compare(T, T). It will do all magic and return a root node, representing the objects, you passed as arguments. Lets test it out with simple Strings.

final String working = "Hello";
final String base = "World";
final Node root = objectDiffer.compare(working, base);

As you can see, we are thinking in terms of a working (or potentially modified) and its corresponding base version. Terms like ADDED or REMOVED will always relate to the working version.

So how can we see, if the above code returns the expected result? It would be nice to simply print the entire Node hierarchy in a readable form. Fortunately, there is a Visitor for this:

root.visit(new PrintingVisitor(working, base));

This will print the following output to the console:

Property at path '/' has been changed from [ World ] to [ Hello ]

That’s great! It works just as expected. Unfortunately this example is pretty boring, considering that you could have gotten to the same result using the equals method of one of the Strings. So lets move on a more complicated example.

Complex Objects

To do

Clone this wiki locally