Made hashCode() implement satisfy the Object.hashCode() contract, i.e., a.equals(b) implies a.hashCode() == b.hashCode()

Former-commit-id: 9f347a57b740b572d2d6a9a9e523de44e384773e
This commit is contained in:
Dale Visser
2015-06-24 14:41:20 -04:00
parent d76799cfd0
commit e8353089f3
3 changed files with 19 additions and 5 deletions

2
.gitignore vendored
View File

@@ -1,4 +1,6 @@
*/target/**
# IntelliJ test run side-effects
dependency-check-core/data/
# Intellij project files
*.iml
*.ipr

View File

@@ -26,6 +26,9 @@ import java.io.Serializable;
*/
public class Evidence implements Serializable, Comparable<Evidence> {
public static final int MAGIC_HASH_INIT_VALUE = 3;
public static final int MAGIC_HASH_MULTIPLIER = 67;
/**
* Creates a new Evidence object.
*/
@@ -176,11 +179,11 @@ public class Evidence implements Serializable, Comparable<Evidence> {
*/
@Override
public int hashCode() {
int hash = 3;
hash = 67 * hash + (this.name != null ? this.name.hashCode() : 0);
hash = 67 * hash + (this.source != null ? this.source.hashCode() : 0);
hash = 67 * hash + (this.value != null ? this.value.hashCode() : 0);
hash = 67 * hash + (this.confidence != null ? this.confidence.hashCode() : 0);
int hash = MAGIC_HASH_INIT_VALUE;
hash = MAGIC_HASH_MULTIPLIER * hash + (this.name != null ? this.name.toLowerCase().hashCode() : 0);
hash = MAGIC_HASH_MULTIPLIER * hash + (this.source != null ? this.source.toLowerCase().hashCode() : 0);
hash = MAGIC_HASH_MULTIPLIER * hash + (this.value != null ? this.value.toLowerCase().hashCode() : 0);
hash = MAGIC_HASH_MULTIPLIER * hash + (this.confidence != null ? this.confidence.hashCode() : 0);
return hash;
}

View File

@@ -19,6 +19,7 @@ package org.owasp.dependencycheck.dependency;
import org.junit.Test;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
/**
*
@@ -53,6 +54,14 @@ public class EvidenceTest {
assertTrue(instance.equals(that8));
}
@Test
public void testHashcodeContract() throws Exception {
final Evidence titleCase = new Evidence("Manifest", "Implementation-Title", "Spring Framework", Confidence.HIGH);
final Evidence lowerCase = new Evidence("manifest", "implementation-title", "spring framework", Confidence.HIGH);
assertThat(titleCase, is(equalTo(lowerCase)));
assertThat(titleCase.hashCode(), is(equalTo(lowerCase.hashCode())));
}
/**
* Test of compareTo method, of class Evidence.
*/