I am working on some code that I want to detect when something in the object has changed. One of the easiest ways to track changes is to keep an old copy of the object around. However, it looks like getting a copy of the deep graph may be difficult. Here's what I tried:
public class Old{
protected Old old;
protected List stuff;
//Needed for JUnit
public Old(){
}
public Old(List stuff){
this.stuff=stuff;
old=this;
}
public void add(){
stuff.add(2);
}
public void match(){
System.out.printf("old:%d\nnew:%d\n",old.getStuff().size(),stuff.size());
}
public List getStuff(){
return new ArrayList(stuff);
}
@Test
public void testOld(){
List list=new ArrayList();
list.add(1);
Old thing=new Old(list);
thing.add();
thing.match();
}
}
The Output:
old:2 new:2
So, it appears that by default old=this
does not create a deep copy. All I really want is to track changes to that list and potentially a graph of objects. What are some other simple options?