aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDanny van Heumen <danny@dannyvanheumen.nl>2014-08-20 23:22:28 +0200
committerDanny van Heumen <danny@dannyvanheumen.nl>2014-08-21 23:45:30 +0200
commitcd3fef9832ea0eebb2b3a1cbf327629607f3301a (patch)
tree1f4c4d7cbbf9330bfe1c7aa1267d694a1d625859
parent413f55e419ddd2188f755cafb9861e198ac62583 (diff)
downloadjitsi-cd3fef9832ea0eebb2b3a1cbf327629607f3301a.zip
jitsi-cd3fef9832ea0eebb2b3a1cbf327629607f3301a.tar.gz
jitsi-cd3fef9832ea0eebb2b3a1cbf327629607f3301a.tar.bz2
Simplified implementation for 2 unknowns. Added regression tests.
-rw-r--r--src/net/java/sip/communicator/impl/gui/main/contactlist/GroupNode.java17
-rw-r--r--test/net/java/sip/communicator/impl/gui/main/contactlist/GroupNodeTest.java89
2 files changed, 92 insertions, 14 deletions
diff --git a/src/net/java/sip/communicator/impl/gui/main/contactlist/GroupNode.java b/src/net/java/sip/communicator/impl/gui/main/contactlist/GroupNode.java
index b423718..058d393 100644
--- a/src/net/java/sip/communicator/impl/gui/main/contactlist/GroupNode.java
+++ b/src/net/java/sip/communicator/impl/gui/main/contactlist/GroupNode.java
@@ -421,7 +421,7 @@ public class GroupNode
* Note: this comparator imposes orderings that are inconsistent with
* equals.
*/
- private static class NodeComparator
+ static class NodeComparator
implements Comparator<ContactListNode>
{
/**
@@ -439,21 +439,10 @@ public class GroupNode
int index1 = node1.getSourceIndex();
int index2 = node2.getSourceIndex();
- // If both indexes are unknown.
+ // If both indexes are unknown, consider them equal. We need this
+ // case to ensure the property of symmetry in the node comparator.
if (index1 < 0 && index2 < 0)
- {
- // To ensure correct behaviour under the Comparator's general
- // contract, we still need to return consistent (symmetric)
- // choices even in the case where both nodes have negative
- // source index.
- final int hash1 = node1.hashCode();
- final int hash2 = node2.hashCode();
- if (hash1 < hash2)
- return -1;
- if (hash2 < hash1)
- return 1;
return 0;
- }
// If the first index is unknown then we position it at the end.
if (index1 < 0)
return 1;
diff --git a/test/net/java/sip/communicator/impl/gui/main/contactlist/GroupNodeTest.java b/test/net/java/sip/communicator/impl/gui/main/contactlist/GroupNodeTest.java
new file mode 100644
index 0000000..d03d101
--- /dev/null
+++ b/test/net/java/sip/communicator/impl/gui/main/contactlist/GroupNodeTest.java
@@ -0,0 +1,89 @@
+/*
+ * Jitsi, the OpenSource Java VoIP and Instant Messaging client.
+ *
+ * Distributable under LGPL license.
+ * See terms of license at gnu.org.
+ */
+package net.java.sip.communicator.impl.gui.main.contactlist;
+
+import junit.framework.*;
+import net.java.sip.communicator.impl.gui.main.contactlist.GroupNode.NodeComparator;
+import net.java.sip.communicator.service.gui.*;
+
+public class GroupNodeTest
+ extends TestCase
+{
+
+ public void testNodeComparatorUnknownsAtTheEnd()
+ {
+ ContactListNode unknown = new ContactListNode()
+ {
+ @Override
+ public int getSourceIndex()
+ {
+ return -1;
+ }
+ };
+ ContactListNode node = new ContactListNode()
+ {
+ @Override
+ public int getSourceIndex()
+ {
+ return 1;
+ }
+ };
+ NodeComparator comparator = new GroupNode.NodeComparator();
+ Assert.assertEquals(comparator.compare(unknown, node),
+ -1 * comparator.compare(node, unknown));
+ Assert.assertEquals(1, comparator.compare(unknown, node));
+ Assert.assertEquals(-1, comparator.compare(node, unknown));
+ }
+
+ public void testNodeComparatorNormalNodes()
+ {
+ ContactListNode node1 = new ContactListNode()
+ {
+ @Override
+ public int getSourceIndex()
+ {
+ return 4;
+ }
+ };
+ ContactListNode node2 = new ContactListNode()
+ {
+ @Override
+ public int getSourceIndex()
+ {
+ return 7;
+ }
+ };
+ NodeComparator comparator = new GroupNode.NodeComparator();
+ Assert.assertEquals(comparator.compare(node1, node2),
+ -1 * comparator.compare(node2, node1));
+ Assert.assertEquals(-1, comparator.compare(node1, node2));
+ Assert.assertEquals(1, comparator.compare(node2, node1));
+ }
+
+ public void testNodeComparatorSymmetryForUnknownNodes()
+ {
+ ContactListNode unknown1 = new ContactListNode()
+ {
+ @Override
+ public int getSourceIndex()
+ {
+ return -1;
+ }
+ };
+ ContactListNode unknown2 = new ContactListNode()
+ {
+ @Override
+ public int getSourceIndex()
+ {
+ return -1;
+ }
+ };
+ NodeComparator comparator = new GroupNode.NodeComparator();
+ Assert.assertEquals(comparator.compare(unknown1, unknown2),
+ -1 * comparator.compare(unknown2, unknown1));
+ }
+}