Class Equivalence<T>
- java.lang.Object
-
- org.glassfish.jersey.internal.guava.Equivalence<T>
-
public abstract class Equivalence<T> extends Object
A strategy for determining whether two instances are considered equivalent. Examples of equivalences are the identity equivalence and equals equivalence.- Since:
- 10.0 (mostly source-compatible since 4.0)
- Author:
- Bob Lee, Ben Yu, Gregory Kick
-
-
Constructor Summary
Constructors Constructor Description Equivalence()
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected abstract booleandoEquivalent(T a, T b)Returnstrueifaandbare considered equivalent.protected abstract intdoHash(T t)Returns a hash code for non-null objectt.static Equivalence<Object>equals()Returns an equivalence that delegates toObject.equals(java.lang.Object)andObject.hashCode().booleanequivalent(T a, T b)Returnstrueif the given objects are considered equivalent.inthash(T t)Returns a hash code fort.static Equivalence<Object>identity()Returns an equivalence that uses==to compare values andSystem.identityHashCode(Object)to compute the hash code.
-
-
-
Method Detail
-
equals
public static Equivalence<Object> equals()
Returns an equivalence that delegates toObject.equals(java.lang.Object)andObject.hashCode().equivalent(T, T)returnstrueif both values are null, or if neither value is null andObject.equals(java.lang.Object)returnstrue.hash(T)returns0if passed a null value.- Since:
- 4.0 (in Equivalences)
-
identity
public static Equivalence<Object> identity()
Returns an equivalence that uses==to compare values andSystem.identityHashCode(Object)to compute the hash code.equivalent(T, T)returnstrueifa == b, including in the case that a and b are both null.- Since:
- 4.0 (in Equivalences)
-
equivalent
public final boolean equivalent(T a, T b)
Returnstrueif the given objects are considered equivalent.The
equivalentmethod implements an equivalence relation on object references:- It is reflexive: for any reference
x, including null,equivalent(x, x)returnstrue. - It is symmetric: for any references
xandy,equivalent(x, y) == equivalent(y, x). - It is transitive: for any references
x,y, andz, ifequivalent(x, y)returnstrueandequivalent(y, z)returnstrue, thenequivalent(x, z)returnstrue. - It is consistent: for any references
xandy, multiple invocations ofequivalent(x, y)consistently returntrueor consistently returnfalse(provided that neitherxnoryis modified).
- It is reflexive: for any reference
-
doEquivalent
protected abstract boolean doEquivalent(T a, T b)
Returnstrueifaandbare considered equivalent.Called by
equivalent(T, T).aandbare not the same object and are not nulls.- Since:
- 10.0 (previously, subclasses would override equivalent())
-
hash
public final int hash(T t)
Returns a hash code fort.The
hashhas the following properties:- It is consistent: for any reference
x, multiple invocations ofhash(x} consistently return the same value providedxremains unchanged according to the definition of the equivalence. The hash need not remain consistent from one execution of an application to another execution of the same application. - It is distributable across equivalence: for any references
xandy, ifequivalent(x, y), thenhash(x) == hash(y). It is not necessary that the hash be distributable across inequivalence. Ifequivalence(x, y)is false,hash(x) == hash(y)may still be true. hash(null)is0.
- It is consistent: for any reference
-
doHash
protected abstract int doHash(T t)
- Since:
- 10.0 (previously, subclasses would override hash())
-
-