It took me quite a while to understand “AccountRepository.java line 7-47”, how much time do you get it? ;)
The code wants to make sure no duplicate elements would be added into “List<PjpRoleMapping> roleMapping”. When a qualified Java programmer gets the purpose, he/she would know that the code was implemented in a wrong way.
Two simple facts:
If you don’t want duplicate elements in a “Collection“, use “Set“;
If you want to compare 2 instances of a class, implement your own “equals()/hashCode()“ methods.
@Override publicinthashCode() { finalintprime=31; intresult=1; result = prime * result + ((pjpProject == null) ? 0 : pjpProject.hashCode()); result = prime * result + ((pjpRole == null) ? 0 : pjpRole.hashCode()); result = prime * result + ((pjpTenant == null) ? 0 : pjpTenant.hashCode()); result = prime * result + ((pjpUser == null) ? 0 : pjpUser.hashCode()); return result; }
@Override publicbooleanequals(Object obj) { if (this == obj) returntrue; if (obj == null) returnfalse; if (getClass() != obj.getClass()) returnfalse; PjpRoleMappingother= (PjpRoleMapping) obj; if (pjpProject == null) { if (other.pjpProject != null) returnfalse; } elseif (!pjpProject.equals(other.pjpProject)) returnfalse; if (pjpRole == null) { if (other.pjpRole != null) returnfalse; } elseif (!pjpRole.equals(other.pjpRole)) returnfalse; if (pjpTenant == null) { if (other.pjpTenant != null) returnfalse; } elseif (!pjpTenant.equals(other.pjpTenant)) returnfalse; if (pjpUser == null) { if (other.pjpUser != null) returnfalse; } elseif (!pjpUser.equals(other.pjpUser)) returnfalse; returntrue; }