Package cdc.util.data

Interface Parent

  • All Superinterfaces:
    Node
    All Known Implementing Classes:
    Document, Element

    public interface Parent
    extends Node
    Interface implemented by nodes that are parents (they have children nodes): Document and Element.
    Author:
    Damien Carbonne
    • Method Detail

      • clone

        Parent clone​(boolean recurse)
        Description copied from interface: Node
        Return a clone of this node.
        Specified by:
        clone in interface Node
        Parameters:
        recurse - If true, also clone children nodes.
        Returns:
        A clone of this node, and optionally its children.
      • canAddChild

        boolean canAddChild​(Child child)
        Returns true when a child can be added.
        Parameters:
        child - The child
        Returns:
        true when child can be added.
      • getModifiableChildren

        List<Child> getModifiableChildren()
      • getChildren

        List<? extends Child> getChildren()
        Returns:
        A list of children.
      • getChildren

        default <T extends NodeIterable<T> getChildren​(Class<T> childClass)
        Returns an Iterable of children that are instance of a given class.
        Type Parameters:
        T - Type of searched children.
        Parameters:
        childClass - The class of searched children.
        Returns:
        An Iterable of children that are instance of childClass.
      • getChildren

        default <T extends NodeIterable<T> getChildren​(Class<T> childClass,
                                                         Predicate<? super T> predicate)
        Returns an Iterable of children that are instance of a given class and match a predicate.
        Type Parameters:
        T - Type of searched children.
        Parameters:
        childClass - The class of searched children.
        predicate - The predicate.
        Returns:
        An Iterable of children that are instance of childClass and match predicate.
      • getChild

        default <T extends Node> T getChild​(Class<T> childClass,
                                            Predicate<? super T> predicate)
      • getChild

        default <T extends Node> T getChild​(Class<T> childClass)
      • getChildAt

        default <T extends Node> T getChildAt​(Class<T> childClass,
                                              Predicate<? super T> predicate,
                                              int index)
        Returns the child that is an instance of a given class, matches a Predicate and is at a given position.
        Type Parameters:
        T - Type of searched children.
        Parameters:
        childClass - The class of searched children.
        predicate - The predicate.
        index - The index of the searched child. This index takes into account elements that match childClass and predicate.
        Returns:
        The element matching childClass and predicate and at index, or null.
      • getChildAt

        default <T extends Node> T getChildAt​(Class<T> childClass,
                                              int index)
        Returns the child that is an instance of a given class and is at a given position.
        Type Parameters:
        T - Type of searched children.
        Parameters:
        childClass - The class of searched children.
        index - The index of the searched child. This index takes into account elements that match childClass.
        Returns:
        The element matching childClass and at index, or null.
      • getElements

        default Iterable<Element> getElements()
        Returns:
        An Iterable of children elements.
      • hasElements

        default boolean hasElements()
        Returns:
        true if this parent has children elements.
      • getElementsNamed

        default Iterable<Element> getElementsNamed​(String name)
        Returns an Iterable of children elements that have a given name.
        Parameters:
        name - The name.
        Returns:
        An Iterable containing elements named name.
      • getElementNamedAt

        default Element getElementNamedAt​(String name,
                                          int index)
        Returns the child element that has a given name and index.
        Parameters:
        name - The name.
        index - The index of the searched child element. This index takes into account children that are elements and are named name.
        Returns:
        The corresponding element or null.
      • getElementNamed

        default Element getElementNamed​(String name)
        Returns the first child element that has a given name or null.
        Parameters:
        name - The name.
        Returns:
        The first child element named name or null.
      • getElementsNamedIgnoreCase

        default Iterable<Element> getElementsNamedIgnoreCase​(String name)
        Returns an Iterable of children elements that have a given name, ignoring case.
        Parameters:
        name - The name.
        Returns:
        An Iterable containing elements named name ignoring case.
      • getElementNamedIgnoreCaseAt

        default Element getElementNamedIgnoreCaseAt​(String name,
                                                    int index)
        Returns the child element that has a given name, ignoring case, and index.
        Parameters:
        name - The name.
        index - The index of the searched child element. This index takes into account children that are elements and are named name ignoring case.
        Returns:
        The corresponding element or null.
      • getElementNamedIgnoreCase

        default Element getElementNamedIgnoreCase​(String name)
        Returns the first child element that has a given name, ignoring case, or null.
        Parameters:
        name - The name.
        Returns:
        The first child element named name, ignoring case, or null.
      • getElementNamedWithAttribute

        default Element getElementNamedWithAttribute​(String name,
                                                     String attributeName,
                                                     String attributeValue)
      • getTexts

        default Iterable<Text> getTexts()
        Returns:
        An Iterable if text children.
      • hasTexts

        default boolean hasTexts()
        Returns:
        true if this parent has texts children.
      • getComments

        default Iterable<Comment> getComments()
        Returns:
        An Iterable if comment children.
      • hasComments

        default boolean hasComments()
        Returns:
        true if this parent has comments children.
      • getChildrenCount

        default <T extends Node> int getChildrenCount​(Class<T> childClass,
                                                      Predicate<? super T> predicate)
        Returns the number of children that match a class and predicate.
        Type Parameters:
        T - Type of counted children.
        Parameters:
        childClass - The class of counted children.
        predicate - The predicate.
        Returns:
        The number of children that match childClass and predicate.
      • getChildrenCount

        default int getChildrenCount​(Class<? extends Node> childClass)
        Returns the number of children that match a class.
        Parameters:
        childClass - The class of counted children.
        Returns:
        The number of children that match childClass.
      • getChildrenCount

        default int getChildrenCount()
        Returns:
        The number of children.
      • hasChildren

        default <T extends Node> boolean hasChildren​(Class<T> childClass,
                                                     Predicate<? super T> predicate)
        Returns true if this parent has children of a given class and matching a predicate.
        Type Parameters:
        T - Type of searched children.
        Parameters:
        childClass - The child class.
        predicate - The predicate.
        Returns:
        true if this parent has children of childClass class matching predicate.
      • hasChildren

        default boolean hasChildren​(Class<? extends Node> childClass)
        Returns true if this parent has children of a given class.
        Parameters:
        childClass - The child class.
        Returns:
        true if this parent has children of childClass class.
      • hasChildren

        default boolean hasChildren()
        Returns:
        true if this parent has children.
      • getChildAt

        default Child getChildAt​(int index)
        Returns the child at a given, index or null.

        null is returned when index is invalid.

        Parameters:
        index - The index.
        Returns:
        The child at index.
        Throws:
        IndexOutOfBoundsException - When index is out of range (index < 0 || index >= getChildrenCount()).
      • getLastChild

        default Child getLastChild()
        Returns:
        The last child, if any, or null.
      • addChild

        default <C extends Child> C addChild​(C child)
        Adds a child to this parent.

        If possible, child current parent is changed to this node.

        Type Parameters:
        C - Type of the child.
        Parameters:
        child - The child. MUST NOT be null.
        Returns:
        The input child.
        Throws:
        IllegalArgumentException - If child is null.
        IllegalStateException - If child's parent can not be set.
      • addElement

        default Element addElement​(String name)
        Creates and adds a child element as last child, if possible.
        Parameters:
        name - The child name.
        Returns:
        the created element.
        Throws:
        IllegalStateException - If child's parent can not be set.
      • addComment

        default Comment addComment​(String content,
                                   boolean merge)
        Adds a comment as last child.
        Parameters:
        content - The comment content.
        merge - If true, then if last child exists and is a comment, content is added to this last child.
        Otherwise, a comment child is created and added to last position.
        Returns:
        The modified or created comment.
      • addComment

        default Comment addComment​(String content)
        Adds a comment as last child.

        If last child exists and is a comment, content is added to this last child.
        Otherwise, a comment child is created and added to last position.

        Parameters:
        content - The comment content.
        Returns:
        The modified or created comment.
      • addChildren

        default void addChildren​(Iterable<? extends Child> children)
        Adds all children of an Iterable to this parent.
        Parameters:
        children - The children.
      • addChildren

        default void addChildren​(Child... children)
        Adds all children of an array to this parent.
        Parameters:
        children - The children.
      • removeChild

        default boolean removeChild​(Child child)
        Remove a child.

        If child is not a child of this parent, then false is returned.

        Parameters:
        child - The child.
        Returns:
        true if child was removed, false otherwise.
      • removeChildAt

        default Child removeChildAt​(int index)
        Removes the child located at a given index.

        If index is invalid, returns false.

        Parameters:
        index - The index (0-based).
        Returns:
        The removed child.
        Throws:
        IndexOutOfBoundsException - When index is out of range (index < 0 || index >= getChildrenCount()).
      • removeChildren

        default void removeChildren()
        Removes all children locally.
      • removeChildren

        default void removeChildren​(Predicate<? super Child> predicate)
        Removes all children that match a predicate.

        This does only a shallow removal.
        For deep removal, use DataUtils.

        Parameters:
        predicate - The predicate.
      • removeChildren

        default void removeChildren​(Predicate<? super Child> predicate,
                                    boolean pre,
                                    boolean recurse)
        Removes all children that match a predicate, recursively.
        Parameters:
        predicate - The predicate.
        pre - If true, removal is applied before recursion, after otherwise.
        recurse - If true, then this is applied recursively.
      • removeElementsNamed

        default void removeElementsNamed​(String name)
        Removes all children elements that have a given name.
        Parameters:
        name - The name of elements to remove.
      • removeElementsNamed

        default void removeElementsNamed​(String name,
                                         boolean recurse)
        Removes all children elements that have a given name, recursively.
        Parameters:
        name - The name of elements to remove.
        recurse - If true, then this is applied recursively.
      • removeComments

        default void removeComments()
        Removes all comments locally.
      • removeComments

        default void removeComments​(boolean recurse)
        Removes all comments recursively.
        Parameters:
        recurse - If true, then this is applied recursively.
      • removeTexts

        default void removeTexts()
        Removes all texts locally.
      • removeTexts

        default void removeTexts​(boolean recurse)
        Removes all texts recursively.
        Parameters:
        recurse - If true, then this is applied recursively.
      • removeIgnorableTexts

        default void removeIgnorableTexts()
        Removes all ignorable texts locally.
      • removeIgnorableTexts

        default void removeIgnorableTexts​(boolean recurse)
        Removes all ignorable texts recursively.
        Parameters:
        recurse - If true, then this is applied recursively.
      • sortChildren

        default void sortChildren​(Comparator<? super Child> comparator)
        Sorts children locally.
        Parameters:
        comparator - The comparator.
      • sortChildren

        default void sortChildren​(Comparator<? super Child> comparator,
                                  boolean recurse)
        Sorts children recursively.
        Parameters:
        comparator - The comparator.
        recurse - If true, then this is applied recursively.
      • mergeTexts

        default void mergeTexts()
        Merges all consecutive texts locally.
      • mergeTexts

        default void mergeTexts​(boolean recurse)
        Merges all consecutive texts recursively.
        Parameters:
        recurse - If true, then this is applied recursively.
      • mergeComments

        default void mergeComments()
        Merges all consecutive comments locally.
      • mergeComments

        default void mergeComments​(boolean recurse)
        Merges all consecutive comments recursively.
        Parameters:
        recurse - If true, then this is applied recursively.
      • changeTexts

        default void changeTexts​(Function<String,​String> modifier)
        Changes all texts locally.
        Parameters:
        modifier - A function that take content and returns new content.
      • changeTexts

        default void changeTexts​(Function<String,​String> modifier,
                                 boolean recurse)
        Changes all texts recursively.
        Parameters:
        modifier - A function that take content and returns new content.
        recurse - If true, then this is applied recursively.
      • changeNamedTexts

        default void changeNamedTexts​(String name,
                                      Function<String,​String> modifier)
        Changes or set text under element that have a given name, locally.

        This will work on elements that are empty or have text and comment children.
        It will ignore elements that have children elements.

        Children nodes are preserved if possible.
        Otherwise, they are all removed and replace if necessary.
        WARNING: This may remove comments.

        Parameters:
        name - The element name.
        modifier - A function that take content and returns new content.
      • changeNamedTexts

        default void changeNamedTexts​(String name,
                                      Function<String,​String> modifier,
                                      boolean recurse)
        Changes or set text under element that have a given name, recursively.
        Parameters:
        name - The element name.
        modifier - A function that take content and returns new content.
        recurse - If true, then this is applied recursively.
      • changeComments

        default void changeComments​(Function<String,​String> modifier)
        Changes all comments locally.
        Parameters:
        modifier - A function that take content and returns new content.
      • changeComments

        default void changeComments​(Function<String,​String> modifier,
                                    boolean recurse)
        Changes all comments recursively.
        Parameters:
        modifier - A function that take content and returns new content.
        recurse - If true, then this is applied recursively.