View Javadoc

1   package cz.cuni.amis.utils;
2   
3   /**
4    * This class is meant for counting hash codes from any possible type.
5    * It declares a two heavily overloaded methods ;-).
6    * 
7    * 1) hash(whatever) -> returns hash number for 'whatever' (of whatever type)
8    * 2) add(whatever)  -> add number to the hashCode for 'whatever' (of whatever type)
9    * 
10   * Typical usage: 
11   * 
12   * Usualy you will create a method private countHashCode() which you will call from
13   * within the constructors after you've initialized variables from which you want
14   * to count the hash code. It will look like this:
15   * 
16   * private int getHashCode(){
17   * 	 HashCode hc = new HashCode(); // creating new HashCode instance
18   * 	 hc.add(myFirstIntValue);      // adding first parametr to hash code
19   *   hc.add(mySecondIntValue);     // second...
20   *   hc.add(myFloatValue);         // third...
21   *   return hc.getHash();          // returning the hash
22   * }
23   * 
24   * private final int hashCode;
25   * 
26   * public int ConstrucotrOfMyClass(){
27   *   // initializing variables		
28   *   hashCode = getHashCode();
29   * }
30   * 
31   * public int hashCode(){
32   * 	 return hashCode;
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 }