001 /*
002 * Copyright (C) 2010 The Guava Authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017 package com.google.common.base;
018
019 import com.google.common.annotations.Beta;
020 import com.google.common.annotations.GwtCompatible;
021
022 import javax.annotation.Nullable;
023
024 /**
025 * Contains static factory methods for creating {@code Equivalence} instances.
026 *
027 * <p>All methods return serializable instances.
028 *
029 * @author Bob Lee
030 * @author Kurt Alfred Kluever
031 * @author Gregory Kick
032 * @since 4
033 */
034 @Beta
035 @GwtCompatible
036 public final class Equivalences {
037 private Equivalences() {}
038
039 /**
040 * Returns an equivalence that delegates to {@link Object#equals} and {@link Object#hashCode}.
041 * {@link Equivalence#equivalent} returns {@code true} if both values are null, or if neither
042 * value is null and {@link Object#equals} returns {@code true}. {@link Equivalence#hash} returns
043 * {@code 0} if passed a null value.
044 *
045 * @since 8 (present null-friendly behavior)
046 * @since 4 (otherwise)
047 */
048 public static Equivalence<Object> equals() {
049 return Impl.EQUALS;
050 }
051
052 /**
053 * Returns an equivalence that uses {@code ==} to compare values and {@link
054 * System#identityHashCode(Object)} to compute the hash code. {@link Equivalence#equivalent}
055 * returns {@code true} if {@code a == b}, including in the case that a and b are both null.
056 */
057 public static Equivalence<Object> identity() {
058 return Impl.IDENTITY;
059 }
060
061 private enum Impl implements Equivalence<Object> {
062 EQUALS {
063 @Override
064 public boolean equivalent(@Nullable Object a, @Nullable Object b) {
065 // TODO(kevinb): use Objects.equal() after testing issue is worked out.
066 return (a == null) ? (b == null) : a.equals(b);
067 }
068
069 @Override
070 public int hash(@Nullable Object o) {
071 return (o == null) ? 0 : o.hashCode();
072 }
073 },
074 IDENTITY {
075 @Override
076 public boolean equivalent(@Nullable Object a, @Nullable Object b) {
077 return a == b;
078 }
079
080 @Override
081 public int hash(@Nullable Object o) {
082 return System.identityHashCode(o);
083 }
084 },
085 }
086
087 /**
088 * Returns an equivalence over iterables based on the equivalence of their elements. More
089 * specifically, two iterables are considered equivalent if they both contain the same number of
090 * elements, and each pair of corresponding elements is equivalent according to
091 * {@code elementEquivalence}. Null iterables are equivalent to one another.
092 *
093 * @since 9
094 */
095 @GwtCompatible(serializable = true)
096 public static <T> Equivalence<Iterable<T>> pairwise(Equivalence<? super T> elementEquivalence) {
097 /*
098 * Ideally, the returned equivalence would support {@code Iterable<? extends T>}. However, the
099 * need for this is so rare that it's not worth making callers deal with the ugly wildcard.
100 */
101 return new PairwiseEquivalence<T>(elementEquivalence);
102 }
103 }