Code:
/*
* Node.java
*
* Created on December 26, 2007, 4:23 AM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
import java.util.*;
/**
*
* @author isaac
*/
public class Node<V> implements Cloneable{
NodeItem item;
Node parent;
HashMap<String,Node> map;
public String str;
/** Creates a new instance of Node */
public Node() {
this.map=new HashMap<String,Node>();
this.item=null;
Set<String> s=map.keySet();
Iterator<String> i=s.iterator();
}
public Node(Node nod){
this.item=nod.item;
this.map=new HashMap<String,Node>();
this.map= nod.map;
this.parent=nod.parent;
this.str=nod.str;
}
public Node getParent(){ return this.parent; }
public void setParent(Node nd) { this.parent=nd; }
public void setItem(NodeItem a){this.item=a;}
public void dene() throws CloneNotSupportedException{
Node<String> n1=new Node<String>();
n1.str="1";
NodeItem<Integer> it1=new NodeItem<Integer>(2,"2",n1);
n1.item=it1;
Node<String> n2=new Node<String>();
n2.str="2";
n2.item=it1;
n2.parent=n1;
Node<String> n3=new Node<String>();
n3.str="3";
n3.item=it1;
n3.parent=n2;
Node<String> n7=new Node<String>();
n7.str="7";
n7.item=it1;
n7.parent=n3;
n3.map.put("key",n7);
n2.map.put("key",n3);
n1.map.put("key",n2);
Node<String> n4=new Node<String>(n3);
NodeItem<Integer> it6=new NodeItem<Integer>(1,"1",n4);
n4.str="4";
n4.item=it6;
Node<String> n6 =new Node<String>();
n6.str="6";
n6.item=it6;
n4.parent=n6;
n4.map.get("key").parent=n4;
n4.map.get("key").item=it6;
n6.map.put("key2",n4);
}
private class NodeItem<V> {
String key;
V info;
Node x;
public NodeItem(V v, String k,Node x1){
this.info=v;
this.key=k;
this.x=x1;
}
}
}
HI TO ALL...
aS YOU CAN SEE FROM THE CODE OF DENE() METHOD OF THIS CLASS I AM TRYING TO GET A DEEP COPY OF VARIABLE N3.. THAT IS WHEN I COPY THE CONTENTS OF N3 TO N4 IN THE DENE() METHOD I ALSO WANT THAT NOT THE HANDLES BUT ALL THE OBJECT CONTENTS OF N3 TO BE COPIED TO N4.. WHEN THIS DONE I WANT THAT WHEN I EXECUTE THE n4.map.get("key").parent=n4; LINE AND THEN ASK FOR WHAT THE VALUE OF n3.map.get("key").parent be n2 ...
in other words let me explain my problem thus...
i try to build a directory hierarchy system.. i want to include the recurs,ive copy of directories...
as it can be seen in the code of dene() method i first create a hierarchy -from top to down - n1->n2-> n3-> n7 ... and then i create a new directory n6.. say it will be placed next to n1 for example.. it is not important all i want is that n6 should be another root directory just like n1.. and then i copy n3 to n4 and place it under n6.. but when i do only this then n7 becomes shared between n3 and n4 and when the line
n4.map.get("key").parent=n4;
executes.. the parent of n7 under n3 is also changed but i do not want it to change.. and also when i copy n3 to n4 without executing the line
n4.map.get("key").parent=n4;
the n7 under n4 sees n3 as parent..
Now all that i want to achieve is to copy the objects themselves not the handles so that when i copy a directory structure form another and paste it to another place there would not be any shared directories so that when i execute the line n4.map.get("key").parent=n4; the parent of n7 under n3 should stay as n3 and the parent of n7 under n4 should be modified to n4...
i used the clone method for
Code:
this.map=(HashMap<String, Node>) nod.map.clone();
copying the map of n3 but nothing changed..
then i used the copy constructor of hashmap class as
Code:
this.map=new HashMap<String,Node>(nod.map);
but again nothing changed in my issue..
when i try to modify the parent, item, str fields of n4 just after copying it from n3 the corresponding fields of n3 are not changed but when i try to get the node info in the hashmap and modify it then the modification is done in two places..
WHY?
i want to copy all the objects of n3 including its hashmap and the objects in the hashmap ... and if the objects of the hashmap has non-empty hashmaps i also want them to be coppied as objects not handles or references...
so that when i copy a directory info from an existing one and modify the new parent folder to the folders under the one i copied the original parent informations of subdirectories should not be changed...
and of course if n7 too has some subdirectories they would also be copied as objects , not the handles..
ANY SUGGESTIONS???
thank you...