Class MapNode.AutoIndexMap

java.lang.Object
uk.blankaspect.common.basictree.MapNode.AutoIndexMap
Enclosing class:
MapNode

public static class MapNode.AutoIndexMap extends Object
This class implements a map from map nodes to indices that are automatically incremented. The getIndex(MapNode) method returns the index for the specified map node and increments the index.

An instance of this class can be used by a key-generator function to provide separate auto-incrementing indices for multiple map nodes. For example:

 MapNode.AutoIndexMap indexMap = new MapNode.AutoIndexMap();
 MapNode.setKeyGenerator(mapNode -> "// " + indexMap.getIndex(mapNode));

To prevent a memory leak from an instance of AutoIndexMap that is reused with different sets of map nodes, all of its entries should be removed with the clear() method after each use.

  • Constructor Details

    • AutoIndexMap

      public AutoIndexMap()
      Creates a new instance of a map from map nodes to auto-incrementing indices.
  • Method Details

    • getIndex

      public int getIndex(MapNode mapNode)
      Returns the index for the specified map node. If the map does not contain an entry for the map node, one will be added. The initial index is 0, and the index is incremented each time this method is called.
      Parameters:
      mapNode - the map node whose index is required.
      Returns:
      the index for mapNode.
    • remove

      public void remove(MapNode mapNode)
      Removes the entry for the specified map node from this map.
      Parameters:
      mapNode - the map node whose entry will be removed from this map.
    • clear

      public void clear()
      Removes all the entries from this map.