title: Java auto update all references when replacing (not changing) object?
body: <p>So I have an ArrayList of objects of my own class:</p>
  
  <pre><code>public class A
  {...}
  </code></pre>
  
  <p>Now say I have a variable that holds one of these objects from the list. Now I know if I change that object (e.g. set a field on that object), that this changes the memory that holds the object itsself, so both references (the variable and the internal array variable that holds the object in the list) are still identical and both still show to the same, now modified, object. Normal behavior!</p>
  
  <p>But I also want that to be the case when I replace the object in the list that my variable points to. That is when I do</p>
  
  <pre><code>myList.set(theObjectPosition, newInstanceOfA);
  </code></pre>
  
  <p>...in this case I also want my variable to automagically have the reference to this new instance. What would be the easiest way to implement such behavior?</p>
  
  <p>This is why I need this:</p>
  
  <p>In short, I have a set of classes that handles undos/redos of changes made to a ArrayList (which I have not extended, that would not solve the problem anyway?). The classes are called</p>
  
  <pre><code>UndoAction, Add, Remove, Modify (the latter 3 inherit from UndoAction)
  </code></pre>
  
  <p>All classes have methods undo() and redo().</p>
  
  <p>Example by describing (code is at the end if needed):</p>
  
  <p>Say I want to add an item to a List: I do new Add(myObject), then I want to modify the item in the list so I do new Modify(myObject), after which it creates a DeepCopy of the object state as backup, after which I THEN change the object itsself (after having done the call to new Modify(..)). Then I do undo() on the Modify object so it does list.set(somePos, previousDeepCopy) (hence this post, the deepcopy made, by nature, is a new instance which messes things up!!!)....</p>
  
  <p>So you can imagine that this list.set poses some problems. Any reference to the replaced object will be gone. So I can't work effectively with the list like that if it always replaces references to objects like that, hence my undo manager is doomed to failure this way.</p>
  
  <p>So how do I combat these design flaws? Any suggestions are welcome.</p>
  
  <hr>
  
  <p>Some code:</p>
  
  <pre><code> protected abstract class UndoAction {
      protected HashSet&lt;Integer&gt; ids = new HashSet&lt;Integer&gt;();
      protected Marker marker;
  
      public UndoAction(List&lt;E&gt; l) {
        for (E e : l) {
          if (!entities.contains(e)) {
            entities.add(e);
            trace.append(entities.indexOf(e), new Stack&lt;UndoAction&gt;());
          }
          ids.add(entities.indexOf(e));
        }
      }
  
      public boolean sameAffectedTargets(UndoAction undoAction) {
        if (this.ids.containsAll(undoAction.ids) &amp;&amp; undoAction.ids.containsAll(this.ids))
          return true;
        return false;
      }
  
      public Marker getMarker() {
        return new Marker(this);
      }
  
      public void firstRun() {
      }
  
      public void undo() {
      }
  
      public void redo() {
      }
  
      protected List&lt;Data&gt; getCopyEntities() {
        List&lt;Data&gt; l = new ArrayList&lt;Data&gt;();
        for (Integer id : ids) {
          E e = entities.get(id);
          int pos = adapterList.indexOf(e);
          l.add(new Data(id, pos, e));
        }
        return l;
      }
  
      protected List&lt;Data&gt; getDeepCopyEntities() {
        List&lt;Data&gt; l = new ArrayList&lt;Data&gt;();
        for (Integer id : ids) {
          E e = DeepCopy.copy(entities.get(id));
          int pos = adapterList.indexOf(entities.get(id));
          l.add(new Data(id, pos, e));
        }
        return l;
      }
  
      public void addEntities(List&lt;Data&gt; datas) {
        for (Data d : datas)
          d.addEntity();
      }
  
      public void setEntities(List&lt;Data&gt; datas) {
        for (Data d : datas)
          d.setEntity();
      }
  
      public void removeEntities(List&lt;Data&gt; datas) {
        for (Data d : datas)
          d.removeEntity();
      }
  
      protected class Data {
        public int id;
        public int pos;
        public E entity;
  
        public void addEntity() {
          entities.set(this.id, this.entity);
          adapterList.add(this.pos, this.entity);
        }
  
        public void setEntity() {
          entities.set(this.id, this.entity);
          E oldEntity = adapterList.get(this.pos);
          adapterList.set(this.pos, this.entity);
          notifyEntityReplaced(oldEntity, adapterList.get(this.pos), this.pos);
        }
  
        public void removeEntity() {
          entities.set(this.id, null);
          adapterList.remove(this.entity);
        }
  
        public Data(int id, int pos, E entity) {
          this.id = id;
          this.pos = pos;
          this.entity = entity;
        }
      }
    }
  
    protected class Add extends UndoAction {
      protected List&lt;Data&gt; addBackup;
  
      public Add(List&lt;E&gt; l) {
        super(l);
      }
  
      @Override
      public void undo() {
        super.undo();
        addBackup = getCopyEntities();
        removeEntities(addBackup);
      }
  
      @Override
      public void firstRun() {
        super.firstRun();
        adapterList.addAll(entities);
      }
  
      @Override
      public void redo() {
        super.redo();
        addEntities(addBackup);
      }
    }
  
    // call before modifying
    protected class Modify extends UndoAction {
      protected List&lt;Data&gt; beforeDeepCopies;
      protected List&lt;Data&gt; afterDeepCopies;
  
      public Modify(List&lt;E&gt; l) {
        super(l);
      }
  
      @Override
      public void undo() {
        super.undo();
        if (!skipModifying) {
          if (afterDeepCopies == null)
            afterDeepCopies = getDeepCopyEntities();
          setEntities(beforeDeepCopies);
        }
      }
  
      @Override
      public void firstRun() {
        super.firstRun();
        if (!skipModifying) // TODO
          beforeDeepCopies = getDeepCopyEntities();
      }
  
      @Override
      public void redo() {
        super.redo();
        if (!skipModifying)
          setEntities(afterDeepCopies);
      }
    }
  
    protected class Remove extends UndoAction {
      protected List&lt;Data&gt; removeBackup;
  
      public Remove(List&lt;E&gt; l) {
        super(l);
      }
  
      public List&lt;E&gt; getRemoved() {
        List&lt;E&gt; l = new ArrayList&lt;E&gt;();
        for (Data data : removeBackup)
          l.add(data.entity);
        return l;
      }
  
      @Override
      public void undo() {
        super.undo();
        addEntities(removeBackup);
      }
  
      @Override
      public void firstRun() {
        super.firstRun();
        removeBackup = getCopyEntities();
      }
  
      @Override
      public void redo() {
        super.redo();
        removeEntities(removeBackup);
      }
    }
  </code></pre>