Lior Vernia has uploaded a new change for review.

Change subject: webadmin: Render client-side sorting more robust
......................................................................

webadmin: Render client-side sorting more robust

Changed the implementation of the wrapper SortSensitiveComparator to
fall back to the previous ordering of two items in case they're equal
according to the passed comparator. This has two advantages:

1. The sorting is guaranteed to be stable, i.e. items that are equal
as far as the comparator is concerned maintain their previous
positions. This is guaranteed by Java default sorting as well, but
this way we don't rely on it in case implementation changes.

2. "Duplicate" items (as far as the comparator is concerned) won't be
removed from the underlying Set, because the wrapper
SortSensitiveComparator makes sure that no two items are ever
considered "duplicate".

Change-Id: Iffd1ce7763e516ad109fdae5d584b183ee44117c
Signed-off-by: Lior Vernia <[email protected]>
---
M 
frontend/webadmin/modules/uicommonweb/src/main/java/org/ovirt/engine/ui/uicommonweb/models/SortedListModel.java
1 file changed, 16 insertions(+), 2 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/68/28268/1

diff --git 
a/frontend/webadmin/modules/uicommonweb/src/main/java/org/ovirt/engine/ui/uicommonweb/models/SortedListModel.java
 
b/frontend/webadmin/modules/uicommonweb/src/main/java/org/ovirt/engine/ui/uicommonweb/models/SortedListModel.java
index 3244218..f08b292 100644
--- 
a/frontend/webadmin/modules/uicommonweb/src/main/java/org/ovirt/engine/ui/uicommonweb/models/SortedListModel.java
+++ 
b/frontend/webadmin/modules/uicommonweb/src/main/java/org/ovirt/engine/ui/uicommonweb/models/SortedListModel.java
@@ -2,6 +2,9 @@
 
 import java.util.Collection;
 import java.util.Comparator;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
 import java.util.SortedSet;
 import java.util.TreeSet;
 
@@ -9,7 +12,7 @@
 
 public class SortedListModel<T> extends ListModel<T> {
 
-    private static class SortSensitiveComparator<T> implements Comparator<T> {
+    private class SortSensitiveComparator<T> implements Comparator<T> {
 
         private final Comparator<? super T> comparator;
         private final boolean sortAscending;
@@ -22,7 +25,8 @@
 
         @Override
         public int compare(T a, T b) {
-            return sortAscending ? comparator.compare(a, b) : 
comparator.compare(b, a);
+            int res = sortAscending ? comparator.compare(a, b) : 
comparator.compare(b, a);
+            return res != 0 ? res : Integer.signum(positions.get(b) - 
positions.get(a));
         }
 
         @Override
@@ -55,6 +59,7 @@
     }
 
     protected Comparator<? super T> comparator;
+    private final Map<T, Integer> positions = new HashMap<T, Integer>();
 
     public SortedListModel(Comparator<? super T> comparator) {
         super();
@@ -84,9 +89,18 @@
             return items;
         }
 
+        initPositions(items);
         SortedSet<T> sortedItems = new TreeSet<T>(comparator);
         sortedItems.addAll(items);
         return sortedItems;
     }
 
+    private void initPositions(Collection<T> items) {
+        positions.clear();
+        Iterator<T> i = items.iterator();
+        for (int count=0; i.hasNext(); ++count) {
+            positions.put(i.next(), count);
+        }
+    }
+
 }


-- 
To view, visit http://gerrit.ovirt.org/28268
To unsubscribe, visit http://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Iffd1ce7763e516ad109fdae5d584b183ee44117c
Gerrit-PatchSet: 1
Gerrit-Project: ovirt-engine
Gerrit-Branch: master
Gerrit-Owner: Lior Vernia <[email protected]>
_______________________________________________
Engine-patches mailing list
[email protected]
http://lists.ovirt.org/mailman/listinfo/engine-patches

Reply via email to