Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Opt out a property for equals/hashCode #54

Closed
fluentfuture opened this issue Mar 18, 2015 · 3 comments · Fixed by #412
Closed

Opt out a property for equals/hashCode #54

fluentfuture opened this issue Mar 18, 2015 · 3 comments · Fixed by #412

Comments

@fluentfuture
Copy link

I would like the ability to define a value class where a certain field isn't included in equality.

For example, I recently came to a need to define a helper class that wraps the input object T and its original index in the input List.

IndexedFoo {
  int index();
  Foo foo();
}

Multimap<IndexedFoo, Foo> overlappings = ...;

I want IndexedFoo's equality to be based on the index, but not the Foo object.

@alicederyn
Copy link
Collaborator

Just a note: dropping fields from equality makes it very easy to write broken unit tests (and hard to write good ones). If you find yourself wanting this feature, consider first whether you actually want a different collection type (typically a Map rather than a Set, for instance), or if you can use an explicit field-ignoring Comparator in the parts of the code that need it.

Otherwise, 👍 this issue, and consider adding a comment explaining your situation!

@thespags
Copy link
Contributor

thespags commented Apr 3, 2019

@alicederyn sounds like you aren't a fan of the premise but are okay with the feature. In other words, someone could provide a PR for this feature that could be accepted.

For a situation I find myself in:
I have a use case for a lazy loaded graph where the Graph is an attribute of my POJOs for traversing relationships. However two given nodes should be equal even if their Graph attributes aren't in the same state.

@alicederyn
Copy link
Collaborator

Thank you @thespags for implementing this one, now available in v2.4.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants