Class Equivalence<T>


  • @GwtCompatible
    public abstract class Equivalence<T>
    extends Object
    A strategy for determining whether two instances are considered equivalent, and for computing hash codes in a manner consistent with that equivalence. Two examples of equivalences are the identity equivalence and the "equals" equivalence.

    For users targeting Android API level 24 or higher: This class will eventually implement BiPredicate<T, T> (as it does in the main Guava artifact), but we currently target a lower API level. In the meantime, if you have support for method references you can use an equivalence as a bi-predicate like this: myEquivalence::equivalent.

    Since:
    10.0 (mostly source-compatible since 4.0)
    Author:
    Bob Lee, Ben Yu, Gregory Kick
    • Constructor Detail

      • Equivalence

        protected Equivalence()
        Constructor for use by subclasses.
    • Method Detail

      • equivalent

        public final boolean equivalent​(@CheckForNull
                                        T a,
                                        @CheckForNull
                                        T b)
        Returns true if the given objects are considered equivalent.

        This method describes an equivalence relation on object references, meaning that for all references x, y, and z (any of which may be null):

        • equivalent(x, x) is true (reflexive property)
        • equivalent(x, y) and equivalent(y, x) each return the same result (symmetric property)
        • If equivalent(x, y) and equivalent(y, z) are both true, then equivalent(x, z) is also true (transitive property)

        Note that all calls to equivalent(x, y) are expected to return the same result as long as neither x nor y is modified.

      • doEquivalent

        @ForOverride
        protected abstract boolean doEquivalent​(T a,
                                                T b)
        Since:
        10.0 (previously, subclasses would override equivalent())
      • hash

        public final int hash​(@CheckForNull
                              T t)
        Returns a hash code for t.

        The hash has the following properties:

        • It is consistent: for any reference 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.
        • It is distributable across equivalence: for any references 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.
      • doHash

        @ForOverride
        protected abstract int doHash​(T t)
        Implemented by the user to return a hash code for t, subject to the requirements specified in hash(T).

        This method should not be called except by hash(T). When hash(T) calls this method, t is guaranteed to be non-null.

        Since:
        10.0 (previously, subclasses would override hash())
      • onResultOf

        public final <F> Equivalence<F> onResultOf​(Function<? super F,​? extends @Nullable T> function)
        Returns a new equivalence relation for F which evaluates equivalence by first applying function to the argument, then evaluating using this. That is, for any pair of non-null objects x and y, equivalence.onResultOf(function).equivalent(a, b) is true if and only if equivalence.equivalent(function.apply(a), function.apply(b)) is true.

        For example:

        
         Equivalence<Person> SAME_AGE = Equivalence.equals().onResultOf(GET_PERSON_AGE);
         

        function will never be invoked with a null value.

        Note that function must be consistent according to this equivalence relation. That is, invoking Function.apply(F) multiple times for a given value must return equivalent results. For example, Equivalence.identity().onResultOf(Functions.toStringFunction()) is broken because it's not guaranteed that Object.toString()) always returns the same string instance.

        Since:
        10.0
      • wrap

        public final <S extends @Nullable TEquivalence.Wrapper<S> wrap​(S reference)
        Returns a wrapper of reference that implements Object.equals() such that wrap(a).equals(wrap(b)) if and only if equivalent(a, b).

        The returned object is serializable if both this Equivalence and reference are serializable (including when reference is null).

        Since:
        10.0
      • pairwise

        @GwtCompatible(serializable=true)
        public final <S extends @Nullable TEquivalence<Iterable<S>> pairwise()
        Returns an equivalence over iterables based on the equivalence of their elements. More specifically, two iterables are considered equivalent if they both contain the same number of elements, and each pair of corresponding elements is equivalent according to this. Null iterables are equivalent to one another.

        Note that this method performs a similar function for equivalences as Ordering.lexicographical() does for orderings.

        The returned object is serializable if this object is serializable.

        Since:
        10.0
      • equivalentTo

        public final Predicate<@Nullable TequivalentTo​(@CheckForNull
                                                         T target)
        Returns a predicate that evaluates to true if and only if the input is equivalent to target according to this equivalence relation.
        Since:
        10.0