Java Hash Calculate hash(int hash, boolean item)

Here you can find the source of hash(int hash, boolean item)

Description

Add a boolean value to a given hash.

License

Open Source License

Declaration

public static int hash(int hash, boolean item) 

Method Source Code

//package com.java2s;
//License from project: GNU General Public License 

public class Main {
    /** Odd prime number. */
    private static int prime = 31;
    /** Seed. *///from   w  w w. j a  va2  s .  co m
    private static int seed = 17;

    /** Add a boolean value to a given hash. */
    public static int hash(int hash, boolean item) {
        return hash * prime + (item ? 1 : 0);
    }

    /** Add a char value to a given hash. */
    public static int hash(int hash, char item) {
        return hash * prime + item;
    }

    /** Add an int value to a given hash. */
    public static int hash(int hash, int item) {
        return hash * prime + item;
    }

    /** Add a long value to a given hash. */
    public static int hash(int hash, long item) {
        return hash * prime + (int) (item ^ (item >>> 32));
    }

    /** Add a float value to a given hash. */
    public static int hash(int hash, float item) {
        return hash * prime + Float.floatToIntBits(item);
    }

    /** Add a double value to a given hash. */
    public static int hash(int hash, double item) {
        long l = Double.doubleToLongBits(item);
        return seed * prime + (int) (l ^ (l >>> 32));
    }

    /** Add an object to a given hash. */
    public static int hash(int hash, Object item) {
        if (item == null) {
            return hash * prime;
        }
        return hash * prime + item.hashCode();
    }

    /** Hash a boolean value. */
    public static int hash(boolean item) {
        return (item ? 1 : 0);
    }

    /** Hash a char value. */
    public static int hash(char item) {
        return item;
    }

    /** Hash an int value. */
    public static int hash(int item) {
        return item;
    }

    /** Hash a long value. */
    public static int hash(long item) {
        return (int) (item ^ (item >>> 32));
    }

    /** Hash a float value. */
    public static int hash(float item) {
        return Float.floatToIntBits(item);
    }

    /** Hash a double value. */
    public static int hash(double item) {
        long l = Double.doubleToLongBits(item);
        return (int) (l ^ (l >>> 32));
    }

    /** Hash an object. */
    public static int hash(Object item) {
        if (item == null) {
            return 0;
        }
        return item.hashCode();
    }

    /** Hash multiple objects. */
    public static int hash(Object... items) {
        int result = seed;
        for (Object item : items) {
            result = result * prime + (item == null ? 0 : item.hashCode());
        }
        return result;
    }
}

Related

  1. hash(int base, boolean field)
  2. hash(int h)
  3. hash(int h)
  4. hash(int h, boolean v)
  5. hash(int h, Object o)
  6. hash(int i, int j)
  7. hash(int k)
  8. hash(int k)
  9. hash(int key)