Merge pull request #14720 from lajka/master

BAEL-6871: Using the Apache Commons Lang 3 for Comparing Objects in Java
This commit is contained in:
Vini 2023-09-29 08:33:32 +02:00 committed by GitHub
commit d00863eeb2
8 changed files with 271 additions and 1 deletions

View File

@ -18,7 +18,11 @@
<artifactId>mapstruct</artifactId>
<version>${mapstruct.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons-lang3.version}</version>
</dependency>
</dependencies>
<build>
<plugins>

View File

@ -0,0 +1,47 @@
package com.baeldung.compareobjects;
import java.util.Objects;
public class Address {
private String streetAddress;
private String city;
private String postalCode;
public Address(String streetAddress, String city, String postalCode) {
this.streetAddress = streetAddress;
this.city = city;
this.postalCode = postalCode;
}
public String getStreetAddress() {
return streetAddress;
}
public String getCity() {
return city;
}
public String getPostalCode() {
return postalCode;
}
@Override
public String toString() {
return "Address{" + "streetAddress='" + streetAddress + '\'' + ", city='" + city + '\'' + ", postalCode='" + postalCode + '\'' + '}';
}
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
Address address = (Address) o;
return Objects.equals(streetAddress, address.streetAddress) && Objects.equals(city, address.city) && Objects.equals(postalCode, address.postalCode);
}
@Override
public int hashCode() {
return Objects.hash(streetAddress, city, postalCode);
}
}

View File

@ -0,0 +1,58 @@
package com.baeldung.compareobjects;
import java.util.List;
import java.util.Objects;
import org.apache.commons.lang3.builder.DiffExclude;
public class Person {
private String firstName;
private String lastName;
private int age;
private List<PhoneNumber> phoneNumbers;
@DiffExclude
private Address address;
public Person(String firstName, String lastName, int age, List<PhoneNumber> phoneNumbers, Address address) {
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
this.phoneNumbers = phoneNumbers;
this.address = address;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public int getAge() {
return age;
}
public List<PhoneNumber> getPhoneNumbers() {
return phoneNumbers;
}
public Address getAddress() {
return address;
}
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
Person person = (Person) o;
return age == person.age && Objects.equals(firstName, person.firstName) && Objects.equals(lastName, person.lastName) && Objects.equals(phoneNumbers, person.phoneNumbers) && Objects.equals(address, person.address);
}
@Override
public int hashCode() {
return Objects.hash(firstName, lastName, age, phoneNumbers, address);
}
}

View File

@ -0,0 +1,32 @@
package com.baeldung.compareobjects;
import org.apache.commons.lang3.builder.DiffBuilder;
import org.apache.commons.lang3.builder.DiffResult;
import org.apache.commons.lang3.builder.ToStringStyle;
public class PersonDiffBuilder {
public static DiffResult compare(Person first, Person second) {
DiffBuilder diffBuilder = new DiffBuilder(first, second, ToStringStyle.DEFAULT_STYLE).append("person", first.getFirstName(), second.getFirstName())
.append("lastName", first.getLastName(), second.getLastName())
.append("streetAddress", first.getAddress()
.getStreetAddress(), second.getAddress()
.getStreetAddress())
.append("city", first.getAddress()
.getCity(), second.getAddress()
.getCity())
.append("postalCode", first.getAddress()
.getPostalCode(), second.getAddress()
.getPostalCode())
.append("age", first.getAge(), second.getAge());
for (int i = 0; i < first.getPhoneNumbers()
.size(); i++) {
diffBuilder.append("phoneNumbers[" + i + "].number", first.getPhoneNumbers()
.get(i)
.getNumber(), second.getPhoneNumbers()
.get(i)
.getNumber());
}
return diffBuilder.build();
}
}

View File

@ -0,0 +1,11 @@
package com.baeldung.compareobjects;
import org.apache.commons.lang3.builder.DiffResult;
import org.apache.commons.lang3.builder.ReflectionDiffBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
public class PersonReflectionDiffBuilder {
public static DiffResult compare(Person first, Person second) {
return new ReflectionDiffBuilder<>(first, second, ToStringStyle.SHORT_PREFIX_STYLE).build();
}
}

