@Test public void replace() { final Random random = new Random(124); final TreeNode<Integer> tree = newTree(5, random); final TreeNode<Integer> tree1 = newTree(2, random); final TreeNode<Integer> child = tree.childAtPath(0 , 1) .orElseThrow(AssertionError::new); Assert.assertNotEquals(child, tree1); child.replace(0, tree1); Assert.assertEquals(child.childAt(0), tree1); }
public TreeNode<T> replace(final int index, final TreeNode<T> child) { requireNonNull(child); if (_children == null) { throw new ArrayIndexOutOfBoundsException(format( "Child index is out of bounds: %s", index )); } if (isAncestor(child)) { throw new IllegalArgumentException("The new child is an ancestor."); } final TreeNode<T> oldChild = _children.set(index, child); assert oldChild != null; assert oldChild._parent == this; oldChild.parent(null); child.parent(this); return this; }
TreeNode implements
		Tree<T, TreeNode<T>>,
		Iterable<TreeNode<T>>,
		Copyable<TreeNode<T>>,
		Serializable { public TreeNode<T> replace(final int index, final TreeNode<T> child) { requireNonNull(child); if (_children == null) { throw new ArrayIndexOutOfBoundsException(format( "Child index is out of bounds: %s", index )); } if (isAncestor(child)) { throw new IllegalArgumentException("The new child is an ancestor."); } final TreeNode<T> oldChild = _children.set(index, child); assert oldChild != null; assert oldChild._parent == this; oldChild.parent(null); child.parent(this); return this; } }
TreeNode implements
		Tree<T, TreeNode<T>>,
		Iterable<TreeNode<T>>,
		Copyable<TreeNode<T>>,
		Serializable { public TreeNode<T> replace(final int index, final TreeNode<T> child) { requireNonNull(child); if (_children == null) { throw new ArrayIndexOutOfBoundsException(format( "Child index is out of bounds: %s", index )); } if (isAncestor(child)) { throw new IllegalArgumentException("The new child is an ancestor."); } final TreeNode<T> oldChild = _children.set(index, child); assert oldChild != null; assert oldChild._parent == this; oldChild.parent(null); child.parent(this); return this; } private  TreeNode(final T value); }
TreeNode implements
		Tree<T, TreeNode<T>>,
		Iterable<TreeNode<T>>,
		Copyable<TreeNode<T>>,
		Serializable { public TreeNode<T> replace(final int index, final TreeNode<T> child) { requireNonNull(child); if (_children == null) { throw new ArrayIndexOutOfBoundsException(format( "Child index is out of bounds: %s", index )); } if (isAncestor(child)) { throw new IllegalArgumentException("The new child is an ancestor."); } final TreeNode<T> oldChild = _children.set(index, child); assert oldChild != null; assert oldChild._parent == this; oldChild.parent(null); child.parent(this); return this; } private  TreeNode(final T value); void value(final T value); @Override T value(); @Override Optional<TreeNode<T>> parent(); @Override TreeNode<T> childAt(final int index); @Override int childCount(); @Override Iterator<TreeNode<T>> childIterator(); @Override Stream<TreeNode<T>> childStream(); TreeNode<T> insert(final int index, final TreeNode<T> child); TreeNode<T> replace(final int index, final TreeNode<T> child); TreeNode<T> remove(final int index); boolean removeAtPath(final Path path); boolean replaceAtPath(final Path path, final TreeNode<T> child); TreeNode<T> detach(); void remove(final Tree<?, ?> child); void removeAllChildren(); TreeNode<T> attach(final TreeNode<T> child); @SafeVarargs final TreeNode<T> attach(final T... children); TreeNode<T> attach(final T child); @Override TreeNode<T> copy(); TreeNode<B> map(final Function<? super T, ? extends B> mapper); @Override int hashCode(); @Override boolean equals(final Object obj); @Override String toString(); static TreeNode<T> of(); static TreeNode<T> of(final T value); static TreeNode<B> ofTree(
		final Tree<? extends T, ?> tree,
		final Function<? super T, ? extends B> mapper
	); static TreeNode<T> ofTree(final Tree<? extends T, ?> tree); static TreeNode<String> parse(final String tree); static TreeNode<B> parse(
		final String tree,
		final Function<? super String, ? extends B> mapper
	); }
TreeNode implements
		Tree<T, TreeNode<T>>,
		Iterable<TreeNode<T>>,
		Copyable<TreeNode<T>>,
		Serializable { public TreeNode<T> replace(final int index, final TreeNode<T> child) { requireNonNull(child); if (_children == null) { throw new ArrayIndexOutOfBoundsException(format( "Child index is out of bounds: %s", index )); } if (isAncestor(child)) { throw new IllegalArgumentException("The new child is an ancestor."); } final TreeNode<T> oldChild = _children.set(index, child); assert oldChild != null; assert oldChild._parent == this; oldChild.parent(null); child.parent(this); return this; } private  TreeNode(final T value); void value(final T value); @Override T value(); @Override Optional<TreeNode<T>> parent(); @Override TreeNode<T> childAt(final int index); @Override int childCount(); @Override Iterator<TreeNode<T>> childIterator(); @Override Stream<TreeNode<T>> childStream(); TreeNode<T> insert(final int index, final TreeNode<T> child); TreeNode<T> replace(final int index, final TreeNode<T> child); TreeNode<T> remove(final int index); boolean removeAtPath(final Path path); boolean replaceAtPath(final Path path, final TreeNode<T> child); TreeNode<T> detach(); void remove(final Tree<?, ?> child); void removeAllChildren(); TreeNode<T> attach(final TreeNode<T> child); @SafeVarargs final TreeNode<T> attach(final T... children); TreeNode<T> attach(final T child); @Override TreeNode<T> copy(); TreeNode<B> map(final Function<? super T, ? extends B> mapper); @Override int hashCode(); @Override boolean equals(final Object obj); @Override String toString(); static TreeNode<T> of(); static TreeNode<T> of(final T value); static TreeNode<B> ofTree(
		final Tree<? extends T, ?> tree,
		final Function<? super T, ? extends B> mapper
	); static TreeNode<T> ofTree(final Tree<? extends T, ?> tree); static TreeNode<String> parse(final String tree); static TreeNode<B> parse(
		final String tree,
		final Function<? super String, ? extends B> mapper
	);  }