Java Object Hash deepHashCode(Object o)

Here you can find the source of deepHashCode(Object o)

Description

Gets hash code of an object, optionally returns hash code based on the "deep contents" of array if the object is an array.

License

Apache License

Declaration

public static int deepHashCode(Object o) 

Method Source Code

//package com.java2s;
/*// w  w  w. ja  v a2s .  co m
 * Copyright 2008-2009 LinkedIn, Inc
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

import java.util.Arrays;

public class Main {
    /**
     * Gets hash code of an object, optionally returns hash code based on the
     * "deep contents" of array if the object is an array.
     * <p>
     * If {@code o} is null, 0 is returned; if {@code o} is an array, the
     * corresponding {@link Arrays#deepHashCode(Object[])}, or
     * {@link Arrays#hashCode(int[])} or the like is used to calculate the hash
     * code.
     */
    public static int deepHashCode(Object o) {
        if (o == null) {
            return 0;
        }
        if (!o.getClass().isArray()) {
            return o.hashCode();
        }
        if (o instanceof Object[]) {
            return Arrays.deepHashCode((Object[]) o);
        }
        if (o instanceof boolean[]) {
            return Arrays.hashCode((boolean[]) o);
        }
        if (o instanceof char[]) {
            return Arrays.hashCode((char[]) o);
        }
        if (o instanceof byte[]) {
            return Arrays.hashCode((byte[]) o);
        }
        if (o instanceof short[]) {
            return Arrays.hashCode((short[]) o);
        }
        if (o instanceof int[]) {
            return Arrays.hashCode((int[]) o);
        }
        if (o instanceof long[]) {
            return Arrays.hashCode((long[]) o);
        }
        if (o instanceof float[]) {
            return Arrays.hashCode((float[]) o);
        }
        if (o instanceof double[]) {
            return Arrays.hashCode((double[]) o);
        }
        throw new AssertionError();
    }
}

Related

  1. combineHashesOrdered(Object... objs)
  2. deepHashCode(final Object object)
  3. getIdentityAsHex(Object object)
  4. hash(Object o)
  5. hash(Object... objects)
  6. hash(Object... values)