| 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 | | | package org.jcoderz.commons.util; |
| 34 | | | |
| 35 | | | |
| 36 | | | import java.lang.reflect.Array; |
| 37 | | | |
| 38 | | | |
| 39 | | | |
| 40 | | | |
| 41 | | | <code></code> |
| 42 | | | |
| 43 | | | <pre> |
| 44 | | | |
| 45 | | | |
| 46 | | | |
| 47 | | | |
| 48 | | | |
| 49 | | | |
| 50 | | | |
| 51 | | | |
| 52 | | | |
| 53 | | | </pre> |
| 54 | | | |
| 55 | | | @author |
| 56 | | | |
| 57 | | | public final class HashCodeUtil |
| 58 | | | { |
| 59 | | | |
| 60 | | | <code></code> |
| 61 | | | |
| 62 | | | <code></code> |
| 63 | | | |
| 64 | | | public static final int SEED = 23; |
| 65 | | | |
| 66 | | | |
| 67 | | | <i></i> |
| 68 | | | |
| 69 | | | |
| 70 | | | private static final int ODD_PRIME_NUMBER = 37; |
| 71 | | | |
| 72 | | | |
| 73 | | | |
| 74 | | | |
| 75 | | | private HashCodeUtil () |
| 76 | 0 | | { |
| 77 | | | |
| 78 | 0 | | } |
| 79 | | | |
| 80 | | | |
| 81 | | | |
| 82 | | | @param |
| 83 | | | @param |
| 84 | | | @return |
| 85 | | | |
| 86 | | | public static int hash (int aSeed, boolean aBoolean) |
| 87 | | | { |
| 88 | 100 | | return firstTerm(aSeed) + (aBoolean ? 1 : 0); |
| 89 | | | } |
| 90 | | | |
| 91 | | | |
| 92 | | | |
| 93 | | | @param |
| 94 | | | @param |
| 95 | | | @return |
| 96 | | | |
| 97 | | | public static int hash (int aSeed, char aChar) |
| 98 | | | { |
| 99 | 100 | | return firstTerm(aSeed) + aChar; |
| 100 | | | } |
| 101 | | | |
| 102 | | | |
| 103 | | | |
| 104 | | | |
| 105 | | | |
| 106 | | | @param |
| 107 | | | @param |
| 108 | | | @return |
| 109 | | | |
| 110 | | | public static int hash (int aSeed, int aInt) |
| 111 | | | { |
| 112 | 100 | | return firstTerm(aSeed) + aInt; |
| 113 | | | } |
| 114 | | | |
| 115 | | | |
| 116 | | | |
| 117 | | | @param |
| 118 | | | @param |
| 119 | | | @return |
| 120 | | | |
| 121 | | | public static int hash (int aSeed, long aLong) |
| 122 | | | { |
| 123 | 100 | | return firstTerm(aSeed) + (int) (aLong |
| 124 | | | ^ (aLong >>> Constants.BITS_PER_INTEGER)); |
| 125 | | | } |
| 126 | | | |
| 127 | | | |
| 128 | | | |
| 129 | | | @param |
| 130 | | | @param |
| 131 | | | @return |
| 132 | | | |
| 133 | | | public static int hash (int aSeed, float aFloat) |
| 134 | | | { |
| 135 | 100 | | return hash(aSeed, Float.floatToIntBits(aFloat)); |
| 136 | | | } |
| 137 | | | |
| 138 | | | |
| 139 | | | |
| 140 | | | @param |
| 141 | | | @param |
| 142 | | | @return |
| 143 | | | |
| 144 | | | public static int hash (int aSeed, double aDouble) |
| 145 | | | { |
| 146 | 100 | | return hash(aSeed, Double.doubleToLongBits(aDouble)); |
| 147 | | | } |
| 148 | | | |
| 149 | | | |
| 150 | | | |
| 151 | | | |
| 152 | | | <code></code> |
| 153 | | | <code></code> |
| 154 | | | |
| 155 | | | |
| 156 | | | @param |
| 157 | | | @param |
| 158 | | | @return |
| 159 | | | |
| 160 | | | public static int hash (int aSeed, Object aObject) |
| 161 | | | { |
| 162 | 100 | | int result = aSeed; |
| 163 | | | |
| 164 | 100 | | if (aObject == null) |
| 165 | | | { |
| 166 | 100 | | result = hash(result, 0); |
| 167 | | | } |
| 168 | 100 | | else if (!isArray(aObject)) |
| 169 | | | { |
| 170 | 100 | | result = hash(result, aObject.hashCode()); |
| 171 | | | } |
| 172 | | | else |
| 173 | | | { |
| 174 | 100 | | final int length = Array.getLength(aObject); |
| 175 | 100 | | for (int i = 0; i < length; ++i) |
| 176 | | | { |
| 177 | 100 | | final Object item = Array.get(aObject, i); |
| 178 | | | |
| 179 | 100 | | result = hash(result, item); |
| 180 | | | } |
| 181 | 100 | | result = hash(result, length); |
| 182 | | | } |
| 183 | 100 | | return result; |
| 184 | | | } |
| 185 | | | |
| 186 | | | |
| 187 | | | |
| 188 | | | @param |
| 189 | | | @return |
| 190 | | | |
| 191 | | | private static int firstTerm (int aSeed) |
| 192 | | | { |
| 193 | 100 | | return ODD_PRIME_NUMBER * aSeed; |
| 194 | | | } |
| 195 | | | |
| 196 | | | |
| 197 | | | <code></code> |
| 198 | | | @return<code></code> |
| 199 | | | <code></code> |
| 200 | | | |
| 201 | | | private static boolean isArray (Object aObject) |
| 202 | | | { |
| 203 | 100 | | return aObject.getClass().isArray(); |
| 204 | | | } |
| 205 | | | } |