Class ReferenceTracker

java.lang.Object
org.apache.commons.configuration2.tree.ReferenceTracker

class ReferenceTracker extends Object

A class which allows an InMemoryNodeModel to associate arbitrary objects with nodes.

Some special configuration implementations need additional data to be stored with their nodes structure. We call such data "references" because objects required by a configuration are referenced. In such constellations, it is necessary to keep track about the nodes associated with references even if they are replaced by others during an update operation of the model. This is the task of this class.

Basically, an instance manages a map associating nodes with reference objects. When a node is replaced the map gets updated. References becoming orphans because the nodes pointing to them were removed are tracked, too. They may be of importance for special configuration implementations as they might require additional updates. A concrete use case for this class is XMLConfiguration which stores the DOM nodes represented by configuration nodes as references.

Implementation note: This class is intended to work in a concurrent environment. Instances are immutable. The represented state can be updated by creating new instances which are then stored by the owning node model.

  • Field Details

    • references

      private final Map<ImmutableNode,Object> references
      A map with reference data.
    • removedReferences

      private final List<Object> removedReferences
      A list with the removed references.
  • Constructor Details

    • ReferenceTracker

      private ReferenceTracker(Map<ImmutableNode,Object> refs, List<Object> removedRefs)
      Creates a new instance of ReferenceTracker and sets the data to be managed. This constructor is used internally when references are updated.
      Parameters:
      refs - the references
      removedRefs - the removed references
    • ReferenceTracker

      public ReferenceTracker()
      Creates a new instance of ReferenceTracker. This instance does not yet contain any data about references.
  • Method Details

    • addReferences

      public ReferenceTracker addReferences(Map<ImmutableNode,?> refs)
      Adds all references stored in the passed in map to the managed references. A new instance is created managing this new set of references.
      Parameters:
      refs - the references to be added
      Returns:
      the new instance
    • updateReferences

      public ReferenceTracker updateReferences(Map<ImmutableNode,ImmutableNode> replacedNodes, Collection<ImmutableNode> removedNodes)
      Updates the references managed by this object at the end of a model transaction. This method is called by the transaction with the nodes that have been replaced by others and the nodes that have been removed. The internal data structures are updated correspondingly.
      Parameters:
      replacedNodes - the map with nodes that have been replaced
      removedNodes - the list with nodes that have been removed
      Returns:
      the new instance
    • getReference

      public Object getReference(ImmutableNode node)
      Gets the reference object associated with the given node.
      Parameters:
      node - the node
      Returns:
      the reference object for this node or null
    • getRemovedReferences

      public List<Object> getRemovedReferences()
      Gets the list with removed references. This list is immutable.
      Returns:
      the list with removed references