Entity Comparator
A simple Comparator
which lets you compare objects by dynamically choosing the properties to compare.
Please note: if you are using Java 8 or later, please consider the standard Comparator
class.
Get it
Add the latest version dependency to your POM:
<dependency>
<groupId>com.github.carlopantaleo</groupId>
<artifactId>entity-comparator</artifactId>
<version>1.0.1</version>
</dependency>
Example usage
public class Example {
private String field1;
private String field2;
// Getters and setters
// ...
}
Example ex1 = new Example();
ex1.setField1("a");
ex1.setField2("a");
Example ex2 = new Example();
ex2.setField1("a");
ex2.setField2("b");
EntityComparator<Example> ec = new EntityComparator<>(Example.class);
ec.addComparingProperty("field1");
assertEquals(0, ec.compare(ex1, ex2));
ec.addComparingProperty("field2");
assertEquals(-1, ec.compare(ex1, ex2));