1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package net.sf.webmancer.util;
23
24 import java.lang.reflect.Array;
25
26
27
28
29 public class HashCodeUtil {
30
31
32
33
34 public static final int SEED = 23;
35
36
37
38
39 private static final int ODD_PRIME_NUMBER = 37;
40
41
42
43
44
45
46 public static int hash(int seed, boolean value) {
47 return (ODD_PRIME_NUMBER * seed) + (value ? 1 : 0);
48 }
49
50
51
52
53
54
55 public static int hash(int seed, char value) {
56 return (ODD_PRIME_NUMBER * seed) + value;
57 }
58
59
60
61
62
63
64 public static int hash(int seed, int value) {
65 return (ODD_PRIME_NUMBER * seed) + value;
66 }
67
68
69
70
71
72
73 public static int hash(int seed, long value) {
74 return (ODD_PRIME_NUMBER * seed) + (int) (value ^ (value >>> 32));
75 }
76
77
78
79
80
81
82 public static int hash(int seed, float value) {
83 return hash(seed, Float.floatToIntBits(value));
84 }
85
86
87
88
89
90
91 public static int hash(int seed, double value) {
92 return hash(seed, Double.doubleToLongBits(value));
93 }
94
95
96
97
98
99
100 public static int hash(int seed, Object value) {
101 int result = seed;
102 if (value == null) {
103 result = hash(result, 0);
104 } else if (!value.getClass().isArray()) {
105 result = hash(result, value.hashCode());
106 } else {
107 int length = Array.getLength(value);
108 for (int i = 0; i < length; ++i) {
109 Object item = Array.get(value, i);
110 result = hash(result, item);
111 }
112 }
113 return result;
114 }
115
116
117
118
119 private HashCodeUtil() {
120 }
121
122 }