[vlma-devel] commit: Don't rely on hashCode (because of collisions). (Adrien Grand )

git version control git at videolan.org
Tue Nov 11 12:29:06 CET 2008


vlma | branch: master | Adrien Grand <jpountz at videolan.org> | Tue Nov 11 12:20:16 2008 +0100| [e19839c01600c7d566e31d952648d63c871a5279] | committer: Adrien Grand 

Don't rely on hashCode (because of collisions).

> http://git.videolan.org/gitweb.cgi/vlma.git/?a=commit;h=e19839c01600c7d566e31d952648d63c871a5279
---

 .../main/java/org/videolan/vlma/model/Order.java   |   30 +++++++++++++------
 .../java/org/videolan/vlma/model/OrderTest.java    |    8 ++++-
 2 files changed, 26 insertions(+), 12 deletions(-)

diff --git a/vlma-api/src/main/java/org/videolan/vlma/model/Order.java b/vlma-api/src/main/java/org/videolan/vlma/model/Order.java
index cae3f87..1521c88 100644
--- a/vlma-api/src/main/java/org/videolan/vlma/model/Order.java
+++ b/vlma-api/src/main/java/org/videolan/vlma/model/Order.java
@@ -110,15 +110,16 @@ public class Order implements Comparable<Order> {
         if (o == null) return false;
         if (o instanceof Order) {
             Order other = (Order) o;
-            if(getAdapter().getCapacity() <= 1) {
-                 if(getMedias().isEmpty()) {
-                     return getAdapter().equals(other.getAdapter()) && other.getMedias().isEmpty();
-                 } else {
-                     return getAdapter().equals(other.getAdapter()) &&
-                             other.getMedias().get(0).equals(other.getMedias().get(0));
-                 }
+            if(adapter.getCapacity() <= 1) {
+                return adapter.equals(other.getAdapter());
             } else {
-                return getAdapter().equals(other.getAdapter());
+                if(medias.isEmpty()) {
+                    return adapter.equals(other.getAdapter()) && other.getMedias().isEmpty();
+                } else {
+                    return adapter.equals(other.getAdapter()) &&
+                            !other.getMedias().isEmpty() &&
+                            medias.get(0).equals(other.getMedias().get(0));
+                }
             }
         }
         return false;
@@ -126,7 +127,11 @@ public class Order implements Comparable<Order> {
 
     @Override
     public int hashCode() {
-        return getName().hashCode();
+        int result = adapter.hashCode();
+        if(adapter.getCapacity() > 1 && !medias.isEmpty()) {
+            result += medias.get(0).hashCode();
+        }
+        return result;
     }
 
     public int compareTo(Order other) {
@@ -134,7 +139,12 @@ public class Order implements Comparable<Order> {
         int result = other.getMedias().getLoad() - getMedias().getLoad();
         if(result == 0) {
             // To be consistent with the equals method
-            return hashCode() - other.hashCode();
+            result = adapter.getName().compareTo(other.getAdapter().getName());
+            if(result == 0) {
+                return adapter.getServer().getName().compareTo(other.getAdapter().getServer().getName());
+            } else {
+                return result;
+            }
         } else {
             return result;
         }
diff --git a/vlma-api/src/test/java/org/videolan/vlma/model/OrderTest.java b/vlma-api/src/test/java/org/videolan/vlma/model/OrderTest.java
index 4481844..27a2b8c 100644
--- a/vlma-api/src/test/java/org/videolan/vlma/model/OrderTest.java
+++ b/vlma-api/src/test/java/org/videolan/vlma/model/OrderTest.java
@@ -53,6 +53,7 @@ public class OrderTest {
         adapter2.setServer(server);
         order = new Order(adapter, group);
         Order order2 = new Order(adapter2, group2);
+        // Same order because same adapter and adapter.capacity == 1
         assertEquals(order, order2);
         assertEquals(order.getName(), order2.getName());
         assertEquals(order.hashCode(), order2.hashCode());
@@ -62,8 +63,10 @@ public class OrderTest {
     public void testEquals2() {
         FilesChannel m1 = new FilesChannel();
         m1.setName("m1");
+        m1.setId(42);
         FilesChannel m2 = new FilesChannel();
         m2.setName("m2");
+        m2.setId(12);
         MediaGroup g1 = new MediaGroup();
         g1.add(m1);
         MediaGroup g2 = new MediaGroup();
@@ -75,7 +78,8 @@ public class OrderTest {
         a.setServer(server);
         order = new Order(a, g1);
         Order order2 = new Order(a, g2);
-        assertTrue(order.equals(order2));
-        assertTrue(order.getName().equals(order2.getName()));
+        // Different media -> different order and different order name
+        assertFalse(order.equals(order2));
+        assertFalse(order.getName().equals(order2.getName()));
     }
 }



More information about the vlma-devel mailing list