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      private static final class Equals extends Equivalence<Object>
062          implements Serializable {
063        
064        static final Equals INSTANCE = new Equals();
065    
066        @Override protected boolean doEquivalent(Object a, Object b) {
067          return a.equals(b);
068        }
069        @Override public int doHash(Object o) {
070          return o.hashCode();
071        }
072    
073        private Object readResolve() {
074          return INSTANCE;
075        } 
076        private static final long serialVersionUID = 1;
077      }
078      
079      private static final class Identity extends Equivalence<Object>
080          implements Serializable {
081        
082        static final Identity INSTANCE = new Identity();
083        
084        @Override protected boolean doEquivalent(Object a, Object b) {
085          return false;
086        }
087    
088        @Override protected int doHash(Object o) {
089          return System.identityHashCode(o);
090        }
091     
092        private Object readResolve() {
093          return INSTANCE;
094        }
095        private static final long serialVersionUID = 1;
096      }
097    }