| 1 | /** |
| 2 | * Copyright 2005-2011 Steve McDuff d-duff@users.sourceforge.net |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package org.deduced.viewer.web.shared; |
| 18 | |
| 19 | /** |
| 20 | * Collected methods which allow easy implementation of <code>hashCode</code>. |
| 21 | * Example use case: |
| 22 | * |
| 23 | * <pre> |
| 24 | * public int hashCode() |
| 25 | * { |
| 26 | * int result = HashCodeUtilities.SEED; |
| 27 | * // collect the contributions of various fields |
| 28 | * result = HashCodeUtilities.hash(result, fPrimitive); |
| 29 | * result = HashCodeUtilities.hash(result, fObject); |
| 30 | * result = HashCodeUtilities.hash(result, fArray); |
| 31 | * return result; |
| 32 | * } |
| 33 | * </pre> |
| 34 | * |
| 35 | * @author Steve McDuff |
| 36 | */ |
| 37 | public final class HashCodeUtilities |
| 38 | { |
| 39 | |
| 40 | /** |
| 41 | * bit count of a Integer |
| 42 | */ |
| 43 | public static final int INTEGER_BIT_COUNT = 32; |
| 44 | |
| 45 | /** |
| 46 | * An initial value for a <code>hashCode</code>, to which is added |
| 47 | * contributions from fields. Using a non-zero value decreases collisions of |
| 48 | * <code>hashCode</code> values. |
| 49 | */ |
| 50 | public static final int SEED = 23; |
| 51 | |
| 52 | /** |
| 53 | * merge hash code with a boolean. |
| 54 | * |
| 55 | * @param aSeed the seed hash code |
| 56 | * @param aBoolean the boolean value |
| 57 | * @return the boolean hash code |
| 58 | */ |
| 59 | public static int hash( |
| 60 | int aSeed, boolean aBoolean) |
| 61 | { |
| 62 | if (aBoolean) |
| 63 | { |
| 64 | return firstTerm(aSeed) + 1; |
| 65 | } |
| 66 | return firstTerm(aSeed); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * merge hash code with a char. |
| 71 | * |
| 72 | * @param aSeed the seed hash code |
| 73 | * @param aChar the char value |
| 74 | * @return the merged hash code |
| 75 | */ |
| 76 | public static int hash( |
| 77 | int aSeed, char aChar) |
| 78 | { |
| 79 | return firstTerm(aSeed) + aChar; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * merge hash code with a int. |
| 84 | * |
| 85 | * @param aSeed the seed hash code |
| 86 | * @param aInt the int value |
| 87 | * @return the merged hash code |
| 88 | */ |
| 89 | public static int hash( |
| 90 | int aSeed, int aInt) |
| 91 | { |
| 92 | /* |
| 93 | * Implementation Note Note that byte and short are handled by this |
| 94 | * method, through implicit conversion. |
| 95 | */ |
| 96 | return firstTerm(aSeed) + aInt; |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * merge hash code with a long. |
| 101 | * |
| 102 | * @param aSeed the seed hash code |
| 103 | * @param aLong the long value |
| 104 | * @return the merged hash code |
| 105 | */ |
| 106 | public static int hash( |
| 107 | int aSeed, long aLong) |
| 108 | { |
| 109 | return firstTerm(aSeed) + (int) (aLong ^ (aLong >>> INTEGER_BIT_COUNT)); |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * merge hash code with a float. |
| 114 | * |
| 115 | * @param aSeed the seed hash code |
| 116 | * @param aFloat the float value |
| 117 | * @return the merged hash code |
| 118 | */ |
| 119 | public static int hash( |
| 120 | int aSeed, float aFloat) |
| 121 | { |
| 122 | Float myFloat = new Float(aFloat); |
| 123 | return hash(aSeed, myFloat.hashCode()); |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * merge hash code with a double. |
| 128 | * |
| 129 | * @param aSeed the seed hash code |
| 130 | * @param aDouble the double value |
| 131 | * @return the merged hash code |
| 132 | */ |
| 133 | public static int hash( |
| 134 | int aSeed, double aDouble) |
| 135 | { |
| 136 | Double myDouble = new Double(aDouble); |
| 137 | return hash(aSeed, myDouble.hashCode()); |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * merge hash code with a java object. <code>aObject</code> is a |
| 142 | * possibly-null object field, and possibly an array. If |
| 143 | * <code>aObject</code> is an array, then each element may be a primitive or |
| 144 | * a possibly-null object. |
| 145 | * |
| 146 | * @param aSeed the seed hash code |
| 147 | * @param aObject the object |
| 148 | * @return the merged hash code |
| 149 | */ |
| 150 | public static int hash( |
| 151 | int aSeed, Object aObject) |
| 152 | { |
| 153 | int result = aSeed; |
| 154 | if (aObject == null) |
| 155 | { |
| 156 | result = hash(result, 0); |
| 157 | } |
| 158 | else if (!isArray(aObject)) |
| 159 | { |
| 160 | result = hash(result, aObject.hashCode()); |
| 161 | } |
| 162 | else |
| 163 | { |
| 164 | Object[] array = (Object[]) aObject; |
| 165 | int length = array.length; |
| 166 | for (int idx = 0; idx < length; ++idx) |
| 167 | { |
| 168 | Object item = array[idx]; |
| 169 | // recursive call! |
| 170 | result = hash(result, item); |
| 171 | } |
| 172 | } |
| 173 | return result; |
| 174 | } |
| 175 | |
| 176 | /** a random prime number to use when merging hash codes */ |
| 177 | private static final int INITIAL_PRIME_NUMBER = 37; |
| 178 | |
| 179 | /** |
| 180 | * modify the hash code for merging |
| 181 | * |
| 182 | * @param aSeed the current hash code |
| 183 | * @return the hash code ready to be merged |
| 184 | */ |
| 185 | private static int firstTerm( |
| 186 | int aSeed) |
| 187 | { |
| 188 | return INITIAL_PRIME_NUMBER * aSeed; |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * test if an object is an array |
| 193 | * |
| 194 | * @param aObject the object to test |
| 195 | * @return true if the object is an array |
| 196 | */ |
| 197 | private static boolean isArray( |
| 198 | Object aObject) |
| 199 | { |
| 200 | return aObject.getClass().isArray(); |
| 201 | } |
| 202 | } |