Comparator

@aljunggren aljunggren authored on 6 Jul 2021
src Added declaring class of diff item 5 years ago
.gitignore Initial commit 5 years ago
README.md Added declaring class of diff item 5 years ago
lombok.config Initial commit 5 years ago
pom.xml Added reflection-utils dependency and embedded object handling 5 years ago
README.md

Comparator

Compare objects by annotated member variables. Non-annotated members will be ignored.

public class User {
    @Comparable
    private String name;
}

Instantiate

Comparator comparator = new Comparator(object1, object2);

Is Equal

boolean equal = comparator.isEqual()

Differences

List<Diff> diffs = comparator.compare();

Diff

  • name - Field name
  • value1 - value of object1 field
  • value2 - value of object2 field
  • clazz - Class containing the field that has a diff. This is useful for embedded objects.

Embedded Objects

Objects other than boxed types and Strings can be marked as @Comparable.

public class User {
    @Comparable
    private Address address;
}

public class Address {
    @Comparable
    private String city;
    private String state;
}

Exceptions

A runtime ComparatorException will be thrown if two object of different classes are being compared.

TODO

Add Collection, Array, and Map handling

Dependencies

  • reflection-utils