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 java.io.Serializable;
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.0
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.0 (present null-friendly behavior)
046       * @since 4.0 (otherwise)
047       */
048      public static Equivalence<Object> equals() {
049        return Equals.INSTANCE;
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 Identity.INSTANCE;
059      }
060    
061      /**
062       * Returns an equivalence over iterables based on the equivalence of their elements.  More
063       * specifically, two iterables are considered equivalent if they both contain the same number of
064       * elements, and each pair of corresponding elements is equivalent according to
065       * {@code elementEquivalence}.  Null iterables are equivalent to one another.
066       *
067       * @since 9.0
068       * @deprecated use {@link Equivalence#pairwise}, which behaves exactly the same. <b>This method is
069       *     scheduled for deletion from Guava in Guava release 11.0.</b>
070       */
071      @Deprecated
072      @GwtCompatible(serializable = true)
073      public
074      static <T> Equivalence<Iterable<T>> pairwise(Equivalence<? super T> elementEquivalence) {
075        /*
076         * Ideally, the returned equivalence would support {@code Iterable<? extends T>}.  However, the
077         * need for this is so rare that it's not worth making callers deal with the ugly wildcard.
078         */
079        return new PairwiseEquivalence<T>(elementEquivalence);
080      }
081    
082      private static final class Equals extends Equivalence<Object>
083          implements Serializable {
084    
085        static final Equals INSTANCE = new Equals();
086    
087        @Override protected boolean doEquivalent(Object a, Object b) {
088          return a.equals(b);
089        }
090        @Override public int doHash(Object o) {
091          return o.hashCode();
092        }
093    
094        private Object readResolve() {
095          return INSTANCE;
096        }
097        private static final long serialVersionUID = 1;
098      }
099    
100      private static final class Identity extends Equivalence<Object>
101          implements Serializable {
102    
103        static final Identity INSTANCE = new Identity();
104    
105        @Override protected boolean doEquivalent(Object a, Object b) {
106          return false;
107        }
108    
109        @Override protected int doHash(Object o) {
110          return System.identityHashCode(o);
111        }
112    
113        private Object readResolve() {
114          return INSTANCE;
115        }
116        private static final long serialVersionUID = 1;
117      }
118    }