* The {@code equivalent} method implements an equivalence relation on object * references: * *
* Called by {@link #equivalent}. {@code a} and {@code b} are not the same * object and are not nulls. * * @since 10.0 (previously, subclasses would override equivalent()) */ protected abstract boolean doEquivalent(T a, T b); /** * Returns a hash code for {@code t}. * *
* The {@code hash} has the following properties: *
* Called by {@link #hash}. * * @since 10.0 (previously, subclasses would override hash()) */ protected abstract int doHash(T t); /** * Returns a new equivalence relation for {@code F} which evaluates equivalence * by first applying {@code function} to the argument, then evaluating using * {@code this}. That is, for any pair of non-null objects {@code x} and * {@code y}, {@code * equivalence.onResultOf(function).equivalent(a, b)} is true if and only if * {@code * equivalence.equivalent(function.apply(a), function.apply(b))} is true. * *
* For example: * *
* { * @code * Equivalence* *SAME_AGE = Equivalence.equals().onResultOf(GET_PERSON_AGE); * } *
* {@code function} will never be invoked with a null value. * *
* Note that {@code function} must be consistent according to {@code this}
* equivalence relation. That is, invoking {@link Function#apply} multiple times
* for a given value must return equivalent results. For example,
* {@code Equivalence.identity().onResultOf(Functions.toStringFunction())} is
* broken because it's not guaranteed that {@link Object#toString}) always
* returns the same string instance.
*
* @since 10.0
*/
public final
* For example, given an {@link Equivalence} for {@link String strings} named
* {@code equiv} that tests equivalence using their lengths:
*
*
* Note in particular that an equivalence wrapper is never equal to the object
* it wraps.
*
* Wrapper wrap(@Nullable S reference) {
return new Wrapper(this, reference);
}
/**
* Wraps an object so that {@link #equals(Object)} and {@link #hashCode()}
* delegate to an {@link Equivalence}.
*
*
* {@code
* equiv.wrap("a").equals(equiv.wrap("b")) // true
* equiv.wrap("a").equals(equiv.wrap("hello")) // false}
*
*
*
* {@code
* equiv.wrap(obj).equals(obj) // always false}
*
*
* @since 10.0
*/
public static final class Wrapper