From 087913ab3acda482f9cd55e2076065acd10b00af Mon Sep 17 00:00:00 2001 From: Dan Mulloy Date: Sat, 6 Aug 2016 13:19:14 -0400 Subject: [PATCH] Standardize equals and hashCode for wrappers --- .../protocol/wrappers/AbstractWrapper.java | 22 ++++++++++ .../protocol/wrappers/BukkitConverters.java | 42 +++++++++---------- .../comphenix/protocol/wrappers/Vector3F.java | 27 ++++++++++-- .../protocol/wrappers/WrappedBlockData.java | 13 +++++- .../wrappers/WrappedChatComponent.java | 15 ------- .../wrappers/WrappedChunkCoordinate.java | 19 --------- .../protocol/wrappers/WrappedDataWatcher.java | 6 ++- .../wrappers/WrappedSignedProperty.java | 7 ++-- .../protocol/wrappers/WrappedStatistic.java | 17 -------- .../wrappers/WrappedWatchableObject.java | 11 ++++- 10 files changed, 96 insertions(+), 83 deletions(-) diff --git a/modules/API/src/main/java/com/comphenix/protocol/wrappers/AbstractWrapper.java b/modules/API/src/main/java/com/comphenix/protocol/wrappers/AbstractWrapper.java index d1f898e3..221e034b 100644 --- a/modules/API/src/main/java/com/comphenix/protocol/wrappers/AbstractWrapper.java +++ b/modules/API/src/main/java/com/comphenix/protocol/wrappers/AbstractWrapper.java @@ -47,4 +47,26 @@ public abstract class AbstractWrapper { public Class getHandleType() { return handleType; } + + @Override + public boolean equals(Object obj) { + if (obj == this) return true; + + if (obj instanceof AbstractWrapper) { + AbstractWrapper that = (AbstractWrapper) obj; + return this.handle.equals(that.handle); + } + + return false; + } + + @Override + public int hashCode() { + return handle.hashCode(); + } + + @Override + public String toString() { + return getClass().getName() + "[handle=" + handle + "]"; + } } diff --git a/modules/API/src/main/java/com/comphenix/protocol/wrappers/BukkitConverters.java b/modules/API/src/main/java/com/comphenix/protocol/wrappers/BukkitConverters.java index 2d01f412..43398bfd 100644 --- a/modules/API/src/main/java/com/comphenix/protocol/wrappers/BukkitConverters.java +++ b/modules/API/src/main/java/com/comphenix/protocol/wrappers/BukkitConverters.java @@ -158,20 +158,20 @@ public class BukkitConverters { @Override public boolean equals(Object obj) { - // Very short - if (this == obj) - return true; - if (obj == null) - return false; - - // See if they're equivalent + if (this == obj) return true; + if (obj instanceof EquivalentConverter) { - @SuppressWarnings("rawtypes") - EquivalentConverter other = (EquivalentConverter) obj; - return Objects.equal(this.getSpecificType(), other.getSpecificType()); + EquivalentConverter that = (EquivalentConverter) obj; + return Objects.equal(this.getSpecificType(), that.getSpecificType()); } + return false; } + + @Override + public int hashCode() { + return Objects.hashCode(this.getSpecificType()); + } } /** @@ -194,23 +194,23 @@ public class BukkitConverters { @Override public boolean equals(Object obj) { - // More shortcuts - if (obj == this) - return true; - if (obj == null) - return false; - + if (obj == this) return true; + // Add another constraint if (obj instanceof WorldSpecificConverter && super.equals(obj)) { - @SuppressWarnings("rawtypes") - WorldSpecificConverter other = (WorldSpecificConverter) obj; - - return Objects.equal(world, other.world); + WorldSpecificConverter that = (WorldSpecificConverter) obj; + return Objects.equal(this.world, that.world); } + return false; } + + @Override + public int hashCode() { + return Objects.hashCode(this.getSpecificType(), this.world); + } } - + /** * Retrieve an equivalent converter for a map of generic keys and primitive values. * @param Key type diff --git a/modules/API/src/main/java/com/comphenix/protocol/wrappers/Vector3F.java b/modules/API/src/main/java/com/comphenix/protocol/wrappers/Vector3F.java index 776fe6b5..2f47e6a7 100644 --- a/modules/API/src/main/java/com/comphenix/protocol/wrappers/Vector3F.java +++ b/modules/API/src/main/java/com/comphenix/protocol/wrappers/Vector3F.java @@ -55,10 +55,29 @@ public class Vector3F { return this; } - public boolean equals(Object object) { - if (object instanceof Vector3F) { - Vector3F that = (Vector3F) object; - return this.x == that.x && this.y == that.y && this.z == that.z; + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + Float.floatToIntBits(x); + result = prime * result + Float.floatToIntBits(y); + result = prime * result + Float.floatToIntBits(z); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) return true; + + if (obj instanceof Vector3F) { + Vector3F that = (Vector3F) obj; + if (Float.floatToIntBits(x) != Float.floatToIntBits(that.x)) + return false; + if (Float.floatToIntBits(y) != Float.floatToIntBits(that.y)) + return false; + if (Float.floatToIntBits(z) != Float.floatToIntBits(that.z)) + return false; + return true; } return false; diff --git a/modules/API/src/main/java/com/comphenix/protocol/wrappers/WrappedBlockData.java b/modules/API/src/main/java/com/comphenix/protocol/wrappers/WrappedBlockData.java index 010155e3..b946abdd 100644 --- a/modules/API/src/main/java/com/comphenix/protocol/wrappers/WrappedBlockData.java +++ b/modules/API/src/main/java/com/comphenix/protocol/wrappers/WrappedBlockData.java @@ -162,13 +162,22 @@ public class WrappedBlockData extends AbstractWrapper { return "WrappedBlockData[handle=" + handle + "]"; } + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + getType().hashCode(); + result = prime * result + getData(); + return result; + } + @Override public boolean equals(Object o) { if (o instanceof WrappedBlockData) { WrappedBlockData that = (WrappedBlockData) o; - return this.getType() == that.getType(); + return this.getType() == that.getType() && getData() == that.getData(); } return false; } -} +} \ No newline at end of file diff --git a/modules/API/src/main/java/com/comphenix/protocol/wrappers/WrappedChatComponent.java b/modules/API/src/main/java/com/comphenix/protocol/wrappers/WrappedChatComponent.java index ccb02e14..f542a808 100644 --- a/modules/API/src/main/java/com/comphenix/protocol/wrappers/WrappedChatComponent.java +++ b/modules/API/src/main/java/com/comphenix/protocol/wrappers/WrappedChatComponent.java @@ -138,21 +138,6 @@ public class WrappedChatComponent extends AbstractWrapper { public WrappedChatComponent deepClone() { return fromJson(getJson()); } - - @Override - public boolean equals(Object obj) { - if (obj == this) - return true; - if (obj instanceof WrappedChatComponent) { - return ((WrappedChatComponent) obj).handle.equals(handle); - } - return false; - } - - @Override - public int hashCode() { - return handle.hashCode(); - } @Override public String toString() { diff --git a/modules/API/src/main/java/com/comphenix/protocol/wrappers/WrappedChunkCoordinate.java b/modules/API/src/main/java/com/comphenix/protocol/wrappers/WrappedChunkCoordinate.java index ff0790ba..ee1bb097 100644 --- a/modules/API/src/main/java/com/comphenix/protocol/wrappers/WrappedChunkCoordinate.java +++ b/modules/API/src/main/java/com/comphenix/protocol/wrappers/WrappedChunkCoordinate.java @@ -19,7 +19,6 @@ package com.comphenix.protocol.wrappers; import com.comphenix.protocol.reflect.StructureModifier; import com.comphenix.protocol.utility.MinecraftReflection; -import com.google.common.base.Objects; /** * Allows access to a chunk coordinate. @@ -161,24 +160,6 @@ public class WrappedChunkCoordinate extends AbstractWrapper implements Comparabl return ((Comparable) handle).compareTo(other.handle); } - @Override - public boolean equals(Object other) { - if (other instanceof WrappedChunkCoordinate) { - WrappedChunkCoordinate wrapper = (WrappedChunkCoordinate) other; - return Objects.equal(handle, wrapper.handle); - } - - // It's tempting to handle the ChunkCoordinate case too, but then - // the equals() method won't be commutative, causing a.equals(b) to - // be different to b.equals(a). - return false; - } - - @Override - public int hashCode() { - return handle.hashCode(); - } - @Override public String toString() { return String.format("ChunkCoordinate [x: %s, y: %s, z: %s]", getX(), getY(), getZ()); diff --git a/modules/API/src/main/java/com/comphenix/protocol/wrappers/WrappedDataWatcher.java b/modules/API/src/main/java/com/comphenix/protocol/wrappers/WrappedDataWatcher.java index 6b3b5250..f74e4b3b 100644 --- a/modules/API/src/main/java/com/comphenix/protocol/wrappers/WrappedDataWatcher.java +++ b/modules/API/src/main/java/com/comphenix/protocol/wrappers/WrappedDataWatcher.java @@ -770,7 +770,6 @@ public class WrappedDataWatcher extends AbstractWrapper implements Iterable