public void insertBefore(Node node) { NodeImpl nodeImpl = (NodeImpl) node; if (logger.isDebugEnabled()) { logger.debug("Inserting " + this + " before " + nodeImpl); } if (CHECK_INVARIANTS) { checkIsolated(); nodeImpl.checkInvariants(); } // Update this node's links. previous = nodeImpl.previous; next = nodeImpl; parent = nodeImpl.parent; // Update the links in the tree. if (previous == null) { // This node is the first in the list so we need to make it the head. parent.head = this; } else { // Add a link forward to this node. previous.next = this; } // Make this node the previous node before the one in the tree. nodeImpl.previous = this; if (CHECK_INVARIANTS) { nodeImpl.checkInvariants(); } }
public void insertAfter(Node node) { NodeImpl nodeImpl = (NodeImpl) node; if (logger.isDebugEnabled()) { logger.debug("Inserting " + this + " after " + nodeImpl); } if (CHECK_INVARIANTS) { checkIsolated(); nodeImpl.checkInvariants(); } // Update this node's links. previous = nodeImpl; next = nodeImpl.next; parent = nodeImpl.parent; // Update the links in the tree. if (next == null) { // This node is the last in the list so we need to make this node // the tail. parent.tail = this; } else { // Add a link back to this node. next.previous = this; } // Make this node the next node after the one in the tree. nodeImpl.next = this; if (CHECK_INVARIANTS) { nodeImpl.checkInvariants(); } }
// Javadoc inherited. public void replaceWith(NodeSequence sequence) { if (CHECK_INVARIANTS) { checkInvariants(); } PrivateNodeSequence s = (PrivateNodeSequence) sequence; // If the sequence is empty then simply remove this node and return // immediately. NodeImpl first = (NodeImpl) s.getFirst(); if (first == null) { remove(); return; } // Make sure that the node being replaced is not in the sequence as // that would corrupt the DOM. Node end = s.getEnd(); for (NodeImpl node = first; node != end; node = node.next) { if (this == node) { throw new IllegalStateException("Cannot replace node with sequence containing node"); } // Update the parent. node.parent = parent; } NodeImpl last = (NodeImpl) s.getLast(); if (previous == null) { parent.head = first; } else { previous.next = first; } first.previous = previous; if (next == null) { parent.tail = last; } else { next.previous = last; } last.next = next; if (CHECK_INVARIANTS) { checkInvariants(); } }
void checkWholeTree() { NodeImpl root = this; while (root.parent != null) { root = root.parent; } root.checkInvariants(); }
public void addToHead(Element element) { if (logger.isDebugEnabled()) { logger.debug("Adding " + this + " to head of " + element); } if (CHECK_INVARIANTS) { checkInvariants(); } element.addHead(this); if (CHECK_INVARIANTS) { checkInvariants(); } }
void checkIsolated() { if (parent != null) { throw new IllegalStateException( this + ": Isolated node should have a null parent but it is " + parent); } if (next != null) { throw new IllegalStateException( this + ": Isolated node should have a null next but it is " + next); } if (previous != null) { throw new IllegalStateException( this + ": Isolated node should have a null previous but it is " + previous); } checkInvariants(); }
public void remove() { if (logger.isDebugEnabled()) { logger.debug("Removing " + this); } if (CHECK_INVARIANTS) { checkInvariants(); } if (previous == null) { // This node is the first in the list so update the parent element's // head to reference the next node. parent.head = next; } else { // Update the previous node's next link so that it references the node // after this one. previous.next = next; } if (next == null) { // This node is the last in the list so update the parent element's // tail to reference the previous node. parent.tail = previous; } else { // Update the next node's previous link so that it references the node // before this one. next.previous = previous; } ElementImpl oldParent = parent; // Reset this node. previous = null; parent = null; next = null; if (CHECK_INVARIANTS) { oldParent.checkInvariants(); checkIsolated(); } }
/** * Replace this node with the specified node. * * @param node The node which will replace this one. */ private void replaceWith(NodeImpl node) { if (logger.isDebugEnabled()) { logger.debug("Replacing " + this + " with " + node); } if (CHECK_INVARIANTS) { node.checkIsolated(); checkInvariants(); } if (previous == null) { parent.head = node; } else { previous.next = node; } if (next == null) { parent.tail = node; } else { next.previous = node; } node.parent = parent; node.next = next; node.previous = previous; parent = null; next = null; previous = null; if (CHECK_INVARIANTS) { node.checkInvariants(); checkIsolated(); } }