This post demonstrates how to identify and list the differences between two objects using a set of classes and interfaces from Apache Commons Lang.
Classes, Intefaces, and pom.xml
The classes and interfaces to be used for this post are as follow:
- org.apache.commons.lang3.builder.Diff
- org.apache.commons.lang3.builder.DiffResult
- org.apache.commons.lang3.builder.DiffBuilder
- org.apache.commons.lang3.builder.Diffable
- org.apache.commons.lang3.builder.ToStringStyle
We are using commons-lang3
version 3.6
. We also use Java 8
.
1 2 3 4 5 6 7 8 9 10 | ... <dependencies> <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.6</version> </dependency> </dependencies> ... |
Scenario
We have two blog posts for our use case, and we need to list the differences between them. The list must show the affected field and the old-new (from post 1 to post 2) values.
Blog Post 1
For the first blog post, we have the following data.
- Title = First Post
- Content = This is my first post
- Except = My Blog Post
Blog Post 2
For the second blog post, we have the following data.
- Title = Second Post
- Content = This is my second post
- My Blog Post
The only difference between them is their titles and contents. Let’s list out the difference using some Java codes.
Java Codes
BlogPost class
It is a Java Bean that represents a blog post. Two things worth highlighting in these codes.
- The class implements the Diffable interface
- The class overrides the
diff method
- This is where the magic happens
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | package com.turreta.apache.commons.lang.diffable; import org.apache.commons.lang3.builder.DiffBuilder; import org.apache.commons.lang3.builder.DiffResult; import org.apache.commons.lang3.builder.Diffable; import org.apache.commons.lang3.builder.ToStringStyle; public class BlogPost implements Diffable<BlogPost>{ private String title; private String content; private String excerpt; public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public String getExcerpt() { return excerpt; } public void setExcerpt(String excerpt) { this.excerpt = excerpt; } @Override public DiffResult diff(BlogPost obj) { return new DiffBuilder(this, obj, ToStringStyle.SHORT_PREFIX_STYLE) .append("title", this.title, obj.title) .append("content", this.content, obj.content) .append("excerpt", this.excerpt, obj.excerpt) .build(); } } |
DiffableDemo class
This is the main class – the entry point.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | package com.turreta.apache.commons.lang.diffable; import org.apache.commons.lang3.builder.Diff; import org.apache.commons.lang3.builder.DiffResult; public class DiffableDemo { public static void main(String[] args) { BlogPost postOne = new BlogPost(); postOne.setTitle("First Post"); postOne.setContent("This is my first post"); postOne.setExcerpt("My Blog Post"); BlogPost postTwo = new BlogPost(); postTwo.setTitle("Second Post"); postTwo.setContent("This is my second post"); postTwo.setExcerpt("My Blog Post"); DiffResult diff = postOne.diff(postTwo); for(Diff<?> d: diff.getDiffs()) { System.out.println(d.getFieldName() + "= FROM[" + d.getLeft() + "] TO [" + d.getRight() + "]"); } System.out.println(); } } |
This outputs:
1 2 | title= FROM[First Post] TO [Second Post] content= FROM[This is my first post] TO [This is my second post] |