-
Notifications
You must be signed in to change notification settings - Fork 0
/
Hasher.java
66 lines (60 loc) · 1.59 KB
/
Hasher.java
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
/**
* Class hashing several types of data.
*/
class Hasher {
/**
* Calculates hash code of an element.
* Calls explicitly defined function for several types and
* default hash function for other types.
*
* @param el element to be hashed
* @param <T> type of the element
* @return hash code
*/
static <T> int getHashCode(T el) {
if (el instanceof Integer) {
return getHashCode((Integer) el);
}
else if (el instanceof String) {
return getHashCode((String) el);
}
return el.hashCode();
}
/**
* Hashes int.
* Just returns its value.
*
* @param el int for hashing
* @return hash code of the element
*/
private static int getHashCode(Integer el) {
return el;
}
/**
* Hashes string.
* Assumes average text - both lower and uppercase letters,
* numbers, punctuation marks, etc. Total number of characters
* will be about 26*2 + 10 + 10 += 5, so constant is closest prime number 79.
*
* @param el string for hashing
* @return hash code of the element
*/
private static int getHashCode(String el) {
int hash = 0;
final int c = 79;
for (int i = 0; i < el.length(); i++) {
hash = c * hash + el.charAt(i) + 1;
}
return hash;
}
/**
* Hashes char.
* Returns its integer value.
*
* @param el char for hashing
* @return hash code of the element
*/
private static int getHashCode(Character el) {
return (int) el;
}
}