/** * Returns {@code true} if the given objects are considered equivalent. * * <p>The {@code equivalent} method implements an equivalence relation on object references: * * <ul> * <li>It is <i>reflexive</i>: for any reference {@code x}, including null, {@code * equivalent(x, x)} returns {@code true}. * <li>It is <i>symmetric</i>: for any references {@code x} and {@code y}, {@code * equivalent(x, y) == equivalent(y, x)}. * <li>It is <i>transitive</i>: for any references {@code x}, {@code y}, and {@code z}, if * {@code equivalent(x, y)} returns {@code true} and {@code equivalent(y, z)} returns {@code * true}, then {@code equivalent(x, z)} returns {@code true}. * <li>It is <i>consistent</i>: for any references {@code x} and {@code y}, multiple invocations * of {@code equivalent(x, y)} consistently return {@code true} or consistently return {@code * false} (provided that neither {@code x} nor {@code y} is modified). * </ul> */ public final boolean equivalent(@Nullable T a, @Nullable T b) { if (a == b) { return true; } if (a == null || b == null) { return false; } return doEquivalent(a, b); }
/** * Returns a hash code for {@code t}. * * <p>The {@code hash} has the following properties: * <ul> * <li>It is <i>consistent</i>: for any reference {@code x}, multiple invocations of * {@code hash(x}} consistently return the same value provided {@code 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. * <li>It is <i>distributable across equivalence</i>: for any references {@code x} and {@code y}, * if {@code equivalent(x, y)}, then {@code hash(x) == hash(y)}. It is <i>not</i> necessary * that the hash be distributable across <i>inequivalence</i>. If {@code equivalence(x, y)} * is false, {@code hash(x) == hash(y)} may still be true. * <li>{@code hash(null)} is {@code 0}. * </ul> */ public final int hash(@Nullable T t) { if (t == null) { return 0; } return doHash(t); }
@Override Equivalence<Object> defaultEquivalence() { return Equivalence.equals(); } },
private static <K, V> void doDifference( Map<? extends K, ? extends V> left, Map<? extends K, ? extends V> right, Equivalence<? super V> valueEquivalence, Map<K, V> onlyOnLeft, Map<K, V> onlyOnRight, Map<K, V> onBoth, Map<K, MapDifference.ValueDifference<V>> differences) { for (Entry<? extends K, ? extends V> entry : left.entrySet()) { K leftKey = entry.getKey(); V leftValue = entry.getValue(); if (right.containsKey(leftKey)) { V rightValue = onlyOnRight.remove(leftKey); if (valueEquivalence.equivalent(leftValue, rightValue)) { onBoth.put(leftKey, leftValue); } else { differences.put( leftKey, ValueDifferenceImpl.create(leftValue, rightValue)); } } else { onlyOnLeft.put(leftKey, leftValue); } } }
@Override Equivalence<Object> defaultEquivalence() { return Equivalence.identity(); } };
int hash(@Nullable Object key) { int h = keyEquivalence.hash(key); return rehash(h); }
private static <K, V> void doDifference( Map<? extends K, ? extends V> left, Map<? extends K, ? extends V> right, Equivalence<? super V> valueEquivalence, Map<K, V> onlyOnLeft, Map<K, V> onlyOnRight, Map<K, V> onBoth, Map<K, MapDifference.ValueDifference<V>> differences) { for (Entry<? extends K, ? extends V> entry : left.entrySet()) { K leftKey = entry.getKey(); V leftValue = entry.getValue(); if (right.containsKey(leftKey)) { V rightValue = onlyOnRight.remove(leftKey); if (valueEquivalence.equivalent(leftValue, rightValue)) { onBoth.put(leftKey, leftValue); } else { differences.put( leftKey, ValueDifferenceImpl.create(leftValue, rightValue)); } } else { onlyOnLeft.put(leftKey, leftValue); } } }
@Override Equivalence<Object> defaultEquivalence() { return Equivalence.identity(); } },
int hash(Object key) { int h = keyEquivalence.hash(key); return rehash(h); }
private static <K, V> void doDifference( Map<? extends K, ? extends V> left, Map<? extends K, ? extends V> right, Equivalence<? super V> valueEquivalence, Map<K, V> onlyOnLeft, Map<K, V> onlyOnRight, Map<K, V> onBoth, Map<K, MapDifference.ValueDifference<V>> differences) { for (Entry<? extends K, ? extends V> entry : left.entrySet()) { K leftKey = entry.getKey(); V leftValue = entry.getValue(); if (right.containsKey(leftKey)) { V rightValue = onlyOnRight.remove(leftKey); if (valueEquivalence.equivalent(leftValue, rightValue)) { onBoth.put(leftKey, leftValue); } else { differences.put( leftKey, ValueDifferenceImpl.create(leftValue, rightValue)); } } else { onlyOnLeft.put(leftKey, leftValue); } } }
@Override Equivalence<Object> defaultEquivalence() { return Equivalence.identity(); } },
@Override Equivalence<Object> defaultEquivalence() { return Equivalence.equals(); } },
int hash(Object key) { int h = keyEquivalence.hash(key); return rehash(h); }
/** * Returns a hash code for {@code t}. * * <p>The {@code hash} has the following properties: * <ul> * <li>It is <i>consistent</i>: for any reference {@code x}, multiple invocations of * {@code hash(x}} consistently return the same value provided {@code 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. * <li>It is <i>distributable across equivalence</i>: for any references {@code x} and {@code y}, * if {@code equivalent(x, y)}, then {@code hash(x) == hash(y)}. It is <i>not</i> necessary * that the hash be distributable across <i>inequivalence</i>. If {@code equivalence(x, y)} * is false, {@code hash(x) == hash(y)} may still be true. * <li>{@code hash(null)} is {@code 0}. * </ul> */ public final int hash(@Nullable T t) { if (t == null) { return 0; } return doHash(t); }
/** * Returns {@code true} if the given objects are considered equivalent. * * <p>The {@code equivalent} method implements an equivalence relation on object references: * * <ul> * <li>It is <i>reflexive</i>: for any reference {@code x}, including null, {@code * equivalent(x, x)} returns {@code true}. * <li>It is <i>symmetric</i>: for any references {@code x} and {@code y}, {@code * equivalent(x, y) == equivalent(y, x)}. * <li>It is <i>transitive</i>: for any references {@code x}, {@code y}, and {@code z}, if * {@code equivalent(x, y)} returns {@code true} and {@code equivalent(y, z)} returns {@code * true}, then {@code equivalent(x, z)} returns {@code true}. * <li>It is <i>consistent</i>: for any references {@code x} and {@code y}, multiple invocations * of {@code equivalent(x, y)} consistently return {@code true} or consistently return {@code * false} (provided that neither {@code x} nor {@code y} is modified). * </ul> */ public final boolean equivalent(@Nullable T a, @Nullable T b) { if (a == b) { return true; } if (a == null || b == null) { return false; } return doEquivalent(a, b); }
private static <K, V> void doDifference( Map<? extends K, ? extends V> left, Map<? extends K, ? extends V> right, Equivalence<? super V> valueEquivalence, Map<K, V> onlyOnLeft, Map<K, V> onlyOnRight, Map<K, V> onBoth, Map<K, MapDifference.ValueDifference<V>> differences) { for (Entry<? extends K, ? extends V> entry : left.entrySet()) { K leftKey = entry.getKey(); V leftValue = entry.getValue(); if (right.containsKey(leftKey)) { V rightValue = onlyOnRight.remove(leftKey); if (valueEquivalence.equivalent(leftValue, rightValue)) { onBoth.put(leftKey, leftValue); } else { differences.put( leftKey, ValueDifferenceImpl.create(leftValue, rightValue)); } } else { onlyOnLeft.put(leftKey, leftValue); } } }
@Override Equivalence<Object> defaultEquivalence() { return Equivalence.identity(); } };
@Override Equivalence<Object> defaultEquivalence() { return Equivalence.equals(); } },
int hash(@Nullable Object key) { int h = keyEquivalence.hash(key); return rehash(h); }
/** * Returns a hash code for {@code t}. * * <p>The {@code hash} has the following properties: * <ul> * <li>It is <i>consistent</i>: for any reference {@code x}, multiple invocations of * {@code hash(x}} consistently return the same value provided {@code 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. * <li>It is <i>distributable across equivalence</i>: for any references {@code x} and {@code y}, * if {@code equivalent(x, y)}, then {@code hash(x) == hash(y)}. It is <i>not</i> necessary * that the hash be distributable across <i>inequivalence</i>. If {@code equivalence(x, y)} * is false, {@code hash(x) == hash(y)} may still be true. * <li>{@code hash(null)} is {@code 0}. * </ul> */ public final int hash(@Nullable T t) { if (t == null) { return 0; } return doHash(t); }