Package org.plumelib.util
Class DeterministicObject
java.lang.Object
org.plumelib.util.DeterministicObject
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 Summary
Constructors -
Method Summary
-
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.
-
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 distinctDeterministicObjectobjects are never equal.
-