[ovirt-devel] SortedListModel

Alexander Wels awels at redhat.com
Thu May 29 15:44:09 UTC 2014


On Thursday, May 29, 2014 04:29:11 PM Lior Vernia wrote:
> Hello Alex,
> 
> On 29/05/14 16:05, Alexander Wels wrote:
> > Hi guys,
> > 
> > I have a question about the SortedListModel. If you look at the
> > setItems(Collection<T> value) method. You will notice that eventually all
> > the items are added to a SortedSet. This is not a problem if all the
> > elements of your collection are different. But what happens if the
> > elements of your collection are not all different. More specifically if I
> > pass in a comparator that matches on a field of the object that is not
> > different like description, or size or something of that nature.
> > 
> > The set will reduce the number of elements. Before I change it to be a
> > list
> > that can have duplicates, I would like to know the origin of the set and
> > if
> > there are going to be any issues when I do that.
> 
> It was originally conceived as a way to consistently keep items in a
> sorted manner, whether they're added via setItems() or getItems().add().
> It had nothing to do with the fact that duplicates aren't allowed, so I
> doubt a change will adversely affect current uses (of which there aren't
> many, if I'm not mistaken, so it's better to verify).
> 
> However, a list doesn't automatically sort itself as items are added, so
> you should preserve that functionality if you decide to change the
> implementation.
> 
> You could implement a customized List that invokes sort() upon add(). I
> think the current implementation is far more efficient in that insertion
> and removal is done in O(logn), whereas the alternative would be O(n).
> You might say that's not important, I think it might be important for
> scalability in the future.
> 

Personally I think that having correct behavior is more important than having 
incorrect but fast behavior. Basically the set works fine if you use the 
natural order of the objects. However if you pass in a different Comparator the 
set falls apart as you now potentially have duplicates based on the 
Comparator. A good example is entities with a description or comment field. If 
I pass in a Comparator that simply compares the description field of the 
entities, I can and do have duplicates. The resulting collection is now 
smaller than the original which is obviously not what we want.

You are right that I will be unable to force a sort when someone does a 
getItems.add. There seems to be only one data structure in java that is 
automatically sorted on adds as well as allows for duplicate entries, which is 
a PriorityQueue.  Since we are using Collections this should not be a problem, 
however if anyone casts it to a list, it will explode.

> But frankly, it's not difficult to notice what one inserts into their
> SortedListModel, and to make sure that its equals() method is consistent
> with what they're trying to achieve (or wrap the items in case it isn't).
> 
> > Thanks,
> > Alexander
> > _______________________________________________
> > Devel mailing list
> > Devel at ovirt.org
> > http://lists.ovirt.org/mailman/listinfo/devel




More information about the Devel mailing list