]> source.dussan.org Git - vaadin-framework.git/commitdiff
Add select/deselect using collection methods 8335/head pr8335/r1
authorMikael Grankvist <mikael.grankvist@vaadin.com>
Thu, 26 Jan 2017 07:01:07 +0000 (09:01 +0200)
committerMikael Grankvist <mikael.grankvist@vaadin.com>
Thu, 26 Jan 2017 07:01:07 +0000 (09:01 +0200)
Added select(Collection<T>) and deselect(Collection<T>) methods
to selection model multi.

server/src/main/java/com/vaadin/data/SelectionModel.java
server/src/test/java/com/vaadin/tests/components/grid/GridMultiSelectionModelTest.java

index 397e2a155711a86bedcb421be8b9514e46e70e7b..2f22c81aa1df6edd47286259e653ebd4f56c3a73 100644 (file)
@@ -17,12 +17,13 @@ package com.vaadin.data;
 
 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;
@@ -167,13 +168,33 @@ public interface SelectionModel<T> extends Serializable {
          */
         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);
@@ -192,10 +213,26 @@ public interface SelectionModel<T> extends Serializable {
          */
         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())));
         }
 
         /**
index c45bad66f0f8780fc71ae426e7301c51b57b9a73..27523677d6df2a9a0c05304baa703de3945b010c 100644 (file)
@@ -7,6 +7,7 @@ import static org.junit.Assert.assertTrue;
 
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.Collection;
 import java.util.Collections;
 import java.util.LinkedHashMap;
 import java.util.LinkedHashSet;
@@ -305,6 +306,18 @@ public class GridMultiSelectionModelTest {
         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);
@@ -334,6 +347,23 @@ public class GridMultiSelectionModelTest {
         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);