View File

@ -0,0 +1,43 @@
package com.baeldung.compareobjects;
import java.util.Objects;
import org.apache.commons.lang3.builder.DiffExclude;
public class PhoneNumber {
private String type;
private String number;
public PhoneNumber(String type, String number) {
this.type = type;
this.number = number;
}
public String getType() {
return type;
}
public String getNumber() {
return number;
}
@Override
public String toString() {
return "PhoneNumber{" + "type='" + type + '\'' + ", number='" + number + '\'' + '}';
}
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
PhoneNumber that = (PhoneNumber) o;
return Objects.equals(number, that.number);
}
@Override
public int hashCode() {
return Objects.hash(number);
}
}

View File

@ -0,0 +1,38 @@
package com.baeldung.compareobjects;
import static org.junit.jupiter.api.Assertions.assertFalse;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang3.builder.Diff;
import org.apache.commons.lang3.builder.DiffResult;
import org.junit.jupiter.api.Test;
public class PersonDiffBuilderUnitTest {
@Test
void givenTwoPeopleDifferent_whenComparingWithDiffBuilder_thenDifferencesFound() {
List<PhoneNumber> phoneNumbers1 = new ArrayList<>();
phoneNumbers1.add(new PhoneNumber("home", "123-456-7890"));
phoneNumbers1.add(new PhoneNumber("work", "987-654-3210"));
List<PhoneNumber> phoneNumbers2 = new ArrayList<>();
phoneNumbers2.add(new PhoneNumber("mobile1", "123-456-7890"));
phoneNumbers2.add(new PhoneNumber("mobile2", "987-654-3210"));
Address address1 = new Address("123 Main St", "London", "12345");
Address address2 = new Address("123 Main St", "Paris", "54321");
Person person1 = new Person("John", "Doe", 30, phoneNumbers1, address1);
Person person2 = new Person("Jane", "Smith", 28, phoneNumbers2, address2);
DiffResult<Person> diff = PersonDiffBuilder.compare(person1, person2);
for (Diff<?> d : diff.getDiffs()) {
System.out.println(d.getFieldName() + ": " + d.getLeft() + " != " + d.getRight());
}
assertFalse(diff.getDiffs()
.isEmpty());
}
}

View File

@ -0,0 +1,37 @@
package com.baeldung.compareobjects;
import static org.junit.jupiter.api.Assertions.assertFalse;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang3.builder.Diff;
import org.apache.commons.lang3.builder.DiffResult;
import org.junit.jupiter.api.Test;
public class PersonReflectionDiffBuilderUnitTest {
@Test
void givenTwoPeopleDifferent_whenComparingWithReflectionDiffBuilder_thenDifferencesFound() {
List<PhoneNumber> phoneNumbers1 = new ArrayList<>();
phoneNumbers1.add(new PhoneNumber("home", "123-456-7890"));
phoneNumbers1.add(new PhoneNumber("work", "987-654-3210"));
List<PhoneNumber> phoneNumbers2 = new ArrayList<>();
phoneNumbers2.add(new PhoneNumber("mobile1", "123-456-7890"));
phoneNumbers2.add(new PhoneNumber("mobile2", "987-654-3210"));
Address address1 = new Address("123 Main St", "London", "12345");
Address address2 = new Address("123 Main St", "Paris", "54321");
Person person1 = new Person("John", "Doe", 30, phoneNumbers1, address1);
Person person2 = new Person("Jane", "Smith", 28, phoneNumbers2, address2);
DiffResult<Person> diff = PersonReflectionDiffBuilder.compare(person1, person2);
for (Diff<?> d : diff.getDiffs()) {
System.out.println(d.getFieldName() + ": " + d.getLeft() + " != " + d.getRight());
}
assertFalse(diff.getDiffs()
.isEmpty());
}
}