1 package cz.cuni.amis.utils;
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 public final class HashCode {
37
38 public int hash(boolean b){
39 return b ? 0 : 1;
40 }
41
42 public int hash(byte b){
43 return (int) b;
44 }
45
46 public int hash(char c){
47 return (int) c;
48 }
49
50 public int hash(short s){
51 return (int) s;
52 }
53
54 public int hash(int i){
55 return (int) i;
56 }
57
58 public int hash(long i){
59 return (int) i;
60 }
61
62 public int hash(float f){
63 return Float.floatToIntBits(f);
64 }
65
66 public int hash(double d){
67 long l = Double.doubleToLongBits(d);
68 return (int)(l ^ (l >>> 32));
69 }
70
71 public int hash(Object o){
72 return o == null ? 0 : o.hashCode();
73 }
74
75 private int hashCode;
76
77 public HashCode(){
78 hashCode = 17;
79 }
80
81 private HashCode addNumber(int number){
82 hashCode = 37 * hashCode + number;
83 return this;
84 }
85
86 public HashCode add(boolean b){
87 addNumber(hash(b));
88 return this;
89 }
90
91 public HashCode add(byte b){
92 addNumber(hash(b));
93 return this;
94 }
95
96 public HashCode add(char c){
97 addNumber(hash(c));
98 return this;
99 }
100
101 public HashCode add(short s){
102 addNumber(hash(s));
103 return this;
104 }
105
106 public HashCode add(int i){
107 addNumber(hash(i));
108 return this;
109 }
110
111 public HashCode add(long l){
112 addNumber(hash(l));
113 return this;
114 }
115
116 public HashCode add(float f){
117 addNumber(hash(f));
118 return this;
119 }
120
121 public HashCode add(double d){
122 addNumber(hash(d));
123 return this;
124 }
125
126 public HashCode add(Object o){
127 addNumber(hash(o));
128 return this;
129 }
130
131 public int getHash(){
132 return hashCode;
133 }
134
135 }