Archiviert
13
0

Standardize equals and hashCode for wrappers

Dieser Commit ist enthalten in:
Dan Mulloy 2016-08-06 13:19:14 -04:00
Ursprung 64942cbb5e
Commit 087913ab3a
10 geänderte Dateien mit 96 neuen und 83 gelöschten Zeilen

Datei anzeigen

@ -47,4 +47,26 @@ public abstract class AbstractWrapper {
public Class<?> getHandleType() { public Class<?> getHandleType() {
return handleType; 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 + "]";
}
} }

Datei anzeigen

@ -158,20 +158,20 @@ public class BukkitConverters {
@Override @Override
public boolean equals(Object obj) { public boolean equals(Object obj) {
// Very short if (this == obj) return true;
if (this == obj)
return true;
if (obj == null)
return false;
// See if they're equivalent
if (obj instanceof EquivalentConverter) { if (obj instanceof EquivalentConverter) {
@SuppressWarnings("rawtypes") EquivalentConverter<?> that = (EquivalentConverter<?>) obj;
EquivalentConverter other = (EquivalentConverter) obj; return Objects.equal(this.getSpecificType(), that.getSpecificType());
return Objects.equal(this.getSpecificType(), other.getSpecificType());
} }
return false; return false;
} }
@Override
public int hashCode() {
return Objects.hashCode(this.getSpecificType());
}
} }
/** /**
@ -194,21 +194,21 @@ public class BukkitConverters {
@Override @Override
public boolean equals(Object obj) { public boolean equals(Object obj) {
// More shortcuts if (obj == this) return true;
if (obj == this)
return true;
if (obj == null)
return false;
// Add another constraint // Add another constraint
if (obj instanceof WorldSpecificConverter && super.equals(obj)) { if (obj instanceof WorldSpecificConverter && super.equals(obj)) {
@SuppressWarnings("rawtypes") WorldSpecificConverter<?> that = (WorldSpecificConverter<?>) obj;
WorldSpecificConverter other = (WorldSpecificConverter) obj; return Objects.equal(this.world, that.world);
return Objects.equal(world, other.world);
} }
return false; return false;
} }
@Override
public int hashCode() {
return Objects.hashCode(this.getSpecificType(), this.world);
}
} }
/** /**

Datei anzeigen

@ -55,10 +55,29 @@ public class Vector3F {
return this; return this;
} }
public boolean equals(Object object) { @Override
if (object instanceof Vector3F) { public int hashCode() {
Vector3F that = (Vector3F) object; final int prime = 31;
return this.x == that.x && this.y == that.y && this.z == that.z; 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; return false;

Datei anzeigen

@ -162,11 +162,20 @@ public class WrappedBlockData extends AbstractWrapper {
return "WrappedBlockData[handle=" + handle + "]"; 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 @Override
public boolean equals(Object o) { public boolean equals(Object o) {
if (o instanceof WrappedBlockData) { if (o instanceof WrappedBlockData) {
WrappedBlockData that = (WrappedBlockData) o; WrappedBlockData that = (WrappedBlockData) o;
return this.getType() == that.getType(); return this.getType() == that.getType() && getData() == that.getData();
} }
return false; return false;

Datei anzeigen

@ -139,21 +139,6 @@ public class WrappedChatComponent extends AbstractWrapper {
return fromJson(getJson()); 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 @Override
public String toString() { public String toString() {
return "WrappedChatComponent[json=" + getJson() + "]"; return "WrappedChatComponent[json=" + getJson() + "]";

Datei anzeigen

@ -19,7 +19,6 @@ package com.comphenix.protocol.wrappers;
import com.comphenix.protocol.reflect.StructureModifier; import com.comphenix.protocol.reflect.StructureModifier;
import com.comphenix.protocol.utility.MinecraftReflection; import com.comphenix.protocol.utility.MinecraftReflection;
import com.google.common.base.Objects;
/** /**
* Allows access to a chunk coordinate. * Allows access to a chunk coordinate.
@ -161,24 +160,6 @@ public class WrappedChunkCoordinate extends AbstractWrapper implements Comparabl
return ((Comparable<Object>) handle).compareTo(other.handle); return ((Comparable<Object>) 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 @Override
public String toString() { public String toString() {
return String.format("ChunkCoordinate [x: %s, y: %s, z: %s]", getX(), getY(), getZ()); return String.format("ChunkCoordinate [x: %s, y: %s, z: %s]", getX(), getY(), getZ());

Datei anzeigen

@ -770,7 +770,6 @@ public class WrappedDataWatcher extends AbstractWrapper implements Iterable<Wrap
@Override @Override
public boolean equals(Object obj) { public boolean equals(Object obj) {
if (obj == this) return true; if (obj == this) return true;
if (obj == null) return false;
if (obj instanceof WrappedDataWatcherObject) { if (obj instanceof WrappedDataWatcherObject) {
WrappedDataWatcherObject other = (WrappedDataWatcherObject) obj; WrappedDataWatcherObject other = (WrappedDataWatcherObject) obj;
@ -779,6 +778,11 @@ public class WrappedDataWatcher extends AbstractWrapper implements Iterable<Wrap
return false; return false;
} }
@Override
public int hashCode() {
return handle.hashCode();
}
} }
private static class DummyWatcherObject extends WrappedDataWatcherObject { private static class DummyWatcherObject extends WrappedDataWatcherObject {

Datei anzeigen

@ -132,15 +132,16 @@ public class WrappedSignedProperty extends AbstractWrapper {
} }
@Override @Override
public boolean equals(Object object){ public boolean equals(Object object) {
if (object == this) return true;
if (object instanceof WrappedSignedProperty) { if (object instanceof WrappedSignedProperty) {
if (!super.equals(object))
return false;
WrappedSignedProperty that = (WrappedSignedProperty) object; WrappedSignedProperty that = (WrappedSignedProperty) object;
return Objects.equal(this.getName(), that.getName()) return Objects.equal(this.getName(), that.getName())
&& Objects.equal(this.getValue(), that.getValue()) && Objects.equal(this.getValue(), that.getValue())
&& Objects.equal(this.getSignature(), that.getSignature()); && Objects.equal(this.getSignature(), that.getSignature());
} }
return false; return false;
} }

Datei anzeigen

@ -82,21 +82,4 @@ public class WrappedStatistic extends AbstractWrapper {
public String toString() { public String toString() {
return String.valueOf(handle); return String.valueOf(handle);
} }
@Override
public int hashCode() {
return handle.hashCode();
}
@Override
public boolean equals(Object obj) {
if (obj == this)
return true;
if (obj instanceof WrappedGameProfile) {
WrappedStatistic other = (WrappedStatistic) obj;
return handle.equals(other.handle);
}
return false;
}
} }

Datei anzeigen

@ -170,7 +170,6 @@ public class WrappedWatchableObject extends AbstractWrapper {
@Override @Override
public boolean equals(Object obj) { public boolean equals(Object obj) {
if (obj == this) return true; if (obj == this) return true;
if (obj == null) return false;
if (obj instanceof WrappedWatchableObject) { if (obj instanceof WrappedWatchableObject) {
WrappedWatchableObject that = (WrappedWatchableObject) obj; WrappedWatchableObject that = (WrappedWatchableObject) obj;
@ -182,6 +181,16 @@ public class WrappedWatchableObject extends AbstractWrapper {
return false; return false;
} }
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + getIndex();
result = prime * result + getRawValue().hashCode();
result = prime * result + (getDirtyState() ? 1231 : 1237);
return result;
}
@Override @Override
public String toString() { public String toString() {
return "DataWatcherItem[index=" + getIndex() + ", value=" + getValue() + ", dirty=" + getDirtyState() + "]"; return "DataWatcherItem[index=" + getIndex() + ", value=" + getValue() + ", dirty=" + getDirtyState() + "]";