diff --git a/platform-independent/java/gui/src/main/java/com/lyndir/masterpassword/gui/util/CollectionListModel.java b/platform-independent/java/gui/src/main/java/com/lyndir/masterpassword/gui/util/CollectionListModel.java index f4a99bb0..e7e86706 100644 --- a/platform-independent/java/gui/src/main/java/com/lyndir/masterpassword/gui/util/CollectionListModel.java +++ b/platform-independent/java/gui/src/main/java/com/lyndir/masterpassword/gui/util/CollectionListModel.java @@ -1,5 +1,10 @@ package com.lyndir.masterpassword.gui.util; +import com.google.common.base.Predicates; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.Iterables; +import com.lyndir.lhunath.opal.system.logging.Logger; +import com.lyndir.lhunath.opal.system.util.ObjectUtils; import java.util.*; import java.util.function.Consumer; import javax.annotation.Nullable; @@ -15,6 +20,8 @@ import javax.swing.event.ListSelectionListener; public class CollectionListModel extends AbstractListModel implements ComboBoxModel, ListSelectionListener, Selectable> { + private static final Logger logger = Logger.get( CollectionListModel.class ); + private final List model = new LinkedList<>(); @Nullable private JList list; @@ -51,16 +58,11 @@ public class CollectionListModel extends AbstractListModel * This operation will mutate the internal model to reflect the given model. * The given model will remain untouched and independent from this object. */ - @SuppressWarnings({ "unchecked", "SuspiciousToArrayCall" }) - public synchronized void set(final Collection elements) { - set( (E[]) elements.toArray( new Object[0] ) ); - } - - @SuppressWarnings("AssignmentToForLoopParameter") - public synchronized void set(final E... elements) { + @SuppressWarnings({ "Guava", "AssignmentToForLoopParameter" }) + public synchronized void set(final Iterable elements) { ListIterator oldIt = model.listIterator(); for (int from = 0; oldIt.hasNext(); ++from) { - int to = Arrays.binarySearch( elements, oldIt.next() ); + int to = Iterables.indexOf( elements, Predicates.equalTo( oldIt.next() ) ); if (to != from) { oldIt.remove(); @@ -69,19 +71,25 @@ public class CollectionListModel extends AbstractListModel } } - for (int to = 0; to < elements.length; ++to) { - E newSite = elements[to]; - + int to = 0; + for (final E newSite : elements) { if ((to >= model.size()) || !Objects.equals( model.get( to ), newSite )) { model.add( to, newSite ); fireIntervalAdded( this, to, to ); } + + ++to; } if ((selectedItem == null) || !model.contains( selectedItem )) setSelectedItem( getElementAt( 0 ) ); } + @SafeVarargs + public final synchronized void set(final E... elements) { + set( ImmutableList.copyOf( elements ) ); + } + @Override @SuppressWarnings({ "unchecked", "SuspiciousMethodCalls" }) public synchronized void setSelectedItem(@Nullable final Object newSelectedItem) {