private static Node<Container> replace(Node<Container> list, Container old, AbstractConfigValue replacement) {
        Container child = list.head();
        if (child != old)
            throw new ConfigException.BugOrBroken("Can only replace() the top node we're resolving; had " + child
                    + " on top and tried to replace " + old + " overall list was " + list);
        Container parent = list.tail() == null ? null : list.tail().head();
        if (replacement == null || !(replacement instanceof Container)) {
            if (parent == null) {
                return null;
            } else {
                /*
                 * we are deleting the child from the stack of containers
                 * because it's either going away or not a container
                 */
                AbstractConfigValue newParent = parent.replaceChild((AbstractConfigValue) old, null);

                return replace(list.tail(), parent, newParent);
            }
        } else {
            /* we replaced the container with another container */
            if (parent == null) {
                return new Node<Container>((Container) replacement);
            } else {
                AbstractConfigValue newParent = parent.replaceChild((AbstractConfigValue) old, replacement);
                Node<Container> newTail = replace(list.tail(), parent, newParent);
                if (newTail != null)
                    return newTail.prepend((Container) replacement);
                else
                    return new Node<Container>((Container) replacement);
            }
        }
    }
private static Node < Container > replace ( Node < Container > list , Container old , AbstractConfigValue replacement ) { Container child = list . head ( ) ; if ( child != old ) throw new ConfigException . BugOrBroken ( "Can only replace() the top node we're resolving; had " + child + " on top and tried to replace " + old + " overall list was " + list ) ; Container parent = list . tail ( ) == null ? null : list . tail ( ) . head ( ) ; if ( replacement == null || ! ( replacement instanceof Container ) ) { if ( parent == null ) { return null ; } else { AbstractConfigValue newParent = parent . replaceChild ( ( AbstractConfigValue ) old , null ) ; return replace ( list . tail ( ) , parent , newParent ) ; } } else { if ( parent == null ) { return new Node < Container > ( ( Container ) replacement ) ; } else { AbstractConfigValue newParent = parent . replaceChild ( ( AbstractConfigValue ) old , replacement ) ; Node < Container > newTail = replace ( list . tail ( ) , parent , newParent ) ; if ( newTail != null ) return newTail . prepend ( ( Container ) replacement ) ; else return new Node < Container > ( ( Container ) replacement ) ; } } }
returns null if the replacement results in deleting all the nodes.