diff --git a/src/main/java/com/threerings/admin/client/DSetEditor.java b/src/main/java/com/threerings/admin/client/DSetEditor.java index 51555291f..325250d21 100644 --- a/src/main/java/com/threerings/admin/client/DSetEditor.java +++ b/src/main/java/com/threerings/admin/client/DSetEditor.java @@ -25,8 +25,11 @@ import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; -import java.util.ArrayList; +import java.util.Collection; import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.TreeMap; import javax.swing.JPanel; import javax.swing.JScrollPane; @@ -35,6 +38,7 @@ import javax.swing.JTable; import com.google.common.base.Predicate; import com.google.common.collect.Iterators; import com.google.common.collect.Lists; +import com.google.common.collect.Maps; import com.samskivert.util.ComparableArrayList; @@ -219,7 +223,7 @@ public class DSetEditor extends JPanel _accessor.updateEntry(_setName, (DSet.Entry)ce.getArgument()); } - public void setData (ComparableArrayList> keys, Object[] data) + public void setData (ComparableArrayList> keys, Collection data) { _keys = keys; _table.setData(data); @@ -296,28 +300,27 @@ public class DSetEditor extends JPanel protected void refreshData () { + // add our entries to a tree map so that we get them sorted by key (optionally applying + // our filter in the process) + TreeMap,F> data = Maps.newTreeMap(); + Iterator iter = (_entryFilter == null) ? iterator() : + Iterators.filter(iterator(), _entryFilter); + while (iter.hasNext()) { + F entry = iter.next(); + data.put(entry.getKey(), entry); + } + + // now extract that data into a sorted key list and sorted value list ComparableArrayList> keys = new ComparableArrayList>(); - E[] entries; - - if (_entryFilter == null) { - entries = createArray(); - - } else { - // Do some shuffling to get out a filtered array. - Iterator itr = Iterators.filter(iterator(), _entryFilter); - ArrayList list = Lists.newArrayList(); - Iterators.addAll(list, itr); - - @SuppressWarnings("unchecked") F[] tmp = (F[])new DSet.Entry[list.size()]; - entries = tmp; - list.toArray(entries); + List values = Lists.newArrayList(); + for (Map.Entry,F> entry : data.entrySet()) { + @SuppressWarnings("unchecked") Comparable key = + (Comparable)entry.getKey(); + keys.add(key); + values.add(entry.getValue()); } - - for (E entry : entries) { - keys.insertSorted(getKey(entry)); - } - setData(keys, entries); // this works because DSet itself is sorted + setData(keys, values); } // documentation inherited from interface SetListener @@ -351,14 +354,6 @@ public class DSetEditor extends JPanel } } - public E[] createArray () - { - @SuppressWarnings("unchecked") F[] tmp = (F[])new DSet.Entry[_set.size()]; - F[] entries = tmp; - _set.toArray(entries); - return entries; - } - public Iterator iterator () { return _set.iterator(); diff --git a/src/main/java/com/threerings/presents/dobj/DSet.java b/src/main/java/com/threerings/presents/dobj/DSet.java index cf8bd4a1e..067caabb9 100644 --- a/src/main/java/com/threerings/presents/dobj/DSet.java +++ b/src/main/java/com/threerings/presents/dobj/DSet.java @@ -243,22 +243,6 @@ public class DSet }; } - /** - * Copies the elements of this distributed set into the supplied array. If the array is not - * large enough to hold all of the elements, as many as fit into the array will be copied. If - * the array argument is null, an object array of sufficient size to contain all - * of the elements of this set will be created and returned. - */ - public E[] toArray (E[] array) - { - if (array == null) { - @SuppressWarnings("unchecked") E[] copy = (E[])new Entry[size()]; - array = copy; - } - System.arraycopy(_entries, 0, array, 0, array.length); - return array; - } - /** * Creates an immutable view of this distributed set as a Java set. */ @@ -288,7 +272,28 @@ public class DSet } /** - * @deprecated use {@link #toArray(Entry[])}. + * Copies the elements of this distributed set into the supplied array. If the array is not + * large enough to hold all of the elements, as many as fit into the array will be copied. If + * the array argument is null, an object array of sufficient size to contain all + * of the elements of this set will be created and returned. + * + * @deprecated use {@link #clone} or add this to an {@link ArrayList}. Arrays are an untypesafe + * quagmire. + */ + @Deprecated + public E[] toArray (E[] array) + { + if (array == null) { + @SuppressWarnings("unchecked") E[] copy = (E[])new Entry[size()]; + array = copy; + } + System.arraycopy(_entries, 0, array, 0, array.length); + return array; + } + + /** + * @deprecated use {@link #clone} or add this to an {@link ArrayList}. Arrays are an untypesafe + * quagmire. */ @Deprecated public Object[] toArray (Object[] array)