Standardize equals and hashCode for wrappers
Dieser Commit ist enthalten in:
Ursprung
64942cbb5e
Commit
087913ab3a
@ -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 + "]";
|
||||
}
|
||||
}
|
||||
|
@ -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 <T> Key type
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
@ -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() {
|
||||
|
@ -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<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
|
||||
public String toString() {
|
||||
return String.format("ChunkCoordinate [x: %s, y: %s, z: %s]", getX(), getY(), getZ());
|
||||
|
@ -770,7 +770,6 @@ public class WrappedDataWatcher extends AbstractWrapper implements Iterable<Wrap
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (obj == this) return true;
|
||||
if (obj == null) return false;
|
||||
|
||||
if (obj instanceof WrappedDataWatcherObject) {
|
||||
WrappedDataWatcherObject other = (WrappedDataWatcherObject) obj;
|
||||
@ -779,6 +778,11 @@ public class WrappedDataWatcher extends AbstractWrapper implements Iterable<Wrap
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return handle.hashCode();
|
||||
}
|
||||
}
|
||||
|
||||
private static class DummyWatcherObject extends WrappedDataWatcherObject {
|
||||
|
@ -132,15 +132,16 @@ public class WrappedSignedProperty extends AbstractWrapper {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object object){
|
||||
public boolean equals(Object object) {
|
||||
if (object == this) return true;
|
||||
|
||||
if (object instanceof WrappedSignedProperty) {
|
||||
if (!super.equals(object))
|
||||
return false;
|
||||
WrappedSignedProperty that = (WrappedSignedProperty) object;
|
||||
return Objects.equal(this.getName(), that.getName())
|
||||
&& Objects.equal(this.getValue(), that.getValue())
|
||||
&& Objects.equal(this.getSignature(), that.getSignature());
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -82,21 +82,4 @@ public class WrappedStatistic extends AbstractWrapper {
|
||||
public String toString() {
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
@ -170,7 +170,6 @@ public class WrappedWatchableObject extends AbstractWrapper {
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (obj == this) return true;
|
||||
if (obj == null) return false;
|
||||
|
||||
if (obj instanceof WrappedWatchableObject) {
|
||||
WrappedWatchableObject that = (WrappedWatchableObject) obj;
|
||||
@ -182,6 +181,16 @@ public class WrappedWatchableObject extends AbstractWrapper {
|
||||
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
|
||||
public String toString() {
|
||||
return "DataWatcherItem[index=" + getIndex() + ", value=" + getValue() + ", dirty=" + getDirtyState() + "]";
|
||||
|
In neuem Issue referenzieren
Einen Benutzer sperren