Browse Source

Wrong equals and hashCode functions

Julian 16 years ago
parent
commit
034d7987fe

+ 24 - 10
L2_GameServer/java/net/sf/l2j/gameserver/model/L2DropData.java

@@ -169,18 +169,32 @@ public class L2DropData
     }
 
     /**
-     * Returns if parameter "o" is a L2DropData and has the same itemID that the current object
-     * @param Object to compare to the current one
-     * @return boolean
+     * @see java.lang.Object#hashCode()
      */
     @Override
-	public boolean equals(Object o)
+    public int hashCode()
     {
-        if (o instanceof L2DropData)
-        {
-            L2DropData drop = (L2DropData)o;
-            return drop.getItemId() == getItemId();
-        }
-        return false;
+	    final int prime = 31;
+	    int result = 1;
+	    result = prime * result + _itemId;
+	    return result;
+    }
+
+	/**
+     * @see java.lang.Object#equals(java.lang.Object)
+     */
+    @Override
+    public boolean equals(Object obj)
+    {
+	    if (this == obj)
+		    return true;
+	    if (obj == null)
+		    return false;
+	    if (!(obj instanceof L2DropData))
+		    return false;
+	    final L2DropData other = (L2DropData) obj;
+	    if (_itemId != other._itemId)
+		    return false;
+	    return true;
     }
 }

+ 36 - 16
L2_GameServer/java/net/sf/l2j/gameserver/model/L2Radar.java

@@ -67,7 +67,7 @@ public final class L2Radar
 			_player.sendPacket(new RadarControl(0, 1, tempMarker._x, tempMarker._y, tempMarker._z));
 	}
 
-    public class RadarMarker
+    public static class RadarMarker
     {
         // Simple class to model radar points.
         public int _type, _x, _y, _z;
@@ -88,23 +88,43 @@ public final class L2Radar
             _z = z;
         }
 
+        /**
+         * @see java.lang.Object#hashCode()
+         */
         @Override
-		public boolean equals(Object obj)
+        public int hashCode()
         {
-            try
-            {
-                RadarMarker temp = (RadarMarker) obj;
-
-                if ((temp._x == _x) && (temp._y == _y)
-                        && (temp._z == _z) && (temp._type == _type))
-                    return true;
-
-                return false;
-            }
-            catch (Exception e)
-            {
-                return false;
-            }
+	        final int prime = 31;
+	        int result = 1;
+	        result = prime * result + _type;
+	        result = prime * result + _x;
+	        result = prime * result + _y;
+	        result = prime * result + _z;
+	        return result;
+        }
+
+		/**
+         * @see java.lang.Object#equals(java.lang.Object)
+         */
+        @Override
+        public boolean equals(Object obj)
+        {
+	        if (this == obj)
+		        return true;
+	        if (obj == null)
+		        return false;
+	        if (!(obj instanceof RadarMarker))
+		        return false;
+	        final RadarMarker other = (RadarMarker) obj;
+	        if (_type != other._type)
+		        return false;
+	        if (_x != other._x)
+		        return false;
+	        if (_y != other._y)
+		        return false;
+	        if (_z != other._z)
+		        return false;
+	        return true;
         }
     }
 }