Class TreeData<T>

    • 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:
        IllegalArgumentException - if any of the given items have already been added to this structure
        NullPointerException - if any of the items are {code null}
      • addRootItems

        public TreeData<T> addRootItems​(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:
        IllegalArgumentException - if any of the given items have already been added to this structure
        NullPointerException - if any of the items are {code null}
      • addRootItems

        public TreeData<T> addRootItems​(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:
        IllegalArgumentException - if any of the given items have already been added to this structure
        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:
        IllegalArgumentException - if parent is not null and not already added to this structure
        IllegalArgumentException - if the item has already been added to this structure
        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:
        IllegalArgumentException - if parent is not null and not already added to this structure
        IllegalArgumentException - if any of the given items have already been added to this structure
        NullPointerException - if any of the items are null
      • addItems

        public TreeData<T> addItems​(T parent,
                                    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:
        IllegalArgumentException - if parent is not null and not already added to this structure
        IllegalArgumentException - if any of the given items have already been added to this structure
        NullPointerException - if any of the items are null
      • addItems

        public TreeData<T> addItems​(T parent,
                                    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:
        IllegalArgumentException - if parent is not null and not already added to this structure
        IllegalArgumentException - if any of the given items have already been added to this structure
        NullPointerException - if any of the items are null
      • addItems

        public TreeData<T> addItems​(Collection<T> rootItems,
                                    ValueProvider<T,​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​(Stream<T> rootItems,
                                    ValueProvider<T,​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:
        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 List<T> getRootItems()
        Gets the root items of this structure.
        Returns:
        an unmodifiable list of root items of this structure
      • getChildren

        public 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:
        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:
        IllegalArgumentException - if the item does not exist in this structure
      • 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
      • 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
      • 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