/** * Removes all children that match a predicate. * <p> * This does only a shallow removal.<br> * For deep removal, use {@link cdc.util.data.util.DataUtil}. * * @param predicate The predicate. */ public default void removeChildren(Predicate<? super Child> predicate) { List<Integer> indices = null; // Add candidates to removal in reverse order // Better performances for removal for (int index = getChildrenCount() - 1; index >= 0; index--) { final Child child = getChildAt(index); if (predicate.test(child)) { if (indices == null) { indices = new ArrayList<>(); } indices.add(index); } } if (indices != null) { for (final int index : indices) { final Child removed = removeChildAt(index); assert removed != null; } } }
/** * Merges all consecutive texts locally. */ public default void mergeTexts() { if (getChildrenCount() > 1) { Child ref = getChildAt(0); int index = 1; while (index < getChildrenCount()) { final Child next = getChildAt(index); if (ref.getType() == NodeType.TEXT && next.getType() == NodeType.TEXT) { // ref and next are both texts: merge them ((Text) ref).appendContent(((Text) next).getContent()); // Remove next next.detach(); // Do not change index and ref } else { ref = next; index++; } } } }
/** * Merges all consecutive comments locally. */ public default void mergeComments() { if (getChildrenCount() > 1) { Child ref = getChildAt(0); int index = 1; while (index < getChildrenCount()) { final Child next = getChildAt(index); if (ref.getType() == NodeType.COMMENT && next.getType() == NodeType.COMMENT) { // ref and next are both texts: merge them ((Comment) ref).appendContent(((Comment) next).getContent()); // Remove next next.detach(); // Do not change index and ref } else { ref = next; index++; } } } }