Skip to content

Commit 4635656

Browse files
committed
Merge pull request DozerMapper#127 from MakotoTheKnight/master
Modified CacheKey's equals method to handle null comparisons.
2 parents 2edfe3f + 736c775 commit 4635656

File tree

3 files changed

+36
-14
lines changed

3 files changed

+36
-14
lines changed

core/src/main/java/org/dozer/cache/CacheKeyFactory.java

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -55,29 +55,38 @@ private CacheKey(Class<?> srcClass, Class<?> destClass) {
5555
}
5656

5757
@Override
58-
public boolean equals(Object o) {
59-
CacheKey cacheKey = (CacheKey) o;
60-
61-
if (destClass != null ? !destClass.equals(cacheKey.destClass) : cacheKey.destClass != null)
58+
public boolean equals(final Object o) {
59+
if(this == o) {
60+
return true;
61+
}
62+
if (o == null || getClass() != o.getClass()) {
6263
return false;
63-
if (srcClass != null ? !srcClass.equals(cacheKey.srcClass) : cacheKey.srcClass != null)
64+
}
65+
final CacheKey cacheKey = (CacheKey) o;
66+
if (destClass != null ? !destClass.equals(cacheKey.destClass) : cacheKey.destClass != null) {
6467
return false;
65-
if (mapId != null ? !mapId.equals(cacheKey.mapId) : cacheKey.mapId != null)
68+
}
69+
if (srcClass != null ? !srcClass.equals(cacheKey.srcClass) : cacheKey.srcClass != null) {
6670
return false;
67-
71+
}
72+
if (mapId != null ? !mapId.equals(cacheKey.mapId) : cacheKey.mapId != null) {
73+
return false;
74+
}
6875
return true;
6976
}
7077

78+
7179
@Override
7280
public int hashCode() {
73-
int result;
74-
result = (srcClass != null ? srcClass.hashCode() : 0);
75-
result = 31 * result + (destClass != null ? destClass.hashCode() : 0);
76-
result = 31 * result + (mapId != null ? mapId.hashCode() : 0);
77-
return result;
81+
int result;
82+
result = (srcClass != null ? srcClass.hashCode() : 0);
83+
result = 31 * result + (destClass != null ? destClass.hashCode() : 0);
84+
result = 31 * result + (mapId != null ? mapId.hashCode() : 0);
85+
return result;
7886
}
7987

80-
@Override
88+
89+
@Override
8190
public String toString() {
8291
return ReflectionToStringBuilder.toString(this, ToStringStyle.MULTI_LINE_STYLE);
8392
}

core/src/test/java/org/dozer/cache/CacheKeyFactoryTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,17 @@ public void testCreateKey_AllParameters() throws Exception {
5353
assertFalse(cacheKey.hashCode() == cacheKey2.hashCode());
5454
}
5555

56+
@Test
57+
public void testCreateKey_handlesNullGracefullyInEquals() throws Exception {
58+
Object nullCacheKey = null;
59+
Object normalCacheKey = CacheKeyFactory.createKey(String.class, Long.class);
60+
61+
assertFalse("Null isn't handled properly!", normalCacheKey.equals(nullCacheKey));
62+
}
63+
64+
@Test
65+
public void testCreateKey_equivalenceOfSelf() throws Exception {
66+
Object cacheKey = CacheKeyFactory.createKey(String.class, Long.class);
67+
assertTrue("Key isn't equivalent to self!", cacheKey.equals(cacheKey));
68+
}
5669
}

sf-migration/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
<parent>
2525
<groupId>net.sf.dozer</groupId>
2626
<artifactId>dozer-parent</artifactId>
27-
<version>5.4.0-SNAPSHOT</version>
27+
<version>5.5.0-SNAPSHOT</version>
2828
</parent>
2929

3030
<artifactId>sf-migration</artifactId>

0 commit comments

Comments
 (0)