Class TreeData<T>

  • Type Parameters:
    T - data type
    All Implemented Interfaces:
    java.io.Serializable

    public class TreeData<T>
    extends java.lang.Object
    implements java.io.Serializable
    Class for representing hierarchical data.

    Typically used as a backing data source for TreeDataProvider.

    Since:
    8.1
    Author:
    Vaadin Ltd
    See Also:
    Serialized Form
    • Constructor Summary

      Constructors 
      Constructor Description
      TreeData()
      Creates an initially empty hierarchical data representation to which items can be added or removed.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      TreeData<T> addItem​(T parent, T item)
      Adds a data item as a child of parent.
      TreeData<T> addItems​(java.util.Collection<T> rootItems, ValueProvider<T,​java.util.Collection<T>> childItemProvider)
      Adds the given items as root items and uses the given value provider to recursively populate children of the root items.
      TreeData<T> addItems​(java.util.stream.Stream<T> rootItems, ValueProvider<T,​java.util.stream.Stream<T>> childItemProvider)
      Adds the given items as root items and uses the given value provider to recursively populate children of the root items.
      TreeData<T> addItems​(T parent, java.util.Collection<T> items)
      Adds a list of data items as children of parent.
      TreeData<T> addItems​(T parent, java.util.stream.Stream<T> items)
      Adds data items contained in a stream as children of parent.
      TreeData<T> addItems​(T parent, T... items)
      Adds a list of data items as children of parent.
      TreeData<T> addRootItems​(java.util.Collection<T> items)
      Adds the items of the given collection as root items to this structure.
      TreeData<T> addRootItems​(java.util.stream.Stream<T> items)
      Adds the items of the given stream as root items to this structure.
      TreeData<T> addRootItems​(T... items)
      Adds the items as root items to this structure.
      TreeData<T> clear()
      Clear all items from this structure.
      boolean contains​(T item)
      Check whether the given item is in this hierarchy.
      java.util.List<T> getChildren​(T item)
      Get the immediate child items for the given item.
      T getParent​(T item)
      Get the parent item for the given item.
      java.util.List<T> getRootItems()
      Gets the root items of this structure.
      void moveAfterSibling​(T item, T sibling)
      Moves an item to the position immediately after a sibling item.
      TreeData<T> removeItem​(T item)
      Remove a given item from this structure.
      void setParent​(T item, T parent)
      Moves an item to become a child of the given parent item.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • TreeData

        public TreeData()
        Creates an initially empty hierarchical data representation to which items can be added or removed.
    • Method Detail

      • addRootItems

        public TreeData<T> addRootItems​(T... items)
        Adds the items as root items to this structure.
        Parameters:
        items - the items to add
        Returns:
        this
        Throws:
        java.lang.IllegalArgumentException - if any of the given items have already been added to this structure
        java.lang.NullPointerException - if any of the items are {code null}
      • addRootItems

        public TreeData<T> addRootItems​(java.util.Collection<T> items)
        Adds the items of the given collection as root items to this structure.
        Parameters:
        items - the collection of items to add
        Returns:
        this
        Throws:
        java.lang.IllegalArgumentException - if any of the given items have already been added to this structure
        java.lang.NullPointerException - if any of the items are {code null}
      • addRootItems

        public TreeData<T> addRootItems​(java.util.stream.Stream<T> items)
        Adds the items of the given stream as root items to this structure.
        Parameters:
        items - the stream of root items to add
        Returns:
        this
        Throws:
        java.lang.IllegalArgumentException - if any of the given items have already been added to this structure
        java.lang.NullPointerException - if any of the items are {code null}
      • addItem

        public TreeData<T> addItem​(T parent,
                                   T item)
        Adds a data item as a child of parent. Call with null as parent to add a root level item. The given parent item must already exist in this structure, and an item can only be added to this structure once.
        Parameters:
        parent - the parent item for which the items are added as children
        item - the item to add
        Returns:
        this
        Throws:
        java.lang.IllegalArgumentException - if parent is not null and not already added to this structure
        java.lang.IllegalArgumentException - if the item has already been added to this structure
        java.lang.NullPointerException - if item is null
      • addItems

        public TreeData<T> addItems​(T parent,
                                    T... items)
        Adds a list of data items as children of parent. Call with null as parent to add root level items. The given parent item must already exist in this structure, and an item can only be added to this structure once.
        Parameters:
        parent - the parent item for which the items are added as children
        items - the list of items to add
        Returns:
        this
        Throws:
        java.lang.IllegalArgumentException - if parent is not null and not already added to this structure
        java.lang.IllegalArgumentException - if any of the given items have already been added to this structure
        java.lang.NullPointerException - if any of the items are null
      • addItems

        public TreeData<T> addItems​(T parent,
                                    java.util.Collection<T> items)
        Adds a list of data items as children of parent. Call with null as parent to add root level items. The given parent item must already exist in this structure, and an item can only be added to this structure once.
        Parameters:
        parent - the parent item for which the items are added as children
        items - the collection of items to add
        Returns:
        this
        Throws:
        java.lang.IllegalArgumentException - if parent is not null and not already added to this structure
        java.lang.IllegalArgumentException - if any of the given items have already been added to this structure
        java.lang.NullPointerException - if any of the items are null
      • addItems

        public TreeData<T> addItems​(T parent,
                                    java.util.stream.Stream<T> items)
        Adds data items contained in a stream as children of parent. Call with null as parent to add root level items. The given parent item must already exist in this structure, and an item can only be added to this structure once.
        Parameters:
        parent - the parent item for which the items are added as children
        items - stream of items to add
        Returns:
        this
        Throws:
        java.lang.IllegalArgumentException - if parent is not null and not already added to this structure
        java.lang.IllegalArgumentException - if any of the given items have already been added to this structure
        java.lang.NullPointerException - if any of the items are null
      • addItems

        public TreeData<T> addItems​(java.util.Collection<T> rootItems,
                                    ValueProvider<T,​java.util.Collection<T>> childItemProvider)
        Adds the given items as root items and uses the given value provider to recursively populate children of the root items.
        Parameters:
        rootItems - the root items to add
        childItemProvider - the value provider used to recursively populate this TreeData from the given root items
        Returns:
        this
      • addItems

        public TreeData<T> addItems​(java.util.stream.Stream<T> rootItems,
                                    ValueProvider<T,​java.util.stream.Stream<T>> childItemProvider)
        Adds the given items as root items and uses the given value provider to recursively populate children of the root items.
        Parameters:
        rootItems - the root items to add
        childItemProvider - the value provider used to recursively populate this TreeData from the given root items
        Returns:
        this
      • removeItem

        public TreeData<T> removeItem​(T item)
        Remove a given item from this structure. Additionally, this will recursively remove any descendants of the item.
        Parameters:
        item - the item to remove, or null to clear all data
        Returns:
        this
        Throws:
        java.lang.IllegalArgumentException - if the item does not exist in this structure
      • clear

        public TreeData<T> clear()
        Clear all items from this structure. Shorthand for calling removeItem(Object) with null.
        Returns:
        this
      • getRootItems

        public java.util.List<T> getRootItems()
        Gets the root items of this structure.
        Returns:
        an unmodifiable list of root items of this structure
      • getChildren

        public java.util.List<T> getChildren​(T item)
        Get the immediate child items for the given item.
        Parameters:
        item - the item for which to retrieve child items for, null to retrieve all root items
        Returns:
        an unmodifiable list of child items for the given item
        Throws:
        java.lang.IllegalArgumentException - if the item does not exist in this structure
      • getParent

        public T getParent​(T item)
        Get the parent item for the given item.
        Parameters:
        item - the item for which to retrieve the parent item for
        Returns:
        parent item for the given item or null if the item is a root item.
        Throws:
        java.lang.IllegalArgumentException - if the item does not exist in this structure
        Since:
        8.1.1
      • setParent

        public void setParent​(T item,
                              T parent)
        Moves an item to become a child of the given parent item. The new parent item must exist in the hierarchy. Setting the parent to null makes the item a root item. After making changes to the tree data, AbstractDataProvider.refreshAll() should be called.
        Parameters:
        item - the item to be set as the child of parent
        parent - the item to be set as parent or null to set the item as root
        Since:
        8.1
      • moveAfterSibling

        public void moveAfterSibling​(T item,
                                     T sibling)
        Moves an item to the position immediately after a sibling item. The two items must have the same parent. After making changes to the tree data, AbstractDataProvider.refreshAll() should be called.
        Parameters:
        item - the item to be moved
        sibling - the item after which the moved item will be located, or null to move item to first position
        Since:
        8.1
      • contains

        public boolean contains​(T item)
        Check whether the given item is in this hierarchy.
        Parameters:
        item - the item to check
        Returns:
        true if the item is in this hierarchy, false if not