Package org.dellroad.stuff.java
Class ObjectComparator
java.lang.Object
org.dellroad.stuff.java.ObjectComparator
- All Implemented Interfaces:
Comparator<Object>
A
Comparator
that creates a stable, total ordering of all Java objects.
Instances are compared first using identity hash codes, and then using an internally generated unique identifier; identifiers are indexed using weak keys to avoid memory leaks. Any two distict Java objects will always compare as non-equal.
Note: this ordering is only consistent with equals (see Comparable
) for classes whose
equals()
method is implemented in terms of object equality (e.g., not overridden at all).
Note: while each instance creates a stable sort ordering, distinct instances of this class may sort objects differently.
Null values are supported and always sort last. Instances of this class are thread safe.
This class requires Google Guava.
-
Constructor Summary
ModifierConstructorDescriptionDefault constructor.ObjectComparator
(int concurrencyLevel) Constructor.protected
ObjectComparator
(CacheBuilder<Object, Object> cacheBuilder) Internal constructor. -
Method Summary
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface java.util.Comparator
equals, reversed, thenComparing, thenComparing, thenComparing, thenComparingDouble, thenComparingInt, thenComparingLong
-
Constructor Details
-
ObjectComparator
public ObjectComparator()Default constructor. -
ObjectComparator
public ObjectComparator(int concurrencyLevel) Constructor.- Parameters:
concurrencyLevel
- guides the allowed concurrency among update operations; used as a hint for internal sizing- Throws:
IllegalArgumentException
- ifconcurrencyLevel
is zero or less- See Also:
-
CacheBuilder.concurrencyLevel
-
ObjectComparator
Internal constructor.- Parameters:
cacheBuilder
- object unique ID cache builder- Throws:
NullPointerException
- ifcacheBuilder
is null
-
-
Method Details
-
compare
- Specified by:
compare
in interfaceComparator<Object>
-