diff options
author | Artur Signell <artur.signell@itmill.com> | 2010-04-19 13:42:28 +0000 |
---|---|---|
committer | Artur Signell <artur.signell@itmill.com> | 2010-04-19 13:42:28 +0000 |
commit | 15bac5d7815a2fd4eec7155e5c119ba8dab58fc7 (patch) | |
tree | 41a7bb97fe543c0e6ea876343a14dfcb2a874796 /src/com/vaadin/data/util | |
parent | fa9c9283c25d2407af4554d6d1b3663b165edf85 (diff) | |
download | vaadin-framework-15bac5d7815a2fd4eec7155e5c119ba8dab58fc7.tar.gz vaadin-framework-15bac5d7815a2fd4eec7155e5c119ba8dab58fc7.zip |
Merged fixes from 6.3:
* Build updates
* Fixes for #4418,#4562,#4514,#4536,#4533
* Test case updates
* DevelopmentServerLauncher fix
svn changeset:12649/svn branch:6.4
Diffstat (limited to 'src/com/vaadin/data/util')
-rw-r--r-- | src/com/vaadin/data/util/BeanItemContainer.java | 3 | ||||
-rw-r--r-- | src/com/vaadin/data/util/ListSet.java | 84 |
2 files changed, 77 insertions, 10 deletions
diff --git a/src/com/vaadin/data/util/BeanItemContainer.java b/src/com/vaadin/data/util/BeanItemContainer.java index 3e3c4a5538..e99f219dbb 100644 --- a/src/com/vaadin/data/util/BeanItemContainer.java +++ b/src/com/vaadin/data/util/BeanItemContainer.java @@ -444,7 +444,8 @@ public class BeanItemContainer<BT> implements Indexed, Sortable, Filterable, LinkedList<Object> sortables = new LinkedList<Object>(); for (Object propertyId : getContainerPropertyIds()) { Class<?> propertyType = getType(propertyId); - if (Comparable.class.isAssignableFrom(propertyType)) { + if (Comparable.class.isAssignableFrom(propertyType) + || propertyType.isPrimitive()) { sortables.add(propertyId); } } diff --git a/src/com/vaadin/data/util/ListSet.java b/src/com/vaadin/data/util/ListSet.java index 0a3d954a3e..4c689ec361 100644 --- a/src/com/vaadin/data/util/ListSet.java +++ b/src/com/vaadin/data/util/ListSet.java @@ -5,20 +5,30 @@ package com.vaadin.data.util; import java.util.ArrayList; import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; /** * ListSet is an internal Vaadin class which implements a combination of a List - * and a Set. The main purpose of this class is to provide a fast + * and a Set. The main purpose of this class is to provide a list with a fast * {@link #contains(Object)} method. Each inserted object must by unique (as - * specified by {@link #equals(Object)}). + * specified by {@link #equals(Object)}). The {@link #set(int, Object)} method + * allows duplicates because of the way {@link Collections#sort(java.util.List)} + * works. * * This class is subject to change and should not be used outside Vaadin core. */ public class ListSet<E> extends ArrayList<E> { private HashSet<E> itemSet = null; + /** + * Contains a map from an element to the number of duplicates it has. Used + * to temporarily allow duplicates in the list. + */ + private HashMap<E, Integer> duplicates = new HashMap<E, Integer>(); + public ListSet() { super(); itemSet = new HashSet<E>(); @@ -35,7 +45,7 @@ public class ListSet<E> extends ArrayList<E> { itemSet = new HashSet<E>(initialCapacity); } - // Delegate contains operations to the set
+ // Delegate contains operations to the set @Override public boolean contains(Object o) { return itemSet.contains(o); @@ -46,11 +56,11 @@ public class ListSet<E> extends ArrayList<E> { return itemSet.containsAll(c); } - // Methods for updating the set when the list is updated.
+ // Methods for updating the set when the list is updated. @Override public boolean add(E e) { if (contains(e)) { - // Duplicates are not allowed
+ // Duplicates are not allowed return false; } @@ -69,7 +79,7 @@ public class ListSet<E> extends ArrayList<E> { @Override public void add(int index, E element) { if (contains(element)) { - // Duplicates are not allowed
+ // Duplicates are not allowed return; } @@ -173,20 +183,76 @@ public class ListSet<E> extends ArrayList<E> { @Override public E set(int index, E element) { if (contains(element)) { - // Element already exist in the list
+ // Element already exist in the list if (get(index) == element) { - // At the same position, nothing to be done
+ // At the same position, nothing to be done return element; + } else { + // Adding at another position. We assume this is a sort + // operation and temporarily allow it. + + // We could just remove (null) the old element and keep the list + // unique. This would require finding the index of the old + // element (indexOf(element)) which is not a fast operation in a + // list. So we instead allow duplicates temporarily. + addDuplicate(element); } } E old = super.set(index, element); - itemSet.remove(old); + removeFromSet(old); itemSet.add(element); return old; } + /** + * Removes "e" from the set if it no longer exists in the list. + * + * @param e + */ + private void removeFromSet(E e) { + Integer dupl = duplicates.get(e); + if (dupl != null) { + // A duplicate was present so we only decrement the duplicate count + // and continue + if (dupl == 1) { + // This is what always should happen. A sort sets the items one + // by one, temporarily breaking the uniqueness requirement. + duplicates.remove(e); + } else { + duplicates.put(e, dupl - 1); + } + } else { + // The "old" value is no longer in the list. + itemSet.remove(e); + } + + } + + /** + * Marks the "element" can be found more than once from the list. Allowed in + * {@link #set(int, Object)} to make sorting work. + * + * @param element + */ + private void addDuplicate(E element) { + Integer nr = duplicates.get(element); + if (nr == null) { + nr = 1; + } else { + nr++; + } + + /* + * Store the number of duplicates of this element so we know later on if + * we should remove an element from the set or if it was a duplicate (in + * removeFromSet) + */ + duplicates.put(element, nr); + + } + @SuppressWarnings("unchecked") @Override public Object clone() { |