@Override public int hashCode() { if (ob1 == null && ob2 == null) return 0; if (ob1 == null) return ob2.hashCode() % 1499153; if (ob2 == null) return ob1.hashCode() % 1499153; return (ob1.hashCode() + ob2.hashCode()) % 1499153; }
public static <T> void assertEqualsAndHashcode( T objectUnderTest, Collection<T> equalObjects, Collection<T> notEqualObjects) { for (T equalObject : equalObjects) { assertThat( "The object under test should have been equal to the specified object!", objectUnderTest, equalTo(equalObject)); assertThat( "The object under test should have had the same hashcode as the specified object!", objectUnderTest.hashCode(), equalTo(equalObject.hashCode())); } for (T notEqualObject : notEqualObjects) { assertThat( "The object under test should not have been equal to the specified object!", objectUnderTest, is(not(equalTo(notEqualObject)))); assertThat( "The object under test should not have had the same hashcode as the specified object!", objectUnderTest.hashCode(), is(not(equalTo(notEqualObject.hashCode())))); } assertFalse( "The object under test should not have been equal to a random string!", objectUnderTest.equals("somestring")); }
@Override public int hashCode() { int hashCode = 17; hashCode = hashCode * 37 + first.hashCode(); hashCode = hashCode * 37 + second.hashCode(); return hashCode; }
public void apply() throws IllegalAccessException, InvocationTargetException, InstantiationException { if (type.isEnum()) { for (T instance : type.getEnumConstants()) { assertThat( instance.toString(), is( type.getCanonicalName().substring(type.getPackage().getName().length() + 1) + "." + ((Enum<?>) instance).name())); } return; } for (Constructor<?> constructor : type.getDeclaredConstructors()) { if (constructor.isSynthetic() && skipSynthetic) { continue; } constructor.setAccessible(true); Class<?>[] parameterTypes = constructor.getParameterTypes(); Object[] actualArguments = new Object[parameterTypes.length]; Object[] otherArguments = new Object[parameterTypes.length]; int index = 0; for (Class<?> parameterType : parameterTypes) { putInstance(parameterType, actualArguments, otherArguments, index++); } int testIndex = 0; @SuppressWarnings("unchecked") T instance = (T) constructor.newInstance(actualArguments); assertThat(instance, is(instance)); assertThat(instance, not(is((Object) null))); assertThat(instance, not(is(new Object()))); Object similarInstance = constructor.newInstance(actualArguments); assertThat(instance.hashCode(), is(similarInstance.hashCode())); assertThat(instance, is(similarInstance)); if (skipToString) { assertThat(instance.toString(), notNullValue()); } else if (optionalToStringRegex == null) { checkString(instance); } else { assertThat(instance.toString(), new RegexMatcher(optionalToStringRegex)); } for (Object otherArgument : otherArguments) { Object[] compareArguments = new Object[actualArguments.length]; int argumentIndex = 0; for (Object actualArgument : actualArguments) { if (argumentIndex == testIndex) { compareArguments[argumentIndex] = otherArgument; } else { compareArguments[argumentIndex] = actualArgument; } argumentIndex++; } Object unlikeInstance = constructor.newInstance(compareArguments); assertThat(instance.hashCode(), not(is(unlikeInstance))); assertThat(instance, not(is(unlikeInstance))); testIndex++; } } }
@Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((left == null) ? 0 : left.hashCode()); result = prime * result + ((right == null) ? 0 : right.hashCode()); return result; }
@Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((my == null) ? 0 : my.hashCode()); result = prime * result + ((their == null) ? 0 : their.hashCode()); return result; }
@Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((max == null) ? 0 : max.hashCode()); result = prime * result + ((min == null) ? 0 : min.hashCode()); result = prime * result + ((lower == null) ? 0 : lower.hashCode()); return result; }
public int hashCode() { int accu = 0; if (first != null) accu += first.hashCode(); if (second != null) { accu *= 31; accu += second.hashCode(); } return accu; }
@Override public final int hashCode() { int h = field.hashCode(); h ^= (lowerVal != null) ? lowerVal.hashCode() : 550356204; h = (h << 1) | (h >>> 31); // rotate to distinguish lower from upper h ^= (upperVal != null) ? upperVal.hashCode() : -1674416163; h ^= (parser != null) ? parser.hashCode() : -1572457324; h ^= (includeLower ? 1549299360 : -365038026) ^ (includeUpper ? 1721088258 : 1948649653); return h; }
/** * Gets a suitable hash code for the range. * * @return a hash code value for this object */ @Override public int hashCode() { int result = hashCode; if (hashCode == 0) { result = 17; result = 37 * result + getClass().hashCode(); result = 37 * result + minimum.hashCode(); result = 37 * result + maximum.hashCode(); hashCode = result; } return result; }
/** test the hashcode method * */ @Test public void testHashCode() { // test hash code and equals method equality final T muppet = this.newValueObject(); final long hash = muppet.hashCode(); // consistent for (int i = 0; i < iterateLength; i++) { Assert.assertEquals("Failed hash code iterate consistency test.", hash, muppet.hashCode()); } // equals and hash are equal final T a = this.newValueObject(); final T b = this.copyValueObject(a); Assert.assertEquals("Failed equals comparison test.", a, b); Assert.assertEquals("Failed hash code comparison test.", a.hashCode(), b.hashCode()); }
/** * @param structure * @return The record in cache if exists or the record itself and cache it. */ private T initStructure(final T structure, final boolean created) { final T inCache = cache.getIfPresent(structure.hashCode()); if (inCache != null) return inCache; cache.put(structure.hashCode(), structure); final ServerStorageStructurePrivateBase privateBase = ((ServerStorageStructurePrivateBase) structure); privateBase.setOwnerAndTracker(this, changeTracker); if (created) privateBase.onCreate(); return structure; }
/** * Remove an element. * * @param item element to remove * @return true iff element was present */ public boolean remove(T item) { int key = item.hashCode(); while (true) { Entry pred = this.head; Entry curr = pred.next; while (curr.key < key) { pred = curr; curr = curr.next; } pred.lock(); curr.lock(); try { if (validate(pred, curr)) { if (curr.key == key) { // present in list pred.next = curr.next; return true; } else { // not present in list return false; } } } finally { // always unlock pred.unlock(); curr.unlock(); } } }
/* * (non-Javadoc) * * @see java.lang.Object#hashCode() */ @Override public int hashCode() { if (value == null) { return 0; } return value.hashCode(); }
@Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((value == null) ? 0 : value.hashCode()); return result; }
/** Returns true if the key was removed. */ public boolean remove(T key) { int hashCode = key.hashCode(); int index = hashCode & mask; if (key.equals(keyTable[index])) { keyTable[index] = null; size--; return true; } index = hash2(hashCode); if (key.equals(keyTable[index])) { keyTable[index] = null; size--; return true; } index = hash3(hashCode); if (key.equals(keyTable[index])) { keyTable[index] = null; size--; return true; } return removeStash(key); }
/** Skips checks for existing keys. */ private void addResize(T key) { // Check for empty buckets. int hashCode = key.hashCode(); int index1 = hashCode & mask; T key1 = keyTable[index1]; if (key1 == null) { keyTable[index1] = key; if (size++ >= threshold) resize(capacity << 1); return; } int index2 = hash2(hashCode); T key2 = keyTable[index2]; if (key2 == null) { keyTable[index2] = key; if (size++ >= threshold) resize(capacity << 1); return; } int index3 = hash3(hashCode); T key3 = keyTable[index3]; if (key3 == null) { keyTable[index3] = key; if (size++ >= threshold) resize(capacity << 1); return; } push(key, index1, key1, index2, key2, index3, key3); }
@Override public int hashCode() { final int prime = 31; int result = 1; int add; if (prim == null) { add = 0; } else { add = prim.hashCode(); } result = prime * result + add; if (sec == null) { add = 0; } else { add = sec.hashCode(); } result = prime * result + add; if (third == null) { add = 0; } else { add = third.hashCode(); } result = prime * result + add; return result; }
/* * (non-Javadoc) * @see assignment6.HashFunction#hash(java.lang.Object) */ @Override public int hash(T input) { if (null == input) { return 0; } return input.hashCode(); }
@Override public int hashCode() { int result = name.hashCode(); result = 31 * result + (value != null ? value.hashCode() : 0); result = 31 * result + (isNegated ? 1 : 0); return result; }
@Override protected int computeHash() { int hashCode = 1; hashCode = hashCode * Utils.HASH_PRIME + value.hashCode(); hashCode = hashCode * Utils.HASH_PRIME + bitwidth; return hashCode; }
// return hash public int hashCode(IList<T> list) { int hash = 1; for (T v : list) hash = hash * 31 + v.hashCode(); return hash; }
public int compare(T o1, T o2) { if (o1 == o2) return 0; if (o1 == null) { return 1; } else if (o2 == null) { return -1; } int delta = o1.hashCode() - o2.hashCode(); if (delta == 0) { if (o1.equals(o2)) { return 0; } else { throw new RuntimeException("Hash collision for inequal objects: " + o1 + " && " + o2); } } return delta > 0 ? 1 : -1; };
/** * Get a hashCode for the real vector. * * <p>All NaN values have the same hash code. * * @return a hash code value for this object */ @Override public int hashCode() { int h = 3542; for (final T a : data) { h = h ^ a.hashCode(); } return h; }
@Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + (context == null ? 0 : context.hashCode()); result = prime * result + (instance == null ? 0 : instance.hashCode()); return result; }
@Override public int hashCode() { int hashCode = 0; for (T coordinate : coordinates) { hashCode ^= coordinate.hashCode(); } return hashCode; }
public int hashCode() { T obj = get(); if (obj != null) { return obj.hashCode(); } else { return -1; } }
@Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((param == null) ? 0 : param.hashCode()); result = prime * result + ((type == null) ? 0 : type.hashCode()); return result; }
@Override protected int hashCodeInternal() { if (delegate == null) { return super.hashCodeInternal(); } else { return delegate.hashCode(); } }
private void push(T insertKey, int index1, T key1, int index2, T key2, int index3, T key3) { T[] keyTable = this.keyTable; int mask = this.mask; // Push keys until an empty bucket is found. T evictedKey; int i = 0, pushIterations = this.pushIterations; do { // Replace the key and value for one of the hashes. switch (MathUtils.random(2)) { case 0: evictedKey = key1; keyTable[index1] = insertKey; break; case 1: evictedKey = key2; keyTable[index2] = insertKey; break; default: evictedKey = key3; keyTable[index3] = insertKey; break; } // If the evicted key hashes to an empty bucket, put it there and stop. int hashCode = evictedKey.hashCode(); index1 = hashCode & mask; key1 = keyTable[index1]; if (key1 == null) { keyTable[index1] = evictedKey; if (size++ >= threshold) resize(capacity << 1); return; } index2 = hash2(hashCode); key2 = keyTable[index2]; if (key2 == null) { keyTable[index2] = evictedKey; if (size++ >= threshold) resize(capacity << 1); return; } index3 = hash3(hashCode); key3 = keyTable[index3]; if (key3 == null) { keyTable[index3] = evictedKey; if (size++ >= threshold) resize(capacity << 1); return; } if (++i == pushIterations) break; insertKey = evictedKey; } while (true); addStash(evictedKey); }