Class UniqueIdMap<E>

java.lang.Object
org.plumelib.util.UniqueIdMap<E>
Type Parameters:
E - the type of elements that get a unique ID

public class UniqueIdMap<E> extends Object
Provides a unique ID for classes that you cannot modify. The unique ID is useful because it makes output deterministic. For classes that you can edit, implement the UniqueId interface.

The IDs count up from 0, per instance of UniqueIdMap. When you look up an object in this map, it is given a unique ID if it didn't already have one.

Typical use:

  1. Define a field:
    
     /** Unique ids for trees. */
     static UniqueIdMap<Tree> treeUids = new UniqueIdMap<>();
     
  2. Wherever you would call x.hashCode(), instead call treeUids.get(x).
  • Constructor Details

    • UniqueIdMap

      public UniqueIdMap()
      Create a new UniqueIdMap.
  • Method Details

    • get

      public long get(E object)
      Get the unique ID for the given object. If the object's ID has not been previously requested, a new one is generated for it.
      Parameters:
      object - the object to get a unique ID for
      Returns:
      the unique ID for the given object