Java Object Hash hashCode(final Object o)

Here you can find the source of hashCode(final Object o)

Description

hash Code

License

Apache License

Declaration

public static int hashCode(final Object o) 

Method Source Code

//package com.java2s;
/*//from   www.j ava 2s .  com
 * HashUtilities.java
 *
 * Copyright (c) 2012 Mike Strobel
 *
 * This source code is subject to terms and conditions of the Apache License, Version 2.0.
 * A copy of the license can be found in the License.html file at the root of this distribution.
 * By using this source code in any fashion, you are agreeing to be bound by the terms of the
 * Apache License, Version 2.0.
 *
 * You must not remove this notice, or any other, from this software.
 */

import java.util.Arrays;

public class Main {
    public static final int NullHashCode = 0x61E04917;
    private static final int CombinedHashOffset = 5;

    public static int hashCode(final Object o) {
        if (o == null) {
            return NullHashCode;
        }

        if (o.getClass().isArray()) {
            if (o instanceof Object[]) {
                return combineHashCodes((Object[]) 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 char[]) {
                return Arrays.hashCode((char[]) o);
            }

            if (o instanceof float[]) {
                return Arrays.hashCode((float[]) o);
            }

            if (o instanceof double[]) {
                return Arrays.hashCode((double[]) o);
            }

            if (o instanceof boolean[]) {
                return Arrays.hashCode((boolean[]) o);
            }
        }

        return o.hashCode();
    }

    public static int combineHashCodes(final int... hashes) {
        int hash = 0;

        for (final int h : hashes) {
            hash <<= CombinedHashOffset;
            hash ^= h;
        }

        return hash;
    }

    public static int combineHashCodes(final Object... objects) {
        int hash = 0;

        for (final Object o : objects) {
            int entryHash = NullHashCode;

            if (o != null) {
                if (o instanceof Object[]) {
                    entryHash = combineHashCodes((Object[]) o);
                } else {
                    entryHash = hashCode(o);
                }
            }

            hash <<= CombinedHashOffset;
            hash ^= entryHash;
        }

        return hash;
    }

    public static int combineHashCodes(final int hash1, final int hash2) {
        return (hash1 << CombinedHashOffset) ^ hash2;
    }

    public static int combineHashCodes(final int hash1, final int hash2,
            final int hash3) {
        return (((hash1 << CombinedHashOffset) ^ hash2) << CombinedHashOffset)
                ^ hash3;
    }

    public static int combineHashCodes(final int hash1, final int hash2,
            final int hash3, final int hash4) {
        return (((((hash1 << CombinedHashOffset) ^ hash2) << CombinedHashOffset) ^ hash3) << CombinedHashOffset)
                ^ hash4;
    }

    public static int combineHashCodes(final int hash1, final int hash2,
            final int hash3, final int hash4, final int hash5) {

        return (((((((hash1 << CombinedHashOffset) ^ hash2) << CombinedHashOffset) ^ hash3) << CombinedHashOffset) ^ hash4) << CombinedHashOffset)
                ^ hash5;
    }

    public static int combineHashCodes(final int hash1, final int hash2,
            final int hash3, final int hash4, final int hash5,
            final int hash6) {

        return (((((((((hash1 << CombinedHashOffset) ^ hash2) << CombinedHashOffset) ^ hash3) << CombinedHashOffset) ^ hash4) << CombinedHashOffset) ^ hash5) << CombinedHashOffset)
                ^ hash6;
    }

    public static int combineHashCodes(final int hash1, final int hash2,
            final int hash3, final int hash4, final int hash5,
            final int hash6, final int hash7) {

        return (((((((((((hash1 << CombinedHashOffset) ^ hash2) << CombinedHashOffset) ^ hash3) << CombinedHashOffset) ^ hash4) << CombinedHashOffset) ^ hash5) << CombinedHashOffset) ^ hash6) << CombinedHashOffset)
                ^ hash7;
    }

    public static int combineHashCodes(final int hash1, final int hash2,
            final int hash3, final int hash4, final int hash5,
            final int hash6, final int hash7, final int hash8) {

        return (((((((((((((hash1 << CombinedHashOffset) ^ hash2) << CombinedHashOffset) ^ hash3) << CombinedHashOffset) ^ hash4) << CombinedHashOffset) ^ hash5) << CombinedHashOffset) ^ hash6) << CombinedHashOffset) ^ hash7) << CombinedHashOffset)
                ^ hash8;
    }

    public static int combineHashCodes(final Object o1, final Object o2) {
        return combineHashCodes(o1 == null ? NullHashCode : hashCode(o1),
                o2 == null ? NullHashCode : hashCode(o2));
    }

    public static int combineHashCodes(final Object o1, final Object o2,
            final Object o3) {
        return combineHashCodes(o1 == null ? NullHashCode : hashCode(o1),
                o2 == null ? NullHashCode : hashCode(o2),
                o3 == null ? NullHashCode : hashCode(o3));
    }

    public static int combineHashCodes(final Object o1, final Object o2,
            final Object o3, final Object o4) {
        return combineHashCodes(o1 == null ? NullHashCode : hashCode(o1),
                o2 == null ? NullHashCode : hashCode(o2),
                o3 == null ? NullHashCode : hashCode(o3),
                o4 == null ? NullHashCode : hashCode(o4));
    }

    public static int combineHashCodes(final Object o1, final Object o2,
            final Object o3, final Object o4, final Object o5) {

        return combineHashCodes(o1 == null ? NullHashCode : hashCode(o1),
                o2 == null ? NullHashCode : hashCode(o2),
                o3 == null ? NullHashCode : hashCode(o3),
                o4 == null ? NullHashCode : hashCode(o4),
                o5 == null ? NullHashCode : hashCode(o5));
    }

    public static int combineHashCodes(final Object o1, final Object o2,
            final Object o3, final Object o4, final Object o5,
            final Object o6) {

        return combineHashCodes(o1 == null ? NullHashCode : hashCode(o1),
                o2 == null ? NullHashCode : hashCode(o2),
                o3 == null ? NullHashCode : hashCode(o3),
                o4 == null ? NullHashCode : hashCode(o4),
                o5 == null ? NullHashCode : hashCode(o5),
                o6 == null ? NullHashCode : hashCode(o6));
    }

    public static int combineHashCodes(final Object o1, final Object o2,
            final Object o3, final Object o4, final Object o5,
            final Object o6, final Object o7) {

        return combineHashCodes(o1 == null ? NullHashCode : hashCode(o1),
                o2 == null ? NullHashCode : hashCode(o2),
                o3 == null ? NullHashCode : hashCode(o3),
                o4 == null ? NullHashCode : hashCode(o4),
                o5 == null ? NullHashCode : hashCode(o5),
                o6 == null ? NullHashCode : hashCode(o6),
                o7 == null ? NullHashCode : hashCode(o7));
    }

    public static int combineHashCodes(final Object o1, final Object o2,
            final Object o3, final Object o4, final Object o5,
            final Object o6, final Object o7, final Object o8) {

        return combineHashCodes(o1 == null ? NullHashCode : hashCode(o1),
                o2 == null ? NullHashCode : hashCode(o2),
                o3 == null ? NullHashCode : hashCode(o3),
                o4 == null ? NullHashCode : hashCode(o4),
                o5 == null ? NullHashCode : hashCode(o5),
                o6 == null ? NullHashCode : hashCode(o6),
                o7 == null ? NullHashCode : hashCode(o7),
                o8 == null ? NullHashCode : hashCode(o8));
    }
}

Related

  1. deepHashCode(Object o)
  2. getIdentityAsHex(Object object)
  3. hash(Object o)
  4. hash(Object... objects)
  5. hash(Object... values)
  6. hashCode(final Object... objects)
  7. hashCode(final Object... objects)
  8. hashCode(Object o)
  9. hashCode(Object obj)