aboutsummaryrefslogtreecommitdiffhomepage
path: root/third_party/checker_framework_dataflow/java/org/checkerframework/dataflow/util/HashCodeUtils.java
blob: f898e3c9b447cfbf6600dbdc76a1261ce58dfc2e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package org.checkerframework.dataflow.util;

/**
 * Utility class to implement the {@code hashCode} method.
 *
 * @author Stefan Heule
 */
public class HashCodeUtils {

    /** Odd prime number. */
    private static int prime = 31;

    /** Seed. */
    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;
    }
}