Class ReferenceTracker
- java.lang.Object
-
- org.apache.commons.configuration2.tree.ReferenceTracker
-
final class ReferenceTracker extends java.lang.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 Summary
Fields Modifier and Type Field Description private java.util.Map<ImmutableNode,java.lang.Object>
references
A map with reference data.private java.util.List<java.lang.Object>
removedReferences
A list with the removed references.
-
Constructor Summary
Constructors Modifier Constructor Description ReferenceTracker()
Creates a new instance ofReferenceTracker
.private
ReferenceTracker(java.util.Map<ImmutableNode,java.lang.Object> refs, java.util.List<java.lang.Object> removedRefs)
Creates a new instance ofReferenceTracker
and sets the data to be managed.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description ReferenceTracker
addReferences(java.util.Map<ImmutableNode,?> refs)
Adds all references stored in the passed in map to the managed references.java.lang.Object
getReference(ImmutableNode node)
Gets the reference object associated with the given node.java.util.List<java.lang.Object>
getRemovedReferences()
Gets the list with removed references.ReferenceTracker
updateReferences(java.util.Map<ImmutableNode,ImmutableNode> replacedNodes, java.util.Collection<ImmutableNode> removedNodes)
Updates the references managed by this object at the end of a model transaction.
-
-
-
Field Detail
-
references
private final java.util.Map<ImmutableNode,java.lang.Object> references
A map with reference data.
-
removedReferences
private final java.util.List<java.lang.Object> removedReferences
A list with the removed references.
-
-
Constructor Detail
-
ReferenceTracker
public ReferenceTracker()
Creates a new instance ofReferenceTracker
. This instance does not yet contain any data about references.
-
ReferenceTracker
private ReferenceTracker(java.util.Map<ImmutableNode,java.lang.Object> refs, java.util.List<java.lang.Object> removedRefs)
Creates a new instance ofReferenceTracker
and sets the data to be managed. This constructor is used internally when references are updated.- Parameters:
refs
- the referencesremovedRefs
- the removed references
-
-
Method Detail
-
addReferences
public ReferenceTracker addReferences(java.util.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
-
getReference
public java.lang.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 java.util.List<java.lang.Object> getRemovedReferences()
Gets the list with removed references. This list is immutable.- Returns:
- the list with removed references
-
updateReferences
public ReferenceTracker updateReferences(java.util.Map<ImmutableNode,ImmutableNode> replacedNodes, java.util.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 replacedremovedNodes
- the list with nodes that have been removed- Returns:
- the new instance
-
-