Skip to content

Commit 66cbdfd

Browse files
jcgueriaud1tltv
andauthored
feat: add convenient API to set items (#21979)
Add convenient API to set items with ListDataView#setItems(Collection). Fixes: #15978 Co-authored-by: Tomi Virtanen <tltv@vaadin.com>
1 parent 827cd76 commit 66cbdfd

File tree

4 files changed

+57
-0
lines changed

4 files changed

+57
-0
lines changed

flow-data/src/main/java/com/vaadin/flow/data/provider/AbstractListDataView.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,16 @@ public AbstractListDataView<T> removeItems(Collection<T> items) {
286286
return this;
287287
}
288288

289+
@Override
290+
public AbstractListDataView<T> setItems(Collection<T> items) {
291+
Objects.requireNonNull(items, NULL_COLLECTION_ERROR_MESSAGE);
292+
final ListDataProvider<T> dataProvider = getDataProvider();
293+
dataProvider.getItems().clear();
294+
dataProvider.getItems().addAll(items);
295+
dataProvider.refreshAll();
296+
return this;
297+
}
298+
289299
/**
290300
* Validate that index is inside bounds of the data available.
291301
*

flow-data/src/main/java/com/vaadin/flow/data/provider/ListDataView.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,26 @@ public interface ListDataView<T, V extends ListDataView<T, ?>>
282282
*/
283283
V removeItems(Collection<T> items);
284284

285+
/**
286+
* Remove all the items in the list and adds given items to the data list.
287+
* <p>
288+
* The backing {@link List} must be mutable to use this method. Immutable
289+
* data structure will throw an exception.
290+
* <p>
291+
* Refreshes all items of the component after adding the items, i.e. runs
292+
* {@link DataView#refreshAll()}.
293+
*
294+
* @throws UnsupportedOperationException
295+
* if backing collection doesn't support modification
296+
* @param items
297+
* collection of items to set
298+
* @return this ListDataView instance
299+
* @see #addItems(Collection)
300+
* @see #addItemsBefore(Collection, Object)
301+
* @see #addItemsAfter(Collection, Object)
302+
*/
303+
V setItems(Collection<T> items);
304+
285305
/**
286306
* Sets a filter to be applied to the data. The filter replaces any filter
287307
* that has been set or added previously. {@code null} will clear all

flow-data/src/test/java/com/vaadin/flow/data/provider/AbstractListDataViewTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.Collections;
2222
import java.util.HashSet;
2323
import java.util.Iterator;
24+
import java.util.List;
2425
import java.util.Optional;
2526
import java.util.Set;
2627
import java.util.concurrent.atomic.AtomicInteger;
@@ -724,6 +725,27 @@ public void removeItems_emptyCollectionPassed_dataNotChanged() {
724725
dataView.getItems().toArray(String[]::new));
725726
}
726727

728+
@Test
729+
public void setItems_nullCollectionPassed_throwsException() {
730+
exceptionRule.expect(NullPointerException.class);
731+
exceptionRule.expectMessage("Items collection cannot be null");
732+
733+
dataView.setItems(null);
734+
}
735+
736+
@Test
737+
public void setItems_emptyCollectionPassed_dataEmpty() {
738+
dataView.setItems(Collections.emptyList());
739+
Assert.assertTrue(dataView.getItems().toList().isEmpty());
740+
}
741+
742+
@Test
743+
public void setItems_collectionPassed_dataFilled() {
744+
dataView.setItems(List.of("first", "middle", "last"));
745+
Assert.assertArrayEquals(new String[] { "first", "middle", "last" },
746+
dataView.getItems().toArray(String[]::new));
747+
}
748+
727749
@Test
728750
public void addItemsAndRemoveItems_noConcurrencyIssues() {
729751
dataView.addItemsBefore(Arrays.asList("newOne", "newTwo", "newThree"),

flow-data/src/test/java/com/vaadin/flow/data/provider/hierarchy/HasHierarchicalDataProviderTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,11 @@ public TestListDataView removeItems(Collection<String> items) {
159159
return null;
160160
}
161161

162+
@Override
163+
public TestListDataView setItems(Collection<String> items) {
164+
return null;
165+
}
166+
162167
@Override
163168
public TestListDataView setFilter(
164169
SerializablePredicate<String> filter) {

0 commit comments

Comments
 (0)