Thursday, 15 March 2018

Compare two beans with DiffBuilder

Would you like to compare properties of two, not related beans? Are you tired of using Comparable interface from Java ? You might use a class called DiffBuilder which is a part of Apache Commons !

DiffResult diffResult = new DiffBuilder(firstBean, secondBean, ToStringStyle.SHORT_PREFIX_STYLE)
                        .append("id", firstBean.getId(), secondBean.getId())
                        .append("type", firstBean.getType(), secondBean.getType())
                        .append("conditionName", firstBean.getOther(), secondBean.getOther())

boolean hasDifference = diffResult.getNumberOfDiffs() != 0


Reference : [1] DiffBuilder - Apache Commons