private void pruneEmptyContainers()

in src/main/java/org/apache/commons/net/nntp/Threader.java [307:359]


    private void pruneEmptyContainers(final NntpThreadContainer parent) {
        NntpThreadContainer container;
        NntpThreadContainer prev;
        NntpThreadContainer next;
        for (prev = null, container = parent.child, next = container.next; container != null; prev = container, container = next, next = container == null
                ? null
                : container.next) {

            // Is it empty and without any children? If so,delete it
            if (container.threadable == null && container.child == null) {
                if (prev == null) {
                    parent.child = container.next;
                } else {
                    prev.next = container.next;
                }
                // Set container to prev so that prev keeps its same value the next time through the loop
                container = prev;
                // Else if empty, with kids, and (not at root or only one kid)
            } else if (container.threadable == null && (container.parent != null || container.child.next == null)) {
                // We have an invalid/expired message with kids. Promote the kids to this level.
                NntpThreadContainer tail;
                final NntpThreadContainer kids = container.child;

                // Remove this container and replace with 'kids'.
                if (prev == null) {
                    parent.child = kids;
                } else {
                    prev.next = kids;
                }

                // Make each child's parent be this level's parent -> i.e. promote the children.
                // Make the last child's next point to this container's next
                // i.e. splice kids into the list in place of container
                for (tail = kids; tail.next != null; tail = tail.next) {
                    tail.parent = container.parent;
                }

                tail.parent = container.parent;
                tail.next = container.next;

                // next currently points to the item after the inserted items in the chain - reset that, so we process the newly
                // promoted items next time round
                next = kids;

                // Set container to prev so that prev keeps its same value the next time through the loop
                container = prev;
            } else if (container.child != null) {
                // A real message , with kids
                // Iterate over the children
                pruneEmptyContainers(container);
            }
        }
    }