public abstract class Equivalence<T> extends Object
Constructor and Description |
---|
Equivalence() |
Modifier and Type | Method and Description |
---|---|
protected abstract boolean |
doEquivalent(T a,
T b)
Returns
true if a and b are considered equivalent. |
protected abstract int |
doHash(T t)
Returns a hash code for non-null object
t . |
static Equivalence<Object> |
equals()
Returns an equivalence that delegates to
Object.equals(java.lang.Object) and Object.hashCode() . |
boolean |
equivalent(T a,
T b)
Returns
true if the given objects are considered equivalent. |
int |
hash(T t)
Returns a hash code for
t . |
static Equivalence<Object> |
identity()
Returns an equivalence that uses
== to compare values and System.identityHashCode(Object) to compute the hash code. |
public static Equivalence<Object> equals()
Object.equals(java.lang.Object)
and Object.hashCode()
.
equivalent(T, T)
returns true
if both values are null, or if neither
value is null and Object.equals(java.lang.Object)
returns true
. hash(T)
returns
0
if passed a null value.public static Equivalence<Object> identity()
==
to compare values and System.identityHashCode(Object)
to compute the hash code. equivalent(T, T)
returns true
if a == b
, including in the case that a and b are both null.public final boolean equivalent(T a, T b)
true
if the given objects are considered equivalent.
The equivalent
method implements an equivalence relation on object references:
x
, including null, equivalent(x, x)
returns true
.
x
and y
, equivalent(x, y) == equivalent(y, x)
.
x
, y
, and z
, if
equivalent(x, y)
returns true
and equivalent(y, z)
returns true
, then equivalent(x, z)
returns true
.
x
and y
, multiple invocations
of equivalent(x, y)
consistently return true
or consistently return false
(provided that neither x
nor y
is modified).
protected abstract boolean doEquivalent(T a, T b)
true
if a
and b
are considered equivalent.
Called by equivalent(T, T)
. a
and b
are not the same
object and are not nulls.
public final int hash(T t)
t
.
The hash
has the following properties:
x
, multiple invocations of
hash(x
} consistently return the same value provided x
remains 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.
x
and y
,
if equivalent(x, y)
, then hash(x) == hash(y)
. It is not necessary
that the hash be distributable across inequivalence. If equivalence(x, y)
is false, hash(x) == hash(y)
may still be true.
hash(null)
is 0
.
protected abstract int doHash(T t)
Copyright © 2007-2024, Oracle and/or its affiliates. All Rights Reserved. Use is subject to license terms.