diff --git a/core-java-modules/core-java-lang-6/pom.xml b/core-java-modules/core-java-lang-6/pom.xml index 53ef36a898..6561c4fdcc 100644 --- a/core-java-modules/core-java-lang-6/pom.xml +++ b/core-java-modules/core-java-lang-6/pom.xml @@ -18,7 +18,11 @@ mapstruct ${mapstruct.version} - + + org.apache.commons + commons-lang3 + ${commons-lang3.version} + diff --git a/core-java-modules/core-java-lang-6/src/main/java/com/baeldung/compareobjects/Address.java b/core-java-modules/core-java-lang-6/src/main/java/com/baeldung/compareobjects/Address.java new file mode 100644 index 0000000000..e82a1018ce --- /dev/null +++ b/core-java-modules/core-java-lang-6/src/main/java/com/baeldung/compareobjects/Address.java @@ -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); + } +} diff --git a/core-java-modules/core-java-lang-6/src/main/java/com/baeldung/compareobjects/Person.java b/core-java-modules/core-java-lang-6/src/main/java/com/baeldung/compareobjects/Person.java new file mode 100644 index 0000000000..6ba244ff17 --- /dev/null +++ b/core-java-modules/core-java-lang-6/src/main/java/com/baeldung/compareobjects/Person.java @@ -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 phoneNumbers; + @DiffExclude + private Address address; + + public Person(String firstName, String lastName, int age, List 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 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); + } +} diff --git a/core-java-modules/core-java-lang-6/src/main/java/com/baeldung/compareobjects/PersonDiffBuilder.java b/core-java-modules/core-java-lang-6/src/main/java/com/baeldung/compareobjects/PersonDiffBuilder.java new file mode 100644 index 0000000000..9e21f5d7c3 --- /dev/null +++ b/core-java-modules/core-java-lang-6/src/main/java/com/baeldung/compareobjects/PersonDiffBuilder.java @@ -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(); + } +} diff --git a/core-java-modules/core-java-lang-6/src/main/java/com/baeldung/compareobjects/PersonReflectionDiffBuilder.java b/core-java-modules/core-java-lang-6/src/main/java/com/baeldung/compareobjects/PersonReflectionDiffBuilder.java new file mode 100644 index 0000000000..82081e7282 --- /dev/null +++ b/core-java-modules/core-java-lang-6/src/main/java/com/baeldung/compareobjects/PersonReflectionDiffBuilder.java @@ -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(); + } +} diff --git a/core-java-modules/core-java-lang-6/src/main/java/com/baeldung/compareobjects/PhoneNumber.java b/core-java-modules/core-java-lang-6/src/main/java/com/baeldung/compareobjects/PhoneNumber.java new file mode 100644 index 0000000000..4ab2b4a4bd --- /dev/null +++ b/core-java-modules/core-java-lang-6/src/main/java/com/baeldung/compareobjects/PhoneNumber.java @@ -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); + } +} diff --git a/core-java-modules/core-java-lang-6/src/test/java/com/baeldung/compareobjects/PersonDiffBuilderUnitTest.java b/core-java-modules/core-java-lang-6/src/test/java/com/baeldung/compareobjects/PersonDiffBuilderUnitTest.java new file mode 100644 index 0000000000..417e9da0ea --- /dev/null +++ b/core-java-modules/core-java-lang-6/src/test/java/com/baeldung/compareobjects/PersonDiffBuilderUnitTest.java @@ -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 phoneNumbers1 = new ArrayList<>(); + phoneNumbers1.add(new PhoneNumber("home", "123-456-7890")); + phoneNumbers1.add(new PhoneNumber("work", "987-654-3210")); + + List 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 diff = PersonDiffBuilder.compare(person1, person2); + for (Diff d : diff.getDiffs()) { + System.out.println(d.getFieldName() + ": " + d.getLeft() + " != " + d.getRight()); + } + + assertFalse(diff.getDiffs() + .isEmpty()); + } +} diff --git a/core-java-modules/core-java-lang-6/src/test/java/com/baeldung/compareobjects/PersonReflectionDiffBuilderUnitTest.java b/core-java-modules/core-java-lang-6/src/test/java/com/baeldung/compareobjects/PersonReflectionDiffBuilderUnitTest.java new file mode 100644 index 0000000000..fffa56dccc --- /dev/null +++ b/core-java-modules/core-java-lang-6/src/test/java/com/baeldung/compareobjects/PersonReflectionDiffBuilderUnitTest.java @@ -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 phoneNumbers1 = new ArrayList<>(); + phoneNumbers1.add(new PhoneNumber("home", "123-456-7890")); + phoneNumbers1.add(new PhoneNumber("work", "987-654-3210")); + + List 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 diff = PersonReflectionDiffBuilder.compare(person1, person2); + for (Diff d : diff.getDiffs()) { + System.out.println(d.getFieldName() + ": " + d.getLeft() + " != " + d.getRight()); + } + + assertFalse(diff.getDiffs() + .isEmpty()); + } +}