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++; } } }
public boolean tryAcquireSharedLock(T value, long timeout) throws InterruptedException { if (useSpinLock) throw new IllegalStateException("Spin lock does not support try lock mode"); final int index; if (value == null) index = 0; else index = index(value.hashCode()); final ReadWriteLock rwLock = locks[index]; final Lock lock = rwLock.readLock(); return lock.tryLock(timeout, TimeUnit.MILLISECONDS); }
public void releaseExclusiveLock(final T value) { final int index; if (value == null) index = 0; else index = index(value.hashCode()); if (useSpinLock) { OReadersWriterSpinLock spinLock = spinLocks[index]; spinLock.releaseWriteLock(); return; } final ReadWriteLock rwLock = locks[index]; final Lock lock = rwLock.writeLock(); lock.unlock(); }
@Override public Lock acquireSharedLock(final T value) { final int index; if (value == null) index = 0; else index = index(value.hashCode()); if (useSpinLock) { OReadersWriterSpinLock spinLock = spinLocks[index]; spinLock.acquireReadLock(); return new SpinLockWrapper(true, spinLock); } final ReadWriteLock rwLock = locks[index]; final Lock lock = rwLock.readLock(); lock.lock(); return lock; }
@UsedFromByteCode public static <T> int hashCode(T elementType) { return elementType == null ? Integer.MIN_VALUE : elementType.hashCode(); }
private int hash(T item){ return Math.abs(item.hashCode()) % table.length; }
private int hash(T element) { return Math.abs(element.hashCode() % table.length); }