Java Hash Calculate hash(Object value, int seed)

Here you can find the source of hash(Object value, int seed)

Description

Alters the given seed with the hash code value computed from the given value.

License

Apache License

Parameter

Parameter Description
value The value whose hash code to compute, or null .
seed The hash code value computed so far. If this method is invoked for the first field, then any arbitrary value (preferably different for each class) is okay.

Exception

Parameter Description
AssertionError If assertions are enabled and the given value is an array.

Return

An updated hash code value.

Declaration

public static int hash(Object value, int seed) throws AssertionError 

Method Source Code

//package com.java2s;
/* JAI-Ext - OpenSource Java Advanced Image Extensions Library
 *    http://www.geo-solutions.it//*from  w  w  w  .  j  a  v a  2 s.  c o  m*/
 *    Copyright 2014 GeoSolutions
    
    
 * 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.
 */

public class Main {
    /**
     * A prime number used for hash code computation.
     */
    private static final int PRIME_NUMBER = 37;

    /**
     * Alters the given seed with the hash code value computed from the given value. The givan object may be null. This method do <strong>not</strong>
     * iterates recursively in array elements. If array needs to be hashed, use one of {@link Arrays} method or {@link #deepHashCode deepHashCode}
     * instead.
     * <p>
     * <b>Note on assertions:</b> There is no way to ensure at compile time that this method is not invoked with an array argument, while doing so
     * would usually be a program error. Performing a systematic argument check would impose a useless overhead for correctly implemented
     * {@link Object#hashCode} methods. As a compromise we perform this check at runtime only if assertions are enabled. Using assertions for argument
     * check in a public API is usually a deprecated practice, but we make an exception for this particular method.
     * 
     * @param value The value whose hash code to compute, or {@code null}.
     * @param seed The hash code value computed so far. If this method is invoked for the first field, then any arbitrary value (preferably different
     *        for each class) is okay.
     * @return An updated hash code value.
     * @throws AssertionError If assertions are enabled and the given value is an array.
     */
    public static int hash(Object value, int seed) throws AssertionError {
        seed *= PRIME_NUMBER;
        if (value != null) {
            assert !value.getClass().isArray() : value;
            seed += value.hashCode();
        }
        return seed;
    }

    /**
     * Alters the given seed with the hash code value computed from the given value.
     * 
     * @param value The value whose hash code to compute.
     * @param seed The hash code value computed so far. If this method is invoked for the first field, then any arbitrary value (preferably different
     *        for each class) is okay.
     * @return An updated hash code value.
     */
    public static int hash(double value, int seed) {
        return hash(Double.doubleToLongBits(value), seed);
    }

    /**
     * Alters the given seed with the hash code value computed from the given value. {@code byte} and {@code short} primitive types are handled by
     * this method as well through implicit widening conversion.
     * 
     * @param value The value whose hash code to compute.
     * @param seed The hash code value computed so far. If this method is invoked for the first field, then any arbitrary value (preferably different
     *        for each class) is okay.
     * @return An updated hash code value.
     */
    public static int hash(long value, int seed) {
        return seed * PRIME_NUMBER + (((int) value) ^ ((int) (value >>> 32)));
    }
}

Related

  1. hash(Object obj)
  2. hash(Object obj)
  3. hash(Object object)
  4. hash(Object v)
  5. hash(Object value)
  6. hash(Object... as)
  7. hash(Object... value)
  8. hash(Object... values)
  9. hash(Object[] array)