void replaceChild ( INode newChild ) { if ( children == null ) { throw new IllegalArgumentException ( "The directory is empty" ) ; } int low = Collections . binarySearch ( children , newChild . name ) ; if ( low >= 0 ) { // an old child exists so replace by the newChild INode oldChild = children . get ( low ) ; // Need to make sure we are replacing the oldChild with newChild that is the same as oldChild // which means they reference to the same children or they are null or empty array children . set ( low , newChild ) ; // newChild should point to current instance (parent of oldChild) newChild . parent = this ; // if both are directory, all the children from oldChild should point to newChild if ( newChild . isDirectory ( ) && oldChild . isDirectory ( ) ) { if ( ( ( INodeDirectory ) oldChild ) . getChildren ( ) != null ) { for ( INode oldGrandChild : ( ( INodeDirectory ) oldChild ) . getChildren ( ) ) { oldGrandChild . parent = ( INodeDirectory ) newChild ; } } } } else { throw new IllegalArgumentException ( "No child exists to be replaced" ) ; } }
Replace a child that has the same name as newChild by newChild . This is only working on one child case