I am making some simulations with java. I am trying to simulate, several steps ahead, a node of a tree and then discard all changes and go back to original state. But the clone() does not return me proper resuts.
Node class:
public class TreeNode implements Serializable,Cloneable{
HashMap<Integer, Tracker> trackers;
public Object clone(){
try{
TreeNode node = (TreeNode) super.clone();
HashMap<Integer, Tracker> newTrackers = new HashMap<Integer, Tracker>;
for (int i=0:i<4:i++){
newTrackers.put(i, node.trackers.get(i).clone());
}
node.trackers = newTrackers;
return node;
}catch(CloneNotSupportException e){
}
return null;
}
public run(){
TreeNode current = root;
TreeNode CopyNode = (TreeNode) current.clone();
foo(CopyNode);
//Here both current and CopyNode have the same changes at trackers
//made by foo()
}
}
Tracker class:
public class Tracker implements Serializable,Cloneable{
private final Player player;
public Tracker clone(){
try{
Tracker newTracker = (Tracker) super.clone();
newTracker.player = player.clone();
return newTracker;
} catch (CloneNotSupportException e){
}
return null;
}
Player Class:
public class Player implements Serializable,Cloneable{
private int points;
public Player clone(){
try{
return (Player) super.clone();
}catch (CloneNotSupportException e){
}
return null;
}
}
Note: I cannot use apache functions for this like org.apache.commons.lang.SerializationUtils