posted 1 year ago
As Campbell pointed out, there's a lot of wonkiness here.
1) You can't give different equality definitions for sub-classes. Override equals() once and make it final. The implementation should be based on the current class, not sub-classes. Use instanceof, not getClass().
2) equals() should account for ALL normalized properties of an object, not just a subset. If you want to compare a subset of an object's properties, don't use equals().
3) bytes are not Privileges. You're comparing apples to oranges.
4) It looks like you're using Project Lombok. Stop it. Lombok is a terrible substitute for writing proper type-safe code.
5) Most importantly, don't base your business logic on mutable DTOs. A DTO is only there to facilitate transferring data to and from the database. Convert it to an immutable instance of your business model, which will also contain the equals() implementation.
Now, even though Privilege has a correct equals() implementation, DON'T use it to compare privileges if you're only interested in the IDs of the privileges. Do this instead: