Skip to content

Commit 087913a

Browse files
committed
Standardize equals and hashCode for wrappers
1 parent 64942cb commit 087913a

10 files changed

+96
-83
lines changed

modules/API/src/main/java/com/comphenix/protocol/wrappers/AbstractWrapper.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,26 @@ public Object getHandle() {
4747
public Class<?> getHandleType() {
4848
return handleType;
4949
}
50+
51+
@Override
52+
public boolean equals(Object obj) {
53+
if (obj == this) return true;
54+
55+
if (obj instanceof AbstractWrapper) {
56+
AbstractWrapper that = (AbstractWrapper) obj;
57+
return this.handle.equals(that.handle);
58+
}
59+
60+
return false;
61+
}
62+
63+
@Override
64+
public int hashCode() {
65+
return handle.hashCode();
66+
}
67+
68+
@Override
69+
public String toString() {
70+
return getClass().getName() + "[handle=" + handle + "]";
71+
}
5072
}

modules/API/src/main/java/com/comphenix/protocol/wrappers/BukkitConverters.java

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -158,20 +158,20 @@ public final TType getSpecific(Object generic) {
158158

159159
@Override
160160
public boolean equals(Object obj) {
161-
// Very short
162-
if (this == obj)
163-
return true;
164-
if (obj == null)
165-
return false;
166-
167-
// See if they're equivalent
161+
if (this == obj) return true;
162+
168163
if (obj instanceof EquivalentConverter) {
169-
@SuppressWarnings("rawtypes")
170-
EquivalentConverter other = (EquivalentConverter) obj;
171-
return Objects.equal(this.getSpecificType(), other.getSpecificType());
164+
EquivalentConverter<?> that = (EquivalentConverter<?>) obj;
165+
return Objects.equal(this.getSpecificType(), that.getSpecificType());
172166
}
167+
173168
return false;
174169
}
170+
171+
@Override
172+
public int hashCode() {
173+
return Objects.hashCode(this.getSpecificType());
174+
}
175175
}
176176

177177
/**
@@ -194,23 +194,23 @@ public WorldSpecificConverter(World world) {
194194

195195
@Override
196196
public boolean equals(Object obj) {
197-
// More shortcuts
198-
if (obj == this)
199-
return true;
200-
if (obj == null)
201-
return false;
202-
197+
if (obj == this) return true;
198+
203199
// Add another constraint
204200
if (obj instanceof WorldSpecificConverter && super.equals(obj)) {
205-
@SuppressWarnings("rawtypes")
206-
WorldSpecificConverter other = (WorldSpecificConverter) obj;
207-
208-
return Objects.equal(world, other.world);
201+
WorldSpecificConverter<?> that = (WorldSpecificConverter<?>) obj;
202+
return Objects.equal(this.world, that.world);
209203
}
204+
210205
return false;
211206
}
207+
208+
@Override
209+
public int hashCode() {
210+
return Objects.hashCode(this.getSpecificType(), this.world);
211+
}
212212
}
213-
213+
214214
/**
215215
* Retrieve an equivalent converter for a map of generic keys and primitive values.
216216
* @param <T> Key type

modules/API/src/main/java/com/comphenix/protocol/wrappers/Vector3F.java

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,29 @@ public Vector3F setZ(float z) {
5555
return this;
5656
}
5757

58-
public boolean equals(Object object) {
59-
if (object instanceof Vector3F) {
60-
Vector3F that = (Vector3F) object;
61-
return this.x == that.x && this.y == that.y && this.z == that.z;
58+
@Override
59+
public int hashCode() {
60+
final int prime = 31;
61+
int result = 1;
62+
result = prime * result + Float.floatToIntBits(x);
63+
result = prime * result + Float.floatToIntBits(y);
64+
result = prime * result + Float.floatToIntBits(z);
65+
return result;
66+
}
67+
68+
@Override
69+
public boolean equals(Object obj) {
70+
if (this == obj) return true;
71+
72+
if (obj instanceof Vector3F) {
73+
Vector3F that = (Vector3F) obj;
74+
if (Float.floatToIntBits(x) != Float.floatToIntBits(that.x))
75+
return false;
76+
if (Float.floatToIntBits(y) != Float.floatToIntBits(that.y))
77+
return false;
78+
if (Float.floatToIntBits(z) != Float.floatToIntBits(that.z))
79+
return false;
80+
return true;
6281
}
6382

6483
return false;

modules/API/src/main/java/com/comphenix/protocol/wrappers/WrappedBlockData.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,13 +162,22 @@ public String toString() {
162162
return "WrappedBlockData[handle=" + handle + "]";
163163
}
164164

165+
@Override
166+
public int hashCode() {
167+
final int prime = 31;
168+
int result = 1;
169+
result = prime * result + getType().hashCode();
170+
result = prime * result + getData();
171+
return result;
172+
}
173+
165174
@Override
166175
public boolean equals(Object o) {
167176
if (o instanceof WrappedBlockData) {
168177
WrappedBlockData that = (WrappedBlockData) o;
169-
return this.getType() == that.getType();
178+
return this.getType() == that.getType() && getData() == that.getData();
170179
}
171180

172181
return false;
173182
}
174-
}
183+
}

modules/API/src/main/java/com/comphenix/protocol/wrappers/WrappedChatComponent.java

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -138,21 +138,6 @@ public void setJson(String obj) {
138138
public WrappedChatComponent deepClone() {
139139
return fromJson(getJson());
140140
}
141-
142-
@Override
143-
public boolean equals(Object obj) {
144-
if (obj == this)
145-
return true;
146-
if (obj instanceof WrappedChatComponent) {
147-
return ((WrappedChatComponent) obj).handle.equals(handle);
148-
}
149-
return false;
150-
}
151-
152-
@Override
153-
public int hashCode() {
154-
return handle.hashCode();
155-
}
156141

157142
@Override
158143
public String toString() {

modules/API/src/main/java/com/comphenix/protocol/wrappers/WrappedChunkCoordinate.java

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
import com.comphenix.protocol.reflect.StructureModifier;
2121
import com.comphenix.protocol.utility.MinecraftReflection;
22-
import com.google.common.base.Objects;
2322

2423
/**
2524
* Allows access to a chunk coordinate.
@@ -161,24 +160,6 @@ public int compareTo(WrappedChunkCoordinate other) {
161160
return ((Comparable<Object>) handle).compareTo(other.handle);
162161
}
163162

164-
@Override
165-
public boolean equals(Object other) {
166-
if (other instanceof WrappedChunkCoordinate) {
167-
WrappedChunkCoordinate wrapper = (WrappedChunkCoordinate) other;
168-
return Objects.equal(handle, wrapper.handle);
169-
}
170-
171-
// It's tempting to handle the ChunkCoordinate case too, but then
172-
// the equals() method won't be commutative, causing a.equals(b) to
173-
// be different to b.equals(a).
174-
return false;
175-
}
176-
177-
@Override
178-
public int hashCode() {
179-
return handle.hashCode();
180-
}
181-
182163
@Override
183164
public String toString() {
184165
return String.format("ChunkCoordinate [x: %s, y: %s, z: %s]", getX(), getY(), getZ());

modules/API/src/main/java/com/comphenix/protocol/wrappers/WrappedDataWatcher.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -770,7 +770,6 @@ public String toString() {
770770
@Override
771771
public boolean equals(Object obj) {
772772
if (obj == this) return true;
773-
if (obj == null) return false;
774773

775774
if (obj instanceof WrappedDataWatcherObject) {
776775
WrappedDataWatcherObject other = (WrappedDataWatcherObject) obj;
@@ -779,6 +778,11 @@ public boolean equals(Object obj) {
779778

780779
return false;
781780
}
781+
782+
@Override
783+
public int hashCode() {
784+
return handle.hashCode();
785+
}
782786
}
783787

784788
private static class DummyWatcherObject extends WrappedDataWatcherObject {

modules/API/src/main/java/com/comphenix/protocol/wrappers/WrappedSignedProperty.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,15 +132,16 @@ public int hashCode() {
132132
}
133133

134134
@Override
135-
public boolean equals(Object object){
135+
public boolean equals(Object object) {
136+
if (object == this) return true;
137+
136138
if (object instanceof WrappedSignedProperty) {
137-
if (!super.equals(object))
138-
return false;
139139
WrappedSignedProperty that = (WrappedSignedProperty) object;
140140
return Objects.equal(this.getName(), that.getName())
141141
&& Objects.equal(this.getValue(), that.getValue())
142142
&& Objects.equal(this.getSignature(), that.getSignature());
143143
}
144+
144145
return false;
145146
}
146147

modules/API/src/main/java/com/comphenix/protocol/wrappers/WrappedStatistic.java

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -82,21 +82,4 @@ public String getName() {
8282
public String toString() {
8383
return String.valueOf(handle);
8484
}
85-
86-
@Override
87-
public int hashCode() {
88-
return handle.hashCode();
89-
}
90-
91-
@Override
92-
public boolean equals(Object obj) {
93-
if (obj == this)
94-
return true;
95-
96-
if (obj instanceof WrappedGameProfile) {
97-
WrappedStatistic other = (WrappedStatistic) obj;
98-
return handle.equals(other.handle);
99-
}
100-
return false;
101-
}
10285
}

modules/API/src/main/java/com/comphenix/protocol/wrappers/WrappedWatchableObject.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,6 @@ public void setDirtyState(boolean dirty) {
170170
@Override
171171
public boolean equals(Object obj) {
172172
if (obj == this) return true;
173-
if (obj == null) return false;
174173

175174
if (obj instanceof WrappedWatchableObject) {
176175
WrappedWatchableObject that = (WrappedWatchableObject) obj;
@@ -182,6 +181,16 @@ public boolean equals(Object obj) {
182181
return false;
183182
}
184183

184+
@Override
185+
public int hashCode() {
186+
final int prime = 31;
187+
int result = 1;
188+
result = prime * result + getIndex();
189+
result = prime * result + getRawValue().hashCode();
190+
result = prime * result + (getDirtyState() ? 1231 : 1237);
191+
return result;
192+
}
193+
185194
@Override
186195
public String toString() {
187196
return "DataWatcherItem[index=" + getIndex() + ", value=" + getValue() + ", dirty=" + getDirtyState() + "]";

0 commit comments

Comments
 (0)