Override hashCode when equals overridden

Java 8 added a warning for this:

https://bugs.openjdk.java.net/browse/JDK-6563143
This commit is contained in:
Andrew Gaul 2014-05-20 21:12:15 -07:00
parent 4943ff9341
commit 1f1f4f0a49
4 changed files with 30 additions and 0 deletions

View File

@ -52,6 +52,10 @@ public class ImagePredicates {
return obj instanceof Is64BitPredicate; return obj instanceof Is64BitPredicate;
} }
@Override
public int hashCode() {
return 0;
}
} }
/** /**

View File

@ -30,6 +30,7 @@ import java.util.concurrent.atomic.AtomicReference;
import org.testng.annotations.BeforeTest; import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import com.google.common.base.Objects;
import com.google.common.base.Optional; import com.google.common.base.Optional;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import com.google.common.util.concurrent.Atomics; import com.google.common.util.concurrent.Atomics;
@ -132,6 +133,11 @@ public class GsonExperimentsTest {
return false; return false;
} }
} }
@Override
public int hashCode() {
return Objects.hashCode(present, notPresent);
}
} }
public void testPersistOptional() { public void testPersistOptional() {

View File

@ -138,6 +138,11 @@ public final class DeserializationConstructorAndReflectiveTypeAdapterFactoryTest
ValidatedConstructor other = ValidatedConstructor.class.cast(obj); ValidatedConstructor other = ValidatedConstructor.class.cast(obj);
return other != null && Objects.equal(foo, other.foo) && Objects.equal(bar, other.bar); return other != null && Objects.equal(foo, other.foo) && Objects.equal(bar, other.bar);
} }
@Override
public int hashCode() {
return Objects.hashCode(foo, bar);
}
} }
@Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "absent!") @Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "absent!")
@ -193,6 +198,11 @@ public final class DeserializationConstructorAndReflectiveTypeAdapterFactoryTest
RenamedFields other = RenamedFields.class.cast(obj); RenamedFields other = RenamedFields.class.cast(obj);
return other != null && Objects.equal(foo, other.foo) && Objects.equal(bar, other.bar); return other != null && Objects.equal(foo, other.foo) && Objects.equal(bar, other.bar);
} }
@Override
public int hashCode() {
return Objects.hashCode(foo, bar);
}
} }
public void testCanOverrideDefault() throws IOException { public void testCanOverrideDefault() throws IOException {
@ -222,6 +232,11 @@ public final class DeserializationConstructorAndReflectiveTypeAdapterFactoryTest
ComposedObjects other = ComposedObjects.class.cast(obj); ComposedObjects other = ComposedObjects.class.cast(obj);
return other != null && Objects.equal(x, other.x) && Objects.equal(y, other.y); return other != null && Objects.equal(x, other.x) && Objects.equal(y, other.y);
} }
@Override
public int hashCode() {
return Objects.hashCode(x, y);
}
} }
public void checkSimpleComposedObject() throws IOException { public void checkSimpleComposedObject() throws IOException {

View File

@ -67,6 +67,11 @@ public class OptionalTypeAdapterFactoryTest {
SimpleBean that = SimpleBean.class.cast(other); SimpleBean that = SimpleBean.class.cast(other);
return Objects.equal(value, that.value) && Objects.equal(someOtherValue, that.someOtherValue); return Objects.equal(value, that.value) && Objects.equal(someOtherValue, that.someOtherValue);
} }
@Override
public int hashCode() {
return Objects.hashCode(value, someOtherValue);
}
} }
// register the type adapter so that gson can serialize/deserialize to it // register the type adapter so that gson can serialize/deserialize to it