Java Hash Code Calculate hashCodeSafe(final Object value)

Here you can find the source of hashCodeSafe(final Object value)

Description

Determines the hash code of the given value by calling the hashCode method on the given object if it is not null.

License

Open Source License

Parameter

Parameter Description
value The value to get the hash code for. May be null.

Return

The hash code of the given value if it is not null; otherwise, 0.

Declaration

public static int hashCodeSafe(final Object value) 

Method Source Code

//package com.java2s;
/*//from w  w  w .j a va2  s. c o  m
 * File:                ObjectUtil.java
 * Authors:             Justin Basilico
 * Company:             Sandia National Laboratories
 * Project:             Cognitive Foundry
 *
 * Copyright June 28, 2007, Sandia Corporation.  Under the terms of Contract
 * DE-AC04-94AL85000, there is a non-exclusive license for use of this work by
 * or on behalf of the U.S. Government. Export of this program may require a
 * license from the United States Government. See CopyrightHistory.txt for
 * complete details.
 *
 */

public class Main {
    /**
     * Determines the hash code of the given value by calling the hashCode
     * method on the given object if it is not null. If it is null, 0 is
     * returned.
     *
     * @param   value
     *      The value to get the hash code for. May be null.
     * @return
     *      The hash code of the given value if it is not null; otherwise, 0.
     */
    public static int hashCodeSafe(final Object value) {
        if (value == null) {
            return 0;
        } else {
            return value.hashCode();
        }
    }
}

Related

  1. hashCodeNullSafe(final Object o)
  2. hashCodeOf(Object o)
  3. hashCodeOrDefault(Object obj)
  4. hashCodeOrNull(Object[] array)
  5. hashCodeOrZero(Object o)