Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 02b7b68

Browse files
committed
CollectionItemElementSelector now returns a constant hashCode
The ElementSelector `hashCode` is only needed to use it as key in a Map. With introduction of the `IdentityStrategy` this adds an unnecessary source of confusion and complexity when implementing custom IdentityStrategies. To avoid this, returning a constant hashCode seems like a small price to pay. Yes, it may have a small performance impact, but we can still optimize when that turns out to be a problem.
1 parent 678dbfd commit 02b7b68

File tree

3 files changed

+24
-7
lines changed

3 files changed

+24
-7
lines changed

‎CHANGELOG.md‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
11
# Changelog
22

3+
## 0.92.2
4+
5+
- **ElementSelector:** `CollectionItemElementSelector` now returns a constant hashCode
6+
7+
...instead of the one of its reference item.
8+
9+
The ElementSelector `hashCode` is only needed to use it as key in a
10+
Map. With introduction of the `IdentityStrategy` this adds an
11+
unnecessary source of confusion and complexity when implementing custom
12+
IdentityStrategies.
13+
14+
To avoid this, returning a constant hashCode seems like a small price
15+
to pay. Yes, it may have a small performance impact, but we can still
16+
optimize when that turns out to be a problem.
17+
318
## 0.92.1
419

520
- **Feature:** Allow for checking field level annotations via DiffNode (Thanks [@NagyGa1](https://github.com/NagyGa1)) [#134]

‎src/main/java/de/danielbechler/diff/selector/CollectionItemElementSelector.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,6 @@ public boolean equals(final Object o)
7171
@Override
7272
public int hashCode()
7373
{
74-
return item != null ? item.hashCode() : 0;
74+
return 31;
7575
}
7676
}

‎src/test/java/de/danielbechler/diff/selector/CollectionItemElementSelectorTest.groovy‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package de.danielbechler.diff.selector
22

33
import spock.lang.Specification
4-
54
/**
65
* @author Daniel Bechler
76
*/
@@ -37,13 +36,16 @@ class CollectionItemElementSelectorTest extends Specification {
3736
!element.equals(null)
3837
}
3938

40-
def 'should have same hashCode as item'() {
41-
setup:
42-
def item = "foo"
43-
def element = new CollectionItemElementSelector(item)
39+
def 'should have constant hashCode'() {
40+
// NOTE(@SQiShER): In this case the hashCode is only used to use the ElementSelector
41+
// as key in a Map. With introduction of the IdentityStrategy this adds an unnecessary
42+
// source of confusion and complexity when implementing custom IdentityStrategies. To
43+
// avoid this, returning a constant hashCode seems like a small fee to pay. Yes, it may
44+
// have a small performance impact, but we can still optimize when it turns out to
45+
// actually be a problem.
4446

4547
expect:
46-
element.hashCode() == item.hashCode()
48+
newCollectionItemElementSelector('foo').hashCode() == 31
4749
}
4850

4951
def 'should provide accessor for item'() {

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /