Class TreeData

All Implemented Interfaces:
NodeHandler<ImmutableNode>, ReferenceNodeHandler

final class TreeData extends AbstractImmutableNodeHandler implements ReferenceNodeHandler
An internally used helper class for storing information about the managed node structure. An instance of this class represents the current tree. It stores the current root node and additional information which is not part of the ImmutableNode class.
Since:
2.0
  • Field Details

    • root

      private final ImmutableNode root
      The root node of the tree.
    • parentMapping

      private final Map<ImmutableNode,ImmutableNode> parentMapping
      A map that associates the parent node to each node.
    • replacementMapping

      private final Map<ImmutableNode,ImmutableNode> replacementMapping
      Stores information about nodes which have been replaced by manipulations of the structure. This map is used to avoid that the parent mapping has to be updated after each change.
    • inverseReplacementMapping

      private final Map<ImmutableNode,ImmutableNode> inverseReplacementMapping
      An inverse replacement mapping.
    • nodeTracker

      private final NodeTracker nodeTracker
      The node tracker.
    • referenceTracker

      private final ReferenceTracker referenceTracker
      The reference tracker.
  • Constructor Details

    • TreeData

      public TreeData(ImmutableNode root, Map<ImmutableNode,ImmutableNode> parentMapping, Map<ImmutableNode,ImmutableNode> replacements, NodeTracker tracker, ReferenceTracker refTracker)
      Creates a new instance of TreeData and initializes it with all data to be stored.
      Parameters:
      root - the root node of the current tree
      parentMapping - the mapping to parent nodes
      replacements - the map with the nodes that have been replaced
      tracker - the NodeTracker
      refTracker - the ReferenceTracker
  • Method Details

    • getRootNode

      public ImmutableNode getRootNode()
      Description copied from interface: NodeHandler
      Gets the root node of the underlying hierarchy.
      Specified by:
      getRootNode in interface NodeHandler<ImmutableNode>
      Returns:
      the current root node
    • getNodeTracker

      public NodeTracker getNodeTracker()
      Gets the NodeTracker
      Returns:
      the NodeTracker
    • getReferenceTracker

      public ReferenceTracker getReferenceTracker()
      Gets the ReferenceTracker.
      Returns:
      the ReferenceTracker
    • getParent

      public ImmutableNode getParent(ImmutableNode node)
      Gets the parent node of the specified node. Result is null for the root node. If the passed in node cannot be resolved, an exception is thrown.
      Specified by:
      getParent in interface NodeHandler<ImmutableNode>
      Parameters:
      node - the node in question
      Returns:
      the parent node for this node
      Throws:
      IllegalArgumentException - if the node cannot be resolved
    • copyParentMapping

      public Map<ImmutableNode,ImmutableNode> copyParentMapping()
      Returns a copy of the mapping from nodes to their parents.
      Returns:
      the copy of the parent mapping
    • copyReplacementMapping

      public Map<ImmutableNode,ImmutableNode> copyReplacementMapping()
      Returns a copy of the map storing the replaced nodes.
      Returns:
      the copy of the replacement mapping
    • updateNodeTracker

      public TreeData updateNodeTracker(NodeTracker newTracker)
      Creates a new instance which uses the specified NodeTracker. This method is called when there are updates of the state of tracked nodes.
      Parameters:
      newTracker - the new NodeTracker
      Returns:
      the updated instance
    • updateReferenceTracker

      public TreeData updateReferenceTracker(ReferenceTracker newTracker)
      Creates a new instance which uses the specified ReferenceTracker. All other information are unchanged. This method is called when there updates for references.
      Parameters:
      newTracker - the new ReferenceTracker
      Returns:
      the updated instance
    • getReference

      public Object getReference(ImmutableNode node)
      Gets the reference object associated with the specified node. If no reference data is associated with this node, result is null. This implementation delegates to the reference tracker.
      Specified by:
      getReference in interface ReferenceNodeHandler
      Parameters:
      node - the node in question
      Returns:
      the reference object for this node or null
    • removedReferences

      public List<Object> removedReferences()
      Returns a list with the reference objects for nodes which have been removed. Whenever a node associated with a reference object is removed from the nodes structure managed by the owning model, the reference object is recorded. This is necessary for instance to free some resources. With this method all recorded reference objects can be queried. They are typically returned in the order in which they have been removed. This implementation delegates to the reference tracker.
      Specified by:
      removedReferences in interface ReferenceNodeHandler
      Returns:
      a list with reference objects for nodes removed from the model
    • handleReplacements

      private static ImmutableNode handleReplacements(ImmutableNode replace, Map<ImmutableNode,ImmutableNode> mapping)
      Checks whether the passed in node is subject of a replacement by another one. If so, the other node is returned. This is done until a node is found which had not been replaced. Updating the parent mapping may be expensive for large node structures. Therefore, it initially remains constant, and a map with replacements is used. When querying a parent node, the replacement map has to be consulted whether the parent node is still valid.
      Parameters:
      replace - the replacement node
      mapping - the replacement mapping
      Returns:
      the corresponding node according to the mapping
    • createInverseMapping

      private Map<ImmutableNode,ImmutableNode> createInverseMapping(Map<ImmutableNode,ImmutableNode> replacements)
      Creates the inverse replacement mapping.
      Parameters:
      replacements - the original replacement mapping
      Returns:
      the inverse replacement mapping