From 05866ac5178ccb4c48059b58290e259b2b9ac53b Mon Sep 17 00:00:00 2001 From: Aleksi Hietanen Date: Thu, 18 May 2017 15:24:45 +0300 Subject: Add TreeData#addRootItems, getRootItems --- server/src/main/java/com/vaadin/data/TreeData.java | 63 ++++++++++++++++++++++ .../vaadin/data/provider/TreeDataProviderTest.java | 17 ++++++ 2 files changed, 80 insertions(+) diff --git a/server/src/main/java/com/vaadin/data/TreeData.java b/server/src/main/java/com/vaadin/data/TreeData.java index 2311a71984..7f6cc095bc 100644 --- a/server/src/main/java/com/vaadin/data/TreeData.java +++ b/server/src/main/java/com/vaadin/data/TreeData.java @@ -96,6 +96,60 @@ public class TreeData implements Serializable { itemToWrapperMap.put(null, new HierarchyWrapper<>(null, null)); } + /** + * Adds the items as root items to this structure. + * + * @param items + * the items to add + * @return this + * + * @throws IllegalArgumentException + * if any of the given items have already been added to this + * structure + * @throws NullPointerException + * if any of the items are {code null} + */ + public TreeData addRootItems(T... items) { + addItems(null, items); + return this; + } + + /** + * Adds the items of the given collection as root items to this structure. + * + * @param items + * the collection of items to add + * @return this + * + * @throws IllegalArgumentException + * if any of the given items have already been added to this + * structure + * @throws NullPointerException + * if any of the items are {code null} + */ + public TreeData addRootItems(Collection items) { + addItems(null, items); + return this; + } + + /** + * Adds the items of the given stream as root items to this structure. + * + * @param items + * the stream of root items to add + * @return this + * + * @throws IllegalArgumentException + * if any of the given items have already been added to this + * structure + * @throws NullPointerException + * if any of the items are {code null} + */ + public TreeData addRootItems(Stream items) { + addItems(null, items); + return this; + } + /** * Adds a data item as a child of {@code parent}. Call with {@code null} as * parent to add a root level item. The given parent item must already exist @@ -283,6 +337,15 @@ public class TreeData implements Serializable { return this; } + /** + * Gets the root items of this structure. + * + * @return the root items of this structure + */ + public List getRootItems() { + return getChildren(null); + } + /** * Get the immediate child items for the given item. * diff --git a/server/src/test/java/com/vaadin/data/provider/TreeDataProviderTest.java b/server/src/test/java/com/vaadin/data/provider/TreeDataProviderTest.java index f0b2d5f81f..186ed14e8a 100644 --- a/server/src/test/java/com/vaadin/data/provider/TreeDataProviderTest.java +++ b/server/src/test/java/com/vaadin/data/provider/TreeDataProviderTest.java @@ -80,6 +80,23 @@ public class TreeDataProviderTest Assert.assertTrue(data.getChildren(null).contains(item)); } + @Test + public void treeData_root_items() { + TreeData data = new TreeData<>(); + TreeData dataVarargs = new TreeData<>(); + TreeData dataCollection = new TreeData<>(); + TreeData dataStream = new TreeData<>(); + + data.addItems(null, "a", "b", "c"); + dataVarargs.addRootItems("a", "b", "c"); + dataCollection.addRootItems(Arrays.asList("a", "b", "c")); + dataStream.addRootItems(Arrays.asList("a", "b", "c").stream()); + + Assert.assertEquals(data.getRootItems(), dataVarargs.getRootItems()); + Assert.assertEquals(data.getRootItems(), dataCollection.getRootItems()); + Assert.assertEquals(data.getRootItems(), dataStream.getRootItems()); + } + @Test public void populate_treeData_with_child_item_provider() { TreeData stringData = new TreeData<>(); -- cgit v1.2.3