[rajkishorraj185@gmail.com] Lombok equalsandhashcode annotation (#13717)

* example code for equalsandhashcode annotation

* moving codes to lombok-2

---------

Co-authored-by: raj kishor <raj.kishor@smartcoin.co.in>
This commit is contained in:
Raj Kishor 2023-05-08 09:33:17 +05:30 committed by GitHub
parent 7f15d46d00
commit a673cea33d
26 changed files with 608 additions and 0 deletions

View File

@ -0,0 +1,14 @@
package com.baeldung.lombok.equalsandhashcode;
import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.Getter;
@Getter
@EqualsAndHashCode
@AllArgsConstructor
public class Employee {
private String name;
private int id;
private int age;
}

View File

@ -0,0 +1,57 @@
package com.baeldung.lombok.equalsandhashcode;
import lombok.Getter;
@Getter
public class EmployeeDelomboked {
private String name;
private int id;
private int age;
public EmployeeDelomboked() {
}
public boolean equals(Object o) {
if (o == this) {
return true;
} else if (!(o instanceof Employee)) {
return false;
} else {
Employee other = (Employee) o;
if (!other.canEqual(this)) {
return false;
} else if (this.getId() != other.getId()) {
return false;
} else if (this.getAge() != other.getAge()) {
return false;
} else {
Object this$name = this.getName();
Object other$name = other.getName();
if (this$name == null) {
if (other$name == null) {
return true;
}
} else if (this$name.equals(other$name)) {
return true;
}
return false;
}
}
}
protected boolean canEqual(Object other) {
return other instanceof Employee;
}
public int hashCode() {
final int PRIME = 59;
int result = 1;
result = result * PRIME + this.getId();
result = result * PRIME + this.getAge();
Object $name = this.getName();
result = result * PRIME + ($name == null ? 43 : $name.hashCode());
return result;
}
}

View File

@ -0,0 +1,20 @@
package com.baeldung.lombok.equalsandhashcode.commonissue;
import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
@Getter
@Setter
@EqualsAndHashCode
@AllArgsConstructor
@NoArgsConstructor
public class Employee {
private String name;
private int id;
private int age;
private Manager manager;
}

View File

@ -0,0 +1,17 @@
package com.baeldung.lombok.equalsandhashcode.commonissue;
import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
@EqualsAndHashCode(exclude = "manager")
@AllArgsConstructor
public class EmployeeV2 {
private String name;
private int id;
private int age;
private ManagerV2 manager;
}

View File

@ -0,0 +1,16 @@
package com.baeldung.lombok.equalsandhashcode.commonissue;
import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
@EqualsAndHashCode
@AllArgsConstructor
public class Manager {
private String name;
private Employee assistantManager;
}

View File

@ -0,0 +1,16 @@
package com.baeldung.lombok.equalsandhashcode.commonissue;
import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
@EqualsAndHashCode
@AllArgsConstructor
public class ManagerV2 {
private String name;
private EmployeeV2 assistantManager;
}

View File

@ -0,0 +1,15 @@
package com.baeldung.lombok.equalsandhashcode.exclude.classlevel;
import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.Getter;
@Getter
@EqualsAndHashCode(exclude = {"id", "age"})
@AllArgsConstructor
public class Employee {
private String name;
private int id;
private int age;
}

View File

@ -0,0 +1,37 @@
package com.baeldung.lombok.equalsandhashcode.exclude.classlevel;
import lombok.Getter;
@Getter
public class EmployeeDelomboked {
private String name;
private int id;
private int age;
@Override
public boolean equals(final Object o) {
if (o == this) return true;
if (!(o instanceof Employee)) return false;
final Employee other = (Employee) o;
if (!other.canEqual((Object) this)) return false;
final Object this$name = this.getName();
final Object other$name = other.getName();
if (this$name == null ? other$name != null : !this$name.equals(other$name)) return false;
return true;
}
protected boolean canEqual(final Object other) {
return other instanceof Employee;
}
@Override
public int hashCode() {
final int PRIME = 59;
int result = 1;
final Object $name = this.getName();
result = result * PRIME + ($name == null ? 43 : $name.hashCode());
return result;
}
}

View File

@ -0,0 +1,18 @@
package com.baeldung.lombok.equalsandhashcode.exclude.fieldlevel;
import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.Getter;
@Getter
@EqualsAndHashCode
@AllArgsConstructor
public class Employee {
private String name;
@EqualsAndHashCode.Exclude
private int id;
@EqualsAndHashCode.Exclude
private int age;
}

View File

@ -0,0 +1,31 @@
package com.baeldung.lombok.equalsandhashcode.exclude.fieldlevel;
import lombok.Getter;
@Getter
public class EmployeeDelomboked {
private String name;
private int id;
private int age;
public boolean equals(final Object o) {
if (o == this) return true;
if (!(o instanceof com.baeldung.lombok.equalsandhashcode.exclude.fieldlevel.Employee)) return false;
final com.baeldung.lombok.equalsandhashcode.exclude.fieldlevel.Employee other = (com.baeldung.lombok.equalsandhashcode.exclude.fieldlevel.Employee) o;
if (!other.canEqual((Object) this)) return false;
final Object this$name = this.getName();
final Object other$name = other.getName();
if (this$name == null ? other$name != null : !this$name.equals(other$name)) return false;
return true;
}
public int hashCode() {
final int PRIME = 59;
int result = 1;
final Object $name = this.getName();
result = result * PRIME + ($name == null ? 43 : $name.hashCode());
return result;
}
}

View File

@ -0,0 +1,15 @@
package com.baeldung.lombok.equalsandhashcode.include.classlevel;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
@Data
@EqualsAndHashCode(of = {"name", "id"})
@AllArgsConstructor
public class Employee {
private String name;
private int id;
private int age;
}

View File

@ -0,0 +1,18 @@
package com.baeldung.lombok.equalsandhashcode.include.fieldlevel;
import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.Getter;
@Getter
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
@AllArgsConstructor
public class Employee {
@EqualsAndHashCode.Include
private String name;
@EqualsAndHashCode.Include
private int id;
private int age;
}

View File

@ -0,0 +1,20 @@
package com.baeldung.lombok.equalsandhashcode.include.methodlevel;
import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.Getter;
@Getter
@EqualsAndHashCode
@AllArgsConstructor
public class Employee {
private String name;
private int id;
private int age;
@EqualsAndHashCode.Include
public boolean hasOddId() {
return id % 2 != 0;
}
}

View File

@ -0,0 +1,40 @@
package com.baeldung.lombok.equalsandhashcode.include.methodlevel;
import lombok.Getter;
@Getter
public class EmployeeDelomboked {
private String name;
private int id;
private int age;
public boolean hasOddId() {
return id % 2 != 0;
}
public boolean equals(final Object o) {
if (o == this) return true;
if (!(o instanceof Employee)) return false;
final Employee other = (Employee) o;
if (!other.canEqual((Object) this)) return false;
if (this.getId() != other.getId()) return false;
if (this.getAge() != other.getAge()) return false;
if (this.hasOddId() != other.hasOddId()) return false;
final Object this$name = this.getName();
final Object other$name = other.getName();
if (this$name == null ? other$name != null : !this$name.equals(other$name)) return false;
return true;
}
public int hashCode() {
final int PRIME = 59;
int result = 1;
result = result * PRIME + this.getId();
result = result * PRIME + this.getAge();
result = result * PRIME + (this.hasOddId() ? 79 : 97);
final Object $name = this.getName();
result = result * PRIME + ($name == null ? 43 : $name.hashCode());
return result;
}
}

View File

@ -0,0 +1,16 @@
package com.baeldung.lombok.equalsandhashcode.inheritance;
import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
@EqualsAndHashCode
@AllArgsConstructor
public class Employee {
private String name;
private int id;
private int age;
}

View File

@ -0,0 +1,20 @@
package com.baeldung.lombok.equalsandhashcode.inheritance;
import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
@EqualsAndHashCode(callSuper = true)
public class Manager extends Employee {
private String departmentName;
private int uid;
public Manager(String departmentName, int uid, String name, int id, int age) {
super(name, id, age);
this.departmentName = departmentName;
this.uid = uid;
}
}

View File

@ -0,0 +1,37 @@
package com.baeldung.lombok.equalsandhashcode.inheritance;
import lombok.Getter;
@Getter
public class ManagerDelomboked extends Employee {
private String departmentName;
private int uid;
public ManagerDelomboked(String departmentName, int uid, String name, int id, int age) {
super(name, id, age);
this.departmentName = departmentName;
this.uid = uid;
}
public boolean equals(final Object o) {
if (o == this) return true;
if (!(o instanceof com.baeldung.lombok.equalsandhashcode.inheritance.Manager)) return false;
final com.baeldung.lombok.equalsandhashcode.inheritance.Manager other = (com.baeldung.lombok.equalsandhashcode.inheritance.Manager) o;
if (!other.canEqual((Object) this)) return false;
if (!super.equals(o)) return false;
if (this.getUid() != other.getUid()) return false;
final Object this$departmentName = this.getDepartmentName();
final Object other$departmentName = other.getDepartmentName();
if (this$departmentName == null ? other$departmentName != null : !this$departmentName.equals(other$departmentName)) return false;
return true;
}
public int hashCode() {
final int PRIME = 59;
int result = super.hashCode();
result = result * PRIME + this.getUid();
final Object $departmentName = this.getDepartmentName();
result = result * PRIME + ($departmentName == null ? 43 : $departmentName.hashCode());
return result;
}
}

View File

@ -0,0 +1,19 @@
package com.baeldung.lombok.equalsandhashcode.inheritance;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
@EqualsAndHashCode(callSuper = false)
public class ManagerV2 extends Employee {
private String departmentName;
private int uid;
public ManagerV2(String departmentName, int uid, String name, int id, int age) {
super(name, id, age);
this.departmentName = departmentName;
this.uid = uid;
}
}

View File

@ -0,0 +1,20 @@
package com.baeldung.lombok.equalsandhashcode;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
public class EqualsAndHashCodeUnitTest {
@Test
public void whenUsingEqualsAndHashCodeOnClassLevel_thenTwoObjectsAreEqualAndHaveSameHashCodeIfAllFieldsAreSame() {
Employee employee = new Employee("Nimi", 15, 22);
Employee employeeTwo = new Employee("Nimi", 15, 22);
Employee employeeThree = new Employee("Mannat", 15, 22);
assertEquals(employee, employeeTwo);
assertNotEquals(employee, employeeThree);
assertEquals(employee.hashCode(), employeeTwo.hashCode());
assertNotEquals(employee.hashCode(), employeeThree.hashCode());
}
}

View File

@ -0,0 +1,34 @@
package com.baeldung.lombok.equalsandhashcode.commonissue;
import org.junit.Test;
public class EqualsAndHashCodeUnitTest {
@Test(expected = StackOverflowError.class)
public void whenUsingEqualsAndHashInObjectWithCircularDependency_thenCallingEqualsAndHasCodeWillThrowStackOverFlowError() {
Manager manager = new Manager("Mannat", null);
Employee employee = new Employee("Nimi", 15, 22, manager);
manager.setAssistantManager(employee);
employee.setManager(manager);
int hash = employee.hashCode();
}
@Test
public void whenUsingEqualsAndHashInObjectAndExcludingCircularDependencyField_thenNoStackOverFlowError() {
ManagerV2 manager = new ManagerV2("Mannat", null);
EmployeeV2 employee = new EmployeeV2("Nimi", 15, 22, manager);
manager.setAssistantManager(employee);
employee.setManager(manager);
int hash = employee.hashCode();
}
}

View File

@ -0,0 +1,17 @@
package com.baeldung.lombok.equalsandhashcode.exclude.classlevel;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class EqualsAndHashCodeUnitTest {
@Test
public void whenUsingEqualsAndHashCodeOnClassLevelAndExcludingFields_thenTwoObjectsAreEqualAndHaveSameHashCodeEvenIfExcludedFieldsAreNotSame() {
Employee employee = new Employee("Nimi", 15, 22);
Employee employeeTwo = new Employee("Nimi", 17, 21);
assertEquals(employee, employeeTwo);
assertEquals(employee.hashCode(), employeeTwo.hashCode());
}
}

View File

@ -0,0 +1,18 @@
package com.baeldung.lombok.equalsandhashcode.exclude.fieldLevel;
import com.baeldung.lombok.equalsandhashcode.exclude.fieldlevel.Employee;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class EqualsAndHashCodeUnitTest {
@Test
public void whenUsingEqualsAndHashCodeWithExcludingFields_thenTwoObjectsAreEqualAndHaveSameHashCodeEvenIfExcludedFieldsAreNotSame() {
Employee employee = new Employee("Nimi", 15, 22);
Employee employeeTwo = new Employee("Nimi", 17, 21);
assertEquals(employee, employeeTwo);
assertEquals(employee.hashCode(), employeeTwo.hashCode());
}
}

View File

@ -0,0 +1,21 @@
package com.baeldung.lombok.equalsandhashcode.include.classlevel;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class EqualsAndHashCodeUnitTest {
@Test
public void whenUsingEqualsAndHashCodeOnClassLevelAndIncludeFields_thenTwoObjectsAreEqualAndHaveSameHashCodeIfIncludedFieldsAreSame() {
Employee employee = new Employee("Nimi", 15, 22);
Employee employeeTwo = new Employee("Nimi", 15, 22);
Employee employeeThree = new Employee("Nimi", 15, 23);
assertEquals(employee, employeeTwo);
assertEquals(employee, employeeThree);
assertEquals(employee.hashCode(), employeeTwo.hashCode());
assertEquals(employee.hashCode(), employeeThree.hashCode());
}
}

View File

@ -0,0 +1,21 @@
package com.baeldung.lombok.equalsandhashcode.include.fieldlevel;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class EqualsAndHashCodeUnitTest {
@Test
public void whenUsingEqualsAndHashCodeOnFieldLevel_thenTwoObjectsAreEqualAndHaveSameHashCodeIfIncludedFieldsAreSame() {
Employee employee = new Employee("Nimi", 15, 22);
Employee employeeTwo = new Employee("Nimi", 15, 22);
Employee employeeThree = new Employee("Nimi", 15, 23);
assertEquals(employee, employeeTwo);
assertEquals(employee, employeeThree);
assertEquals(employee.hashCode(), employeeTwo.hashCode());
assertEquals(employee.hashCode(), employeeThree.hashCode());
}
}

View File

@ -0,0 +1,22 @@
package com.baeldung.lombok.equalsandhashcode.include.methodlevel;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
public class EqualsAndHashCodeUnitTest {
@Test
public void whenUsingEqualsAndHashCodeOnMethodLevel_thenTwoObjectsAreEqualAndHaveSameHashCodeIfMethodsReturnSameValue() {
Employee employee = new Employee("Nimi", 15, 22);
Employee employeeTwo = new Employee("Nimi", 15, 22);
Employee employeeThree = new Employee("Nimi", 15, 23);
assertEquals(employee, employeeTwo);
assertNotEquals(employee, employeeThree);
assertEquals(employee.hashCode(), employeeTwo.hashCode());
assertNotEquals(employee.hashCode(), employeeThree.hashCode());
}
}

View File

@ -0,0 +1,29 @@
package com.baeldung.lombok.equalsandhashcode.inheritance;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
public class EqualsAndHashCodeUnitTest {
@Test
public void whenUsingEqualsAndHashCodeAndCallSuperIsTrue_thenTwoObjectsAreEqualAndHaveSameHashCodeIfAllFieldsIncludingParentsFieldsAreSame() {
Manager manager = new Manager("Opto", 444, "Nimi", 15, 22);
Manager managerTwo = new Manager("Opto", 444, "Nimi", 15, 22);
Manager managerThree = new Manager("Opto", 444, "Nimi", 15, 23);
assertEquals(manager, managerTwo);
assertNotEquals(managerTwo, managerThree);
}
@Test
public void whenUsingEqualsAndHashCodeAndCallSuperIsFalse_thenTwoObjectsAreEqualAndHaveSameHashCodeEvenIfAllParentsFieldsAreNotSame() {
ManagerV2 manager = new ManagerV2("Opto", 444, "Nimi", 15, 22);
ManagerV2 managerTwo = new ManagerV2("Opto", 444, "Nimi", 15, 22);
ManagerV2 managerThree = new ManagerV2("Opto", 444, "Nimi", 14, 21);
assertEquals(manager, managerTwo);
assertEquals(managerTwo, managerThree);
}
}