View Javadoc

1   /*
2    * File:    HashCodeUtil.java
3    * Created: 11.04.2007 15:49 
4    *
5    * Copyright 2007 Michal Burda.
6    *
7    * This program is free software; you can redistribute it and/or modify
8    * it under the terms of the GNU General Public License as published by
9    * the Free Software Foundation; either version 2 of the License, or
10   * (at your option) any later version.
11   *
12   * This program is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Public License for more details.
16   *
17   * You should have received a copy of the GNU General Public License
18   * along with this program; if not, write to the Free Software
19   * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20   */
21  
22  package net.sf.webmancer.util;
23  
24  import java.lang.reflect.Array;
25  
26  /**
27   * @author Michal Burda
28   */
29  public class HashCodeUtil {
30      /**
31       * An initial value for a <code>hashCode</code>, to which is added contributions from fields. Using a non-zero
32       * value decreases collisons of <code>hashCode</code> values.
33       */
34      public static final int SEED = 23;
35  
36      /**
37       * 
38       */
39      private static final int ODD_PRIME_NUMBER = 37;
40  
41      /**
42       * @param seed
43       * @param value
44       * @return
45       */
46      public static int hash(int seed, boolean value) {
47          return (ODD_PRIME_NUMBER * seed) + (value ? 1 : 0);
48      }
49  
50      /**
51       * @param seed
52       * @param value
53       * @return
54       */
55      public static int hash(int seed, char value) {
56          return (ODD_PRIME_NUMBER * seed) + value;
57      }
58  
59      /**
60       * @param seed
61       * @param value
62       * @return
63       */
64      public static int hash(int seed, int value) {
65          return (ODD_PRIME_NUMBER * seed) + value;
66      }
67  
68      /**
69       * @param seed
70       * @param value
71       * @return
72       */
73      public static int hash(int seed, long value) {
74          return (ODD_PRIME_NUMBER * seed) + (int) (value ^ (value >>> 32));
75      }
76  
77      /**
78       * @param seed
79       * @param value
80       * @return
81       */
82      public static int hash(int seed, float value) {
83          return hash(seed, Float.floatToIntBits(value));
84      }
85  
86      /**
87       * @param seed
88       * @param value
89       * @return
90       */
91      public static int hash(int seed, double value) {
92          return hash(seed, Double.doubleToLongBits(value));
93      }
94  
95      /**
96       * @param seed
97       * @param value
98       * @return
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      * Prevent instantiation
118      */
119     private HashCodeUtil() {
120     }
121 
122 }