1 21 22 package org.opensubsystems.core.util; 23 24 import java.lang.reflect.Array ; 25 26 48 public final class HashCodeUtils 49 { 50 52 55 public static final int SEED = 15; 56 57 60 public static final int ODD_PRIME_NUMBER = 37; 61 62 64 67 private HashCodeUtils( 68 ) 69 { 70 } 72 73 75 82 public static int hash( 83 int iSeed, 84 boolean bValue 85 ) 86 { 87 return firstTerm(iSeed) + (bValue ? 1 : 0); 88 } 89 90 97 public static int hash( 98 int iSeed, 99 char cValue 100 ) 101 { 102 return firstTerm(iSeed) + (int)cValue; 103 } 104 105 113 public static int hash( 114 int iSeed, 115 int iValue 116 ) 117 { 118 return firstTerm(iSeed) + iValue; 119 } 120 121 128 public static int hash( 129 int iSeed, 130 long lValue 131 ) 132 { 133 return firstTerm(iSeed) + (int)(lValue ^ (lValue >>> 32)); 134 } 135 136 143 public static int hash( 144 int iSeed, 145 float fValue 146 ) 147 { 148 return hash(iSeed, Float.floatToIntBits(fValue)); 149 } 150 151 158 public static int hash( 159 int iSeed, 160 double dValue 161 ) 162 { 163 return hash(iSeed, Double.doubleToLongBits(dValue)); 164 } 165 166 176 public static int hash( 177 int iSeed, 178 Object oValue 179 ) 180 { 181 int result = iSeed; 182 if (oValue == null) 183 { 184 result = hash(result, 0); 185 } 186 else if (!isArray(oValue)) 187 { 188 result = hash(result, oValue.hashCode()); 189 } 190 else 191 { 192 int length = Array.getLength(oValue); 193 for (int idx = 0; idx < length; ++idx) 194 { 195 Object item = Array.get(oValue, idx); 196 result = hash(result, item); 198 } 199 } 200 return result; 201 } 202 203 205 211 protected static int firstTerm( 212 int iSeed 213 ) 214 { 215 return ODD_PRIME_NUMBER * iSeed; 216 } 217 218 224 protected static boolean isArray( 225 Object oObject 226 ) 227 { 228 return oObject.getClass().isArray(); 229 } 230 } 231 | Popular Tags |