|
| 1 | +package datadog.trace.util; |
| 2 | + |
| 3 | +/** |
| 4 | + * This class is intended to be a drop-in replacement for the hashing portions of java.util.Objects. |
| 5 | + * This class provides more convenience methods for hashing primitives and includes overrides for |
| 6 | + * <code>hash</code> that take many argument lengths to avoid var-args allocation. |
| 7 | + */ |
| 8 | +public final class HashingUtils { |
| 9 | + private HashingUtils() {} |
| 10 | + |
| 11 | + public static final int hashCode(Object obj) { |
| 12 | + return obj != null ? obj.hashCode() : 0; |
| 13 | + } |
| 14 | + |
| 15 | + public static final int hash(boolean value) { |
| 16 | + return Boolean.hashCode(value); |
| 17 | + } |
| 18 | + |
| 19 | + public static final int hash(char value) { |
| 20 | + return Character.hashCode(value); |
| 21 | + } |
| 22 | + |
| 23 | + public static final int hash(byte value) { |
| 24 | + return Byte.hashCode(value); |
| 25 | + } |
| 26 | + |
| 27 | + public static final int hash(short value) { |
| 28 | + return Short.hashCode(value); |
| 29 | + } |
| 30 | + |
| 31 | + public static final int hash(int value) { |
| 32 | + return Integer.hashCode(value); |
| 33 | + } |
| 34 | + |
| 35 | + public static final int hash(long value) { |
| 36 | + return Long.hashCode(value); |
| 37 | + } |
| 38 | + |
| 39 | + public static final int hash(float value) { |
| 40 | + return Float.hashCode(value); |
| 41 | + } |
| 42 | + |
| 43 | + public static final int hash(double value) { |
| 44 | + return Double.hashCode(value); |
| 45 | + } |
| 46 | + |
| 47 | + public static final int hash(Object obj) { |
| 48 | + return obj != null ? obj.hashCode() : 0; |
| 49 | + } |
| 50 | + |
| 51 | + public static final int hash(Object obj0, Object obj1) { |
| 52 | + return hash(hash(obj0), hash(obj1)); |
| 53 | + } |
| 54 | + |
| 55 | + public static final int hash(int hash0, int hash1) { |
| 56 | + return 31 * hash0 + hash1; |
| 57 | + } |
| 58 | + |
| 59 | + public static final int hash(Object obj0, Object obj1, Object obj2) { |
| 60 | + return hash(hashCode(obj0), hashCode(obj1), hashCode(obj2)); |
| 61 | + } |
| 62 | + |
| 63 | + public static final int hash(int hash0, int hash1, int hash2) { |
| 64 | + // DQH - Micro-optimizing, 31 * 31 will constant fold |
| 65 | + // Since there are multiple execution ports for load & store, |
| 66 | + // this will make good use of the core. |
| 67 | + return 31 * 31 * hash0 + 31 * hash1 + hash2; |
| 68 | + } |
| 69 | + |
| 70 | + public static final int hash(Object obj0, Object obj1, Object obj2, Object obj3) { |
| 71 | + return hash(hashCode(obj0), hashCode(obj1), hashCode(obj2), hashCode(obj3)); |
| 72 | + } |
| 73 | + |
| 74 | + public static final int hash(int hash0, int hash1, int hash2, int hash3) { |
| 75 | + // DQH - Micro-optimizing, 31 * 31 will constant fold |
| 76 | + // Since there are multiple execution ports for load & store, |
| 77 | + // this will make good use of the core. |
| 78 | + return 31 * 31 * 31 * hash0 + 31 * 31 * hash1 + 31 * hash2 + hash3; |
| 79 | + } |
| 80 | + |
| 81 | + public static final int hash(Object obj0, Object obj1, Object obj2, Object obj3, Object obj4) { |
| 82 | + return hash(hashCode(obj0), hashCode(obj1), hashCode(obj2), hashCode(obj3)); |
| 83 | + } |
| 84 | + |
| 85 | + public static final int hash(int hash0, int hash1, int hash2, int hash3, int hash4) { |
| 86 | + // DQH - Micro-optimizing, 31 * 31 will constant fold |
| 87 | + // Since there are multiple execution ports for load & store, |
| 88 | + // this will make good use of the core. |
| 89 | + return 31 * 31 * 31 * 31 * hash0 + 31 * 31 * 31 * hash1 + 31 * 31 * hash2 + 31 * hash3 + hash4; |
| 90 | + } |
| 91 | + |
| 92 | + @Deprecated |
| 93 | + public static final int hash(int[] hashes) { |
| 94 | + int result = 0; |
| 95 | + for (int hash : hashes) { |
| 96 | + result = addToHash(result, hash); |
| 97 | + } |
| 98 | + return result; |
| 99 | + } |
| 100 | + |
| 101 | + public static final int addToHash(int hash, int value) { |
| 102 | + return 31 * hash + value; |
| 103 | + } |
| 104 | + |
| 105 | + public static final int addToHash(int hash, Object obj) { |
| 106 | + return addToHash(hash, hashCode(obj)); |
| 107 | + } |
| 108 | + |
| 109 | + public static final int addToHash(int hash, boolean value) { |
| 110 | + return addToHash(hash, Boolean.hashCode(value)); |
| 111 | + } |
| 112 | + |
| 113 | + public static final int addToHash(int hash, char value) { |
| 114 | + return addToHash(hash, Character.hashCode(value)); |
| 115 | + } |
| 116 | + |
| 117 | + public static final int addToHash(int hash, byte value) { |
| 118 | + return addToHash(hash, Byte.hashCode(value)); |
| 119 | + } |
| 120 | + |
| 121 | + public static final int addToHash(int hash, short value) { |
| 122 | + return addToHash(hash, Short.hashCode(value)); |
| 123 | + } |
| 124 | + |
| 125 | + public static final int addToHash(int hash, long value) { |
| 126 | + return addToHash(hash, Long.hashCode(value)); |
| 127 | + } |
| 128 | + |
| 129 | + public static final int addToHash(int hash, float value) { |
| 130 | + return addToHash(hash, Float.hashCode(value)); |
| 131 | + } |
| 132 | + |
| 133 | + public static final int addToHash(int hash, double value) { |
| 134 | + return addToHash(hash, Double.hashCode(value)); |
| 135 | + } |
| 136 | + |
| 137 | + public static final int hash(Iterable<?> objs) { |
| 138 | + int result = 0; |
| 139 | + for (Object obj : objs) { |
| 140 | + result = addToHash(result, obj); |
| 141 | + } |
| 142 | + return result; |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * Calling this var-arg version can result in large amounts of allocation (see HashingBenchmark) |
| 147 | + * Rather than calliing this method, add another override of hash that handles a larger number of |
| 148 | + * arguments or use calls to addToHash. |
| 149 | + */ |
| 150 | + @Deprecated |
| 151 | + public static final int hash(Object[] objs) { |
| 152 | + int result = 0; |
| 153 | + for (Object obj : objs) { |
| 154 | + result = addToHash(result, obj); |
| 155 | + } |
| 156 | + return result; |
| 157 | + } |
| 158 | +} |
0 commit comments