Example usage for java.lang Object hashCode

List of usage examples for java.lang Object hashCode

Introduction

In this page you can find the example usage for java.lang Object hashCode.

Prototype

@HotSpotIntrinsicCandidate
public native int hashCode();

Source Link

Document

Returns a hash code value for the object.

Usage

From source file:org.springmodules.cache.util.Reflections.java

/**
 * <p>/* ww  w.  jav  a2  s.  co m*/
 * This method uses reflection to build a valid hash code.
 * </p>
 *
 * <p>
 * It uses <code>AccessibleObject.setAccessible</code> to gain access to
 * private fields. This means that it will throw a security exception if run
 * under a security manager, if the permissions are not set up correctly. It
 * is also not as efficient as testing explicitly.
 * </p>
 *
 * <p>
 * Transient members will not be used, as they are likely derived fields,
 * and not part of the value of the <code>Object</code>.
 * </p>
 *
 * <p>
 * Static fields will not be tested. Superclass fields will be included.
 * </p>
 *
 * @param obj
 *          the object to create a <code>hashCode</code> for
 * @return the generated hash code, or zero if the given object is
 *         <code>null</code>
 */
public static int reflectionHashCode(Object obj) {
    if (obj == null)
        return 0;

    Class targetClass = obj.getClass();
    if (Objects.isArrayOfPrimitives(obj) || Objects.isPrimitiveOrWrapper(targetClass)) {
        return Objects.nullSafeHashCode(obj);
    }

    if (targetClass.isArray()) {
        return reflectionHashCode((Object[]) obj);
    }

    if (obj instanceof Collection) {
        return reflectionHashCode((Collection) obj);
    }

    if (obj instanceof Map) {
        return reflectionHashCode((Map) obj);
    }

    // determine whether the object's class declares hashCode() or has a
    // superClass other than java.lang.Object that declares hashCode()
    Class clazz = (obj instanceof Class) ? (Class) obj : obj.getClass();
    Method hashCodeMethod = ReflectionUtils.findMethod(clazz, "hashCode", new Class[0]);

    if (hashCodeMethod != null) {
        return obj.hashCode();
    }

    // could not find a hashCode other than the one declared by java.lang.Object
    int hash = INITIAL_HASH;

    try {
        while (targetClass != null) {
            Field[] fields = targetClass.getDeclaredFields();
            AccessibleObject.setAccessible(fields, true);

            for (int i = 0; i < fields.length; i++) {
                Field field = fields[i];
                int modifiers = field.getModifiers();

                if (!Modifier.isStatic(modifiers) && !Modifier.isTransient(modifiers)) {
                    hash = MULTIPLIER * hash + reflectionHashCode(field.get(obj));
                }
            }
            targetClass = targetClass.getSuperclass();
        }
    } catch (IllegalAccessException exception) {
        // ///CLOVER:OFF
        ReflectionUtils.handleReflectionException(exception);
        // ///CLOVER:ON
    }

    return hash;
}

From source file:org.web4thejob.web.controller.AbstractComponentController.java

@Override
public boolean equals(Object o) {
    if (o == null)
        return false;
    return hashCode() == o.hashCode();
}

From source file:com.github.drbookings.model.data.IDedTest.java

@Test
public void testIdentity03() {
    i = new IDed();
    final Object o = new Object();
    assertNotEquals(i.hashCode(), o.hashCode());
    assertNotEquals(i, o);//  www.j  a  v a 2  s .c  o m
}

From source file:org.codesearch.commons.plugins.vcs.FileIdentifier.java

@Override
public boolean equals(Object o) {
    if (o instanceof FileIdentifier) {
        return o.hashCode() == this.hashCode();
    }//from w  w  w .j a  v  a  2  s .c  o  m
    return false;
}

From source file:org.deploymentobjects.core.domain.model.execution.DispatchableStep.java

@Override
public int compareTo(Object o) {
    return (o.hashCode() > hashCode()) ? -1 : 1;
}

From source file:de.luzzifa.spring.test.unit.TestCustomBeanCreation.java

@Test
public void testCreateBean3TimesWorks() {
    for (int x = 1; x <= 99; ++x) {
        Object protoBean = context.getBean("testBean");
        System.out.println("ProtoBean" + x + ": " + protoBean + "@" + protoBean.hashCode());

    }/*from   www . j  ava 2 s  .c  o m*/
}

From source file:gov.nih.nci.caarray.util.URIUserType.java

/**
 * {@inheritDoc}
 */
@Override
public int hashCode(Object x) {
    return x.hashCode();
}

From source file:org.knime.knip.base.data.ObjectRepository.java

/**
 * Get cached object. Returns null if object was released due to certain memory management conditions.
 *
 * @param obj//from  ww w  .j  a v  a 2 s.com
 *
 * @return the cached object
 */
public final Object getCachedObject(final Object obj) {
    if (CACHED_OBJECTS.get(obj.hashCode()) != null) {
        return CACHED_OBJECTS.get(obj.hashCode()).get();
    } else {
        return null;
    }
}

From source file:com.qrmedia.commons.persistence.hibernate.usertype.MutableUserType.java

public int hashCode(Object x) throws HibernateException {
    assert (x != null);
    return x.hashCode();
}

From source file:com.flipkart.flux.type.SetJsonType.java

@Override
public int hashCode(Object x) throws HibernateException {
    return x.hashCode();
}