Class DeterministicObject

java.lang.Object
org.plumelib.util.DeterministicObject

public class DeterministicObject extends Object
This is a deterministic version of the Object class. To remove one source of nondeterminism from your program, do not instantiate the Object class, as in new Object(); instead do new DeterministicObject().

This class differs from Object in that it overrides hashCode(). Any use of Object.hashCode() is nondeterministic because the return value of Object.hashCode() depends on when the garbage collector runs. That nondeterminism can affect the iteration order of HashMaps and HashSets, the output of toString(), and other behavior.

To implement similar functionality for other classes, see UniqueId.

See Also:
  • Constructor Details

    • DeterministicObject

      public DeterministicObject()
      Create a DeterministicObject.
  • Method Details

    • hashCode

      public int hashCode(@GuardSatisfied DeterministicObject this)

      Returns a unique ID for the object. The first object created has the id (and hash code) 0, the second one has 1, and so forth.

      Overrides:
      hashCode in class Object
    • equals

      public boolean equals(@GuardSatisfied DeterministicObject this, @Nullable @GuardSatisfied Object other)
      Returns true if this object is the same instance as the given object. Two distinct DeterministicObject objects are never equal.
      Overrides:
      equals in class Object
      Parameters:
      other - a value to compare to this
      Returns:
      true if this is the same as the given object