import java.io.Serializable;
import java.util.Arrays;
+import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
-import java.util.stream.Stream;
+import java.util.stream.Collectors;
import com.vaadin.event.selection.SelectionListener;
import com.vaadin.shared.Registration;
*/
public default void selectItems(T... items) {
Objects.requireNonNull(items);
- Stream.of(items).forEach(Objects::requireNonNull);
+ selectItems(Arrays.asList(items));
+ }
- updateSelection(new LinkedHashSet<>(Arrays.asList(items)),
+ /**
+ * Adds the given items to the set of currently selected items.
+ * <p>
+ * By default this does not clear any previous selection. To do that,
+ * use {@link #deselectAll()}.
+ * <p>
+ * If the all the items were already selected, this is a NO-OP.
+ * <p>
+ * This is a short-hand for {@link #updateSelection(Set, Set)} with
+ * nothing to deselect.
+ *
+ * @param items
+ * to add to selection, not {@code null}
+ */
+ public default void selectItems(Collection<T> items) {
+ Objects.requireNonNull(items);
+ items.stream().forEach(Objects::requireNonNull);
+
+ updateSelection(
+ new LinkedHashSet<>(
+ items.stream().collect(Collectors.toList())),
Collections.emptySet());
}
- @SuppressWarnings("unchecked")
@Override
public default void deselect(T item) {
deselectItems(item);
*/
public default void deselectItems(T... items) {
Objects.requireNonNull(items);
- Stream.of(items).forEach(Objects::requireNonNull);
+ deselectItems(Arrays.asList(items));
+ }
+
+ /**
+ * Removes the given items from the set of currently selected items.
+ * <p>
+ * If the none of the items were selected, this is a NO-OP.
+ * <p>
+ * This is a short-hand for {@link #updateSelection(Set, Set)} with
+ * nothing to select.
+ *
+ * @param items
+ * to remove from selection, not {@code null}
+ */
+ public default void deselectItems(Collection<T> items) {
+ Objects.requireNonNull(items);
+ items.stream().forEach(Objects::requireNonNull);
- updateSelection(Collections.emptySet(),
- new LinkedHashSet<>(Arrays.asList(items)));
+ updateSelection(Collections.emptySet(), new LinkedHashSet<>(
+ items.stream().collect(Collectors.toList())));
}
/**
import java.util.ArrayList;
import java.util.Arrays;
+import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
assertEquals(2, events.get());
}
+ @Test
+ public void selectItemsUsingCollection() {
+ Collection<Person> personCollection = Arrays.asList(PERSON_C, PERSON_B);
+ selectionModel.selectItems(personCollection);
+
+ assertFalse(selectionModel.isSelected(PERSON_A));
+ assertTrue(selectionModel.isSelected(PERSON_B));
+ assertTrue(selectionModel.isSelected(PERSON_C));
+
+ assertEquals("Got more events than expected", 1, events.get());
+ }
+
@Test
public void deselectItems() {
selectionModel.selectItems(PERSON_C, PERSON_A, PERSON_B);
assertEquals(3, events.get());
}
+ @Test
+ public void deselectItemsUsingCollection() {
+ selectionModel.selectItems(PERSON_C, PERSON_A, PERSON_B);
+
+ Collection<Person> personCollection = Arrays.asList(PERSON_A, PERSON_B);
+ selectionModel.deselectItems(personCollection);
+
+ assertEquals(PERSON_C,
+ selectionModel.getFirstSelectedItem().orElse(null));
+ assertEquals(Optional.of(PERSON_C),
+ selectionModel.getFirstSelectedItem());
+
+ assertFalse(selectionModel.isSelected(PERSON_A));
+ assertFalse(selectionModel.isSelected(PERSON_B));
+ assertTrue(selectionModel.isSelected(PERSON_C));
+ }
+
@Test
public void selectionEvent_newSelection_oldSelection() {
selectionModel.selectItems(PERSON_C, PERSON_A, PERSON_B